From 721e01511aca0df7a8fdee421ea8d65a5879684b Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 15 Jul 2020 19:27:57 +0400 Subject: Added genre repository Signed-off-by: Valentin Popov --- src/Repositories/GenreRepository.php | 103 +++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/Repositories/GenreRepository.php (limited to 'src/Repositories/GenreRepository.php') diff --git a/src/Repositories/GenreRepository.php b/src/Repositories/GenreRepository.php new file mode 100644 index 0000000..67c9a4e --- /dev/null +++ b/src/Repositories/GenreRepository.php @@ -0,0 +1,103 @@ + + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Repositories + */ +final class GenreRepository implements BaseRepository { + /** + * @var string + */ + private const METHOD = 'genre_list'; + + /** + * @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 $env + * + * @return \EPGService\Repositories\GenreRepository + */ + public static function create(ServiceEnvironment $env): GenreRepository { + return new GenreRepository($env); + } + + /** + * @return \EPGService\Entities\GenreEntity[] + * + * @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->element instanceof SimpleXMLElement) { + throw new RuntimeException('blah-blah-blah'); + } + + $result = []; + + foreach ($xml->element as $element) { + if (!$element instanceof SimpleXMLElement) { + trigger_error('blah-blah-blah', E_USER_WARNING); + continue; + } + + if (!$element->name instanceof SimpleXMLElement) { + trigger_error('blah-blah-blah', E_USER_WARNING); + continue; + } + + $result[] = GenreEntity::create([ + 'id' => IntegerParser::get($element['id']), + 'lang' => StringParser::get($element->name['lang']), + 'name' => StringParser::get($element->name), + 'version' => StringParser::get($element['version']), + ]); + } + + return $result; + } +} -- cgit v1.2.3