From a223adade6e734d97787f3ca486970877f08af1e Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sat, 18 Jul 2020 15:42:17 +0400 Subject: Added program repository Signed-off-by: Valentin Popov --- src/Repositories/ProgramRepository.php | 176 +++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 src/Repositories/ProgramRepository.php (limited to 'src/Repositories/ProgramRepository.php') diff --git a/src/Repositories/ProgramRepository.php b/src/Repositories/ProgramRepository.php new file mode 100644 index 0000000..494b957 --- /dev/null +++ b/src/Repositories/ProgramRepository.php @@ -0,0 +1,176 @@ + + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Repositories + */ +final class ProgramRepository implements BaseRepository { + /** + * @var string + */ + private const METHOD = 'file/%s'; + + /** + * @var string + */ + private string $channel_id; + + /** + * @var \GuzzleHttp\Client + */ + private Client $client; + + /** + * @param \EPGService\Environments\ServiceEnvironment $environment + * @param string $channel_id + */ + private function __construct(ServiceEnvironment $environment, string $channel_id) { + $this->channel_id = $channel_id; + + $this->client = new Client([ + 'base_uri' => $environment->getUrl(), + ]); + } + + /** + * @param \EPGService\Environments\ServiceEnvironment $environment + * @param string $channel_id + * + * @return \EPGService\Repositories\ProgramRepository + */ + public static function create(ServiceEnvironment $environment, string $channel_id): ProgramRepository { + return new ProgramRepository($environment, $channel_id); + } + + /** + * @param array $payload + * + * @return \EPGService\Entities\Program\CreditEntity[] + * + * @throws \RuntimeException + */ + private static function getCreditEntities(array $payload): array { + return array_map(static function (array $value): CreditEntity { + return CreditEntity::create($value); + }, $payload); + } + + /** + * @param array $payload + * + * @return \EPGService\Entities\ProgramEntity + * + * @throws \RuntimeException + * @throws \Exception + */ + private static function getProgramEntity(array $payload): ProgramEntity { + $data = []; + + foreach ($payload as $name => $value) { + switch ($name) { + case 'category': + $data['categories'] = $value; + break; + case 'channel': + $data['channel_id'] = $value; + break; + case 'country': + $data['countries'] = $value; + break; + case 'credits': + $data['credits'] = self::getCreditEntities($value); + break; + case 'date': + $data['date'] = new DateTime($value); + break; + case 'desc': + $data['description'] = $value; + break; + case 'icon': + $data['icons'] = $value; + break; + case 'pg': + $data['parents_guide'] = $value; + break; + case 'production': + $data['productions'] = $value; + break; + case 'start': + $data['start'] = new DateTime($value); + break; + case 'stop': + $data['stop'] = new DateTime($value); + break; + case 'sub_title': + $data['sub_title'] = $value; + break; + case 'title': + $data['title'] = $value; + break; + case 'year': + $data['year'] = $value; + break; + default: + trigger_error('blah-blah-blah', E_USER_WARNING); + break; + } + } + + return ProgramEntity::create($data); + } + + /** + * @return array + * + * @throws \GuzzleHttp\Exception\GuzzleException + * @throws \RuntimeException + * @throws \Exception + */ + public function get(): array { + $uri = sprintf(self::METHOD, $this->channel_id); + $response = $this->client->get($uri); + + $content = $response->getBody()->getContents(); + $json = json_decode($content, true, 512, JSON_THROW_ON_ERROR); + + if (!is_array($json) || !is_array($json['programms'])) { + throw new RuntimeException('blah-blah-blah'); + } + + $result = []; + + foreach ($json['programms'] as $program) { + $result[] = self::getProgramEntity($program); + } + + return $result; + } +} -- cgit v1.2.3 From 131be3511360ba4083caf72d40c47d3f6d5c473f Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sat, 18 Jul 2020 16:00:14 +0400 Subject: Added week of program Signed-off-by: Valentin Popov --- src/Repositories/ProgramRepository.php | 22 ++++++++++++++++++---- tests/Repositories/ProgramRepositoryTest.php | 3 ++- 2 files changed, 20 insertions(+), 5 deletions(-) (limited to 'src/Repositories/ProgramRepository.php') diff --git a/src/Repositories/ProgramRepository.php b/src/Repositories/ProgramRepository.php index 494b957..56884da 100644 --- a/src/Repositories/ProgramRepository.php +++ b/src/Repositories/ProgramRepository.php @@ -24,6 +24,7 @@ use EPGService\Entities\Program\CreditEntity; use EPGService\Entities\ProgramEntity; use EPGService\Environments\ServiceEnvironment; use GuzzleHttp\Client; +use GuzzleHttp\RequestOptions; use RuntimeException; use function is_array; @@ -48,12 +49,19 @@ final class ProgramRepository implements BaseRepository { */ private Client $client; + /** + * @var string + */ + private string $week; + /** * @param \EPGService\Environments\ServiceEnvironment $environment * @param string $channel_id + * @param string $week */ - private function __construct(ServiceEnvironment $environment, string $channel_id) { + private function __construct(ServiceEnvironment $environment, string $channel_id, string $week) { $this->channel_id = $channel_id; + $this->week = $week; $this->client = new Client([ 'base_uri' => $environment->getUrl(), @@ -63,11 +71,12 @@ final class ProgramRepository implements BaseRepository { /** * @param \EPGService\Environments\ServiceEnvironment $environment * @param string $channel_id + * @param string $week * * @return \EPGService\Repositories\ProgramRepository */ - public static function create(ServiceEnvironment $environment, string $channel_id): ProgramRepository { - return new ProgramRepository($environment, $channel_id); + public static function create(ServiceEnvironment $environment, string $channel_id, string $week): ProgramRepository { + return new ProgramRepository($environment, $channel_id, $week); } /** @@ -156,7 +165,12 @@ final class ProgramRepository implements BaseRepository { */ public function get(): array { $uri = sprintf(self::METHOD, $this->channel_id); - $response = $this->client->get($uri); + + $response = $this->client->get($uri, [ + RequestOptions::QUERY => [ + 'week' => $this->week, + ], + ]); $content = $response->getBody()->getContents(); $json = json_decode($content, true, 512, JSON_THROW_ON_ERROR); diff --git a/tests/Repositories/ProgramRepositoryTest.php b/tests/Repositories/ProgramRepositoryTest.php index f8a32d7..f943bb6 100644 --- a/tests/Repositories/ProgramRepositoryTest.php +++ b/tests/Repositories/ProgramRepositoryTest.php @@ -36,11 +36,12 @@ final class ProgramRepositoryTest extends TestCase { * @throws \Exception */ public function testGetAction(): void { + $week = date('Y-m-d', strtotime('this week')); $env = GetServiceEnvironment::get(); $channels = ChannelRepository::create($env)->get(); $channel = array_shift($channels); - foreach (ProgramRepository::create($env, $channel->id)->get() as $program) { + foreach (ProgramRepository::create($env, $channel->id, $week)->get() as $program) { /** @var \EPGService\Entities\ProgramEntity $program */ self::assertNotEmpty($program->channel_id); -- cgit v1.2.3