From 82584345f6340d83008bed2eb35b3600da925135 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Fri, 17 Jul 2020 13:57:33 +0400 Subject: Added channel repository Signed-off-by: Valentin Popov --- src/Repositories/ChannelRepository.php | 104 +++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/Repositories/ChannelRepository.php (limited to 'src/Repositories/ChannelRepository.php') diff --git a/src/Repositories/ChannelRepository.php b/src/Repositories/ChannelRepository.php new file mode 100644 index 0000000..4a400f2 --- /dev/null +++ b/src/Repositories/ChannelRepository.php @@ -0,0 +1,104 @@ + + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Repositories + */ +final class ChannelRepository implements BaseRepository { + /** + * @var string + */ + private const METHOD = 'index'; + + /** + * @var \GuzzleHttp\Client + */ + private Client $client; + + /** + * @param \EPGService\Environments\ServiceEnvironment $environment + */ + private function __construct(ServiceEnvironment $environment) { + $this->client = new Client([ + 'base_uri' => $environment->getUrl(), + ]); + } + + /** + * @param \EPGService\Environments\ServiceEnvironment $environment + * + * @return \EPGService\Repositories\ChannelRepository + */ + public static function create(ServiceEnvironment $environment): ChannelRepository { + return new ChannelRepository($environment); + } + + /** + * @return array + * + * @throws \GuzzleHttp\Exception\GuzzleException + * @throws \RuntimeException + */ + public function get(): array { + $response = $this->client->get(self::METHOD); + $content = $response->getBody()->getContents(); + $xml = simplexml_load_string($content); + + if (is_bool($xml) || !$xml->channel instanceof SimpleXMLElement) { + throw new RuntimeException('blah-blah-blah'); + } + + $result = []; + + foreach ($xml->channel as $element) { + if (!$element instanceof SimpleXMLElement) { + trigger_error('blah-blah-blah', E_USER_WARNING); + continue; + } + + $result[] = ChannelEntity::create([ + 'base_id' => StringParser::get($element->{'base-channel'}), + 'base_name' => StringParser::get($element->{'base-channel'}['id']), + 'epg_id' => StringParser::get($element['epgsrvc_id']), + 'geo_data' => StringParser::get($element->{'geo-data'}), + 'href' => StringParser::get($element->href), + 'icon' => StringParser::get($element->icon['src']), + 'id' => StringParser::get($element['id']), + 'lang' => StringParser::get($element->{'display-name'}['lang']), + 'name' => StringParser::get($element->{'base-channel'}), + 'update_at' => StringParser::get($element->update), + 'week' => StringParser::get($element->week), + ]); + } + + return $result; + } +} -- cgit v1.2.3 From 92bfdcb284477b66d0b3770bc8fbaf1c12cc0b56 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sat, 18 Jul 2020 15:42:53 +0400 Subject: Updated channel repository Signed-off-by: Valentin Popov --- src/Repositories/ChannelRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Repositories/ChannelRepository.php') diff --git a/src/Repositories/ChannelRepository.php b/src/Repositories/ChannelRepository.php index 4a400f2..44eac36 100644 --- a/src/Repositories/ChannelRepository.php +++ b/src/Repositories/ChannelRepository.php @@ -62,7 +62,7 @@ final class ChannelRepository implements BaseRepository { } /** - * @return array + * @return \EPGService\Entities\ChannelEntity[] * * @throws \GuzzleHttp\Exception\GuzzleException * @throws \RuntimeException -- cgit v1.2.3 From a52e2e198708cf3e24eda73cbbc5906593cc730a Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sat, 18 Jul 2020 15:49:06 +0400 Subject: Added DateTime of channel entity Signed-off-by: Valentin Popov --- src/Entities/ChannelEntity.php | 29 ++++++++++++++-------------- src/Repositories/ChannelRepository.php | 6 +++++- tests/Repositories/ChannelRepositoryTest.php | 3 ++- 3 files changed, 22 insertions(+), 16 deletions(-) (limited to 'src/Repositories/ChannelRepository.php') diff --git a/src/Entities/ChannelEntity.php b/src/Entities/ChannelEntity.php index 0d8d3fc..4881722 100644 --- a/src/Entities/ChannelEntity.php +++ b/src/Entities/ChannelEntity.php @@ -19,21 +19,22 @@ declare(strict_types = 1); namespace EPGService\Entities; +use DateTime; use RuntimeException; use function is_string; /** - * @property-read string $base_id - * @property-read string $base_name - * @property-read string $epg_id - * @property-read string $geo_data - * @property-read string $href - * @property-read string $icon - * @property-read string $id - * @property-read string $lang - * @property-read string $name - * @property-read string $update_at - * @property-read string $week + * @property-read string $base_id + * @property-read string $base_name + * @property-read string $epg_id + * @property-read string $geo_data + * @property-read string $href + * @property-read string $icon + * @property-read string $id + * @property-read string $lang + * @property-read string $name + * @property-read \DateTime $update_at + * @property-read string $week * * @copyright Copyright © 2020 “Valentin Popov” * @license http://www.apache.org/licenses/LICENSE-2.0 @@ -86,9 +87,9 @@ final class ChannelEntity { private string $name; /** - * @var string + * @var \DateTime */ - private string $update_at; + private DateTime $update_at; /** * @var string @@ -156,7 +157,7 @@ final class ChannelEntity { throw new RuntimeException('blah-blah-blah'); } - if (!is_string($payload['update_at'])) { + if (!$payload['update_at'] instanceof DateTime) { throw new RuntimeException('blah-blah-blah'); } diff --git a/src/Repositories/ChannelRepository.php b/src/Repositories/ChannelRepository.php index 44eac36..772e4f9 100644 --- a/src/Repositories/ChannelRepository.php +++ b/src/Repositories/ChannelRepository.php @@ -19,6 +19,7 @@ declare(strict_types = 1); namespace EPGService\Repositories; +use DateTime; use EPGService\Entities\ChannelEntity; use EPGService\Environments\ServiceEnvironment; use EPGService\Parsers\StringParser; @@ -66,6 +67,7 @@ final class ChannelRepository implements BaseRepository { * * @throws \GuzzleHttp\Exception\GuzzleException * @throws \RuntimeException + * @throws \Exception */ public function get(): array { $response = $this->client->get(self::METHOD); @@ -84,6 +86,8 @@ final class ChannelRepository implements BaseRepository { continue; } + $update = StringParser::get($element->update); + $result[] = ChannelEntity::create([ 'base_id' => StringParser::get($element->{'base-channel'}), 'base_name' => StringParser::get($element->{'base-channel'}['id']), @@ -94,7 +98,7 @@ final class ChannelRepository implements BaseRepository { 'id' => StringParser::get($element['id']), 'lang' => StringParser::get($element->{'display-name'}['lang']), 'name' => StringParser::get($element->{'base-channel'}), - 'update_at' => StringParser::get($element->update), + 'update_at' => new DateTime($update), 'week' => StringParser::get($element->week), ]); } diff --git a/tests/Repositories/ChannelRepositoryTest.php b/tests/Repositories/ChannelRepositoryTest.php index 2843a8b..eea6511 100644 --- a/tests/Repositories/ChannelRepositoryTest.php +++ b/tests/Repositories/ChannelRepositoryTest.php @@ -19,6 +19,7 @@ declare(strict_types = 1); namespace Tests\Repositories; +use DateTime; use EPGService\Repositories\ChannelRepository; use PHPUnit\Framework\TestCase; use Tests\Utilities\GetServiceEnvironment; @@ -44,8 +45,8 @@ final class ChannelRepositoryTest extends TestCase { self::assertIsString($channel->id); self::assertIsString($channel->lang); self::assertIsString($channel->name); - self::assertIsString($channel->update_at); self::assertIsString($channel->week); + self::isInstanceOf(DateTime::class, $channel->update_at); } } } -- cgit v1.2.3