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