From c18b1373d03f84d58737712b57efa02e642a1e58 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 15 Jul 2020 19:09:58 +0400 Subject: Added category repository Signed-off-by: Valentin Popov --- src/Repositories/BaseRepository.php | 32 ++++++++ src/Repositories/CategoryRepository.php | 104 ++++++++++++++++++++++++++ tests/Repositories/CategoryRepositoryTest.php | 47 ++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 src/Repositories/BaseRepository.php create mode 100644 src/Repositories/CategoryRepository.php create mode 100644 tests/Repositories/CategoryRepositoryTest.php diff --git a/src/Repositories/BaseRepository.php b/src/Repositories/BaseRepository.php new file mode 100644 index 0000000..14c9061 --- /dev/null +++ b/src/Repositories/BaseRepository.php @@ -0,0 +1,32 @@ + + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Repositories + */ +interface BaseRepository { + /** + * @return mixed + */ + public function get(); +} diff --git a/src/Repositories/CategoryRepository.php b/src/Repositories/CategoryRepository.php new file mode 100644 index 0000000..6139168 --- /dev/null +++ b/src/Repositories/CategoryRepository.php @@ -0,0 +1,104 @@ + + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Repositories + */ +final class CategoryRepository implements BaseRepository { + /** + * @var string + */ + private const METHOD = 'category_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 $environment + * + * @return \EPGService\Repositories\CategoryRepository + */ + public static function create(ServiceEnvironment $environment): CategoryRepository { + return new CategoryRepository($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->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[] = CategoryEntity::create([ + 'id' => IntegerParser::get($element['id']), + 'lang' => StringParser::get($element->name['lang']), + 'name' => StringParser::get($element->name), + 'version' => StringParser::get($element['version']), + ]); + } + + return $result; + } +} diff --git a/tests/Repositories/CategoryRepositoryTest.php b/tests/Repositories/CategoryRepositoryTest.php new file mode 100644 index 0000000..f78f5b4 --- /dev/null +++ b/tests/Repositories/CategoryRepositoryTest.php @@ -0,0 +1,47 @@ + + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package Tests\Repositories + */ +final class CategoryRepositoryTest extends TestCase { + /** + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function testGetAction(): void { + $env = GetServiceEnvironment::get(); + + foreach (CategoryRepository::create($env)->get() as $category) { + /** @var \EPGService\Entities\CategoryEntity $category */ + + self::assertIsInt($category->id); + self::assertIsString($category->lang); + self::assertIsString($category->name); + self::assertIsString($category->version); + } + } +} -- cgit v1.2.3