diff --git a/services.yaml b/services.yaml index 03d9c0c..f9b6202 100644 --- a/services.yaml +++ b/services.yaml @@ -1,13 +1,14 @@ services: - _defaults: - public: false - autowire: true + _defaults: + public: false + autowire: true - __Vendor__\GraphQL\__Package__\Controller\: - resource: 'src/Controller/*' - public: true + __Vendor__\GraphQL\__Package__\: + resource: 'src/*' + exclude: 'src/**/DataType' + public: true - __Vendor__\GraphQL\__Package__\Framework\NamespaceMapper: - class: __Vendor__\GraphQL\__Package__\Framework\NamespaceMapper - tags: ['graphql_namespace_mapper'] + __Vendor__\GraphQL\__Package__\Shared\Service\NamespaceMapper: + class: __Vendor__\GraphQL\__Package__\Shared\Service\NamespaceMapper + tags: ['graphql_namespace_mapper'] diff --git a/src/Category/Controller/Category.php b/src/Category/Controller/Category.php new file mode 100644 index 0000000..2bd7fc0 --- /dev/null +++ b/src/Category/Controller/Category.php @@ -0,0 +1,34 @@ +categoryService = $categoryService; + } + + /** + * @Query() + */ + public function category(string $id): CategoryDataType + { + return $this->categoryService->category($id); + } +} diff --git a/src/Category/DataType/Category.php b/src/Category/DataType/Category.php new file mode 100644 index 0000000..efab180 --- /dev/null +++ b/src/Category/DataType/Category.php @@ -0,0 +1,76 @@ +category = $category; + } + + /** + * @Field + */ + public function id(): ID + { + return new ID( + $this->category->getId() + ); + } + + /** + * @Field + */ + public function title(): string + { + return (string) $this->category->getFieldData('oxtitle'); + } + + /** + * @Field() + */ + public function active(?DateTimeInterface $now = null): bool + { + $active = (bool) $this->category->getFieldData('oxactive'); + + if ($active) { + return true; + } + + $from = new DateTimeImmutable( + (string) $this->category->getFieldData('oxactivefrom') + ); + $to = new DateTimeImmutable( + (string) $this->category->getFieldData('oxactiveto') + ); + $now = $now ?? new DateTimeImmutable('now'); + + if ($from <= $now && $to >= $now) { + return true; + } + + return false; + } +} diff --git a/src/Exception/CategoryNotFound.php b/src/Category/Exception/CategoryNotFound.php similarity index 85% rename from src/Exception/CategoryNotFound.php rename to src/Category/Exception/CategoryNotFound.php index e0fb416..ef27dc2 100644 --- a/src/Exception/CategoryNotFound.php +++ b/src/Category/Exception/CategoryNotFound.php @@ -7,7 +7,7 @@ declare(strict_types=1); -namespace __Vendor__\GraphQL\__Package__\Exception; +namespace __Vendor__\GraphQL\__Package__\Category\Exception; use OxidEsales\GraphQL\Base\Exception\NotFound; diff --git a/src/Category/Infrastructure/CategoryRepository.php b/src/Category/Infrastructure/CategoryRepository.php new file mode 100644 index 0000000..63d28f0 --- /dev/null +++ b/src/Category/Infrastructure/CategoryRepository.php @@ -0,0 +1,34 @@ +load($id)) { + throw new NotFound(); + } + + return new CategoryDataType( + $category + ); + } +} diff --git a/src/Category/Service/Category.php b/src/Category/Service/Category.php new file mode 100644 index 0000000..926afb8 --- /dev/null +++ b/src/Category/Service/Category.php @@ -0,0 +1,46 @@ +categoryRepository = $categoryRepository; + } + + /** + * @throws CategoryNotFound + */ + public function category(string $id): CategoryDataType + { + try { + $category = $this->categoryRepository->category($id); + } catch (NotFound $e) { + throw CategoryNotFound::byId($id); + } + + if (!$category->active()) { + throw new InvalidLogin('Unauthorized'); + } + + return $category; + } +} diff --git a/src/Controller/Category.php b/src/Controller/Category.php deleted file mode 100644 index 4520b36..0000000 --- a/src/Controller/Category.php +++ /dev/null @@ -1,37 +0,0 @@ -load($id)) { - throw CategoryNotFound::byId($id); - } - - return new CategoryDataObject( - $category - ); - } -} diff --git a/src/DataObject/Category.php b/src/DataObject/Category.php deleted file mode 100644 index 5e10694..0000000 --- a/src/DataObject/Category.php +++ /dev/null @@ -1,48 +0,0 @@ -category = $category; - } - - /** - * @Field - */ - public function getId(): ID - { - return new ID( - $this->category->getId() - ); - } - - /** - * @Field - */ - public function getTitle(): string - { - return (string) $this->category->getFieldData('oxtitle'); - } -} diff --git a/src/Framework/NamespaceMapper.php b/src/Shared/Service/NamespaceMapper.php similarity index 60% rename from src/Framework/NamespaceMapper.php rename to src/Shared/Service/NamespaceMapper.php index dff008c..08624f7 100644 --- a/src/Framework/NamespaceMapper.php +++ b/src/Shared/Service/NamespaceMapper.php @@ -7,7 +7,7 @@ declare(strict_types=1); -namespace __Vendor__\GraphQL\__Package__\Framework; +namespace __Vendor__\GraphQL\__Package__\Shared\Service; use OxidEsales\GraphQL\Base\Framework\NamespaceMapperInterface; @@ -16,14 +16,14 @@ class NamespaceMapper implements NamespaceMapperInterface public function getControllerNamespaceMapping(): array { return [ - '\\__Vendor__\\GraphQL\\__Package__\\Controller' => __DIR__ . '/../Controller/', + '\\__Vendor__\\GraphQL\\__Package__\\Category\\Controller' => __DIR__ . '/../../Category/Controller/', ]; } public function getTypeNamespaceMapping(): array { return [ - '\\__Vendor__\\GraphQL\\__Package__\\DataObject' => __DIR__ . '/../DataObject/', + '\\__Vendor__\\GraphQL\\__Package__\\Category\\DataType' => __DIR__ . '/../../Category/DataType/', ]; } } diff --git a/tests/Integration/Controller/CategoryTest.php b/tests/Integration/Controller/CategoryTest.php deleted file mode 100644 index 3aea809..0000000 --- a/tests/Integration/Controller/CategoryTest.php +++ /dev/null @@ -1,24 +0,0 @@ -execQuery('query { category }'); - $this->assertSame( - 400, - $result['status'] - ); - } -} diff --git a/tests/Unit/Framework/NamespaceMapperTest.php b/tests/Unit/Shared/Service/NamespaceMapperTest.php similarity index 71% rename from tests/Unit/Framework/NamespaceMapperTest.php rename to tests/Unit/Shared/Service/NamespaceMapperTest.php index 3484b8a..f8f592c 100644 --- a/tests/Unit/Framework/NamespaceMapperTest.php +++ b/tests/Unit/Shared/Service/NamespaceMapperTest.php @@ -7,13 +7,13 @@ declare(strict_types=1); -namespace __Vendor__\GraphQL\__Package__\Tests\Unit\Framework; +namespace __Vendor__\GraphQL\__Package__\Tests\Unit\Shared\Service; use PHPUnit\Framework\TestCase; -use __Vendor__\GraphQL\__Package__\Framework\NamespaceMapper; +use __Vendor__\GraphQL\__Package__\Shared\Service\NamespaceMapper; /** - * @covers __Vendor__\GraphQL\__Package__\Framework\NamespaceMapper + * @covers __Vendor__\GraphQL\__Package__\Shared\Service\NamespaceMapper */ class NamespaceMapperTest extends TestCase {