OXDEV-3559 Refactor to best practices
This commit is contained in:
parent
1588b5bc61
commit
3dc41fd0fe
@ -1,13 +1,14 @@
|
|||||||
services:
|
services:
|
||||||
|
|
||||||
_defaults:
|
_defaults:
|
||||||
public: false
|
public: false
|
||||||
autowire: true
|
autowire: true
|
||||||
|
|
||||||
__Vendor__\GraphQL\__Package__\Controller\:
|
__Vendor__\GraphQL\__Package__\:
|
||||||
resource: 'src/Controller/*'
|
resource: 'src/*'
|
||||||
public: true
|
exclude: 'src/**/DataType'
|
||||||
|
public: true
|
||||||
|
|
||||||
__Vendor__\GraphQL\__Package__\Framework\NamespaceMapper:
|
__Vendor__\GraphQL\__Package__\Shared\Service\NamespaceMapper:
|
||||||
class: __Vendor__\GraphQL\__Package__\Framework\NamespaceMapper
|
class: __Vendor__\GraphQL\__Package__\Shared\Service\NamespaceMapper
|
||||||
tags: ['graphql_namespace_mapper']
|
tags: ['graphql_namespace_mapper']
|
||||||
|
34
src/Category/Controller/Category.php
Normal file
34
src/Category/Controller/Category.php
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* All rights reserved.
|
||||||
|
* See LICENSE file for license details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace __Vendor__\GraphQL\__Package__\Category\Controller;
|
||||||
|
|
||||||
|
use __Vendor__\GraphQL\__Package__\Category\DataType\Category as CategoryDataType;
|
||||||
|
use __Vendor__\GraphQL\__Package__\Category\Service\Category as CategoryService;
|
||||||
|
use TheCodingMachine\GraphQLite\Annotations\Query;
|
||||||
|
|
||||||
|
final class Category
|
||||||
|
{
|
||||||
|
/** @var CategoryService */
|
||||||
|
private $categoryService;
|
||||||
|
|
||||||
|
public function __construct (
|
||||||
|
CategoryService $categoryService
|
||||||
|
) {
|
||||||
|
$this->categoryService = $categoryService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Query()
|
||||||
|
*/
|
||||||
|
public function category(string $id): CategoryDataType
|
||||||
|
{
|
||||||
|
return $this->categoryService->category($id);
|
||||||
|
}
|
||||||
|
}
|
76
src/Category/DataType/Category.php
Normal file
76
src/Category/DataType/Category.php
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* All rights reserved.
|
||||||
|
* See LICENSE file for license details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace __Vendor__\GraphQL\__Package__\Category\DataType;
|
||||||
|
|
||||||
|
use OxidEsales\Eshop\Application\Model\Category as CategoryEshopModel;
|
||||||
|
use TheCodingMachine\GraphQLite\Annotations\Field;
|
||||||
|
use TheCodingMachine\GraphQLite\Annotations\Type;
|
||||||
|
use TheCodingMachine\GraphQLite\Types\ID;
|
||||||
|
use DateTimeInterface;
|
||||||
|
use DateTimeImmutable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Type()
|
||||||
|
*/
|
||||||
|
final class Category
|
||||||
|
{
|
||||||
|
/** @var CategoryEshopModel */
|
||||||
|
private $category;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
CategoryEshopModel $category
|
||||||
|
) {
|
||||||
|
$this->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;
|
||||||
|
}
|
||||||
|
}
|
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace __Vendor__\GraphQL\__Package__\Exception;
|
namespace __Vendor__\GraphQL\__Package__\Category\Exception;
|
||||||
|
|
||||||
use OxidEsales\GraphQL\Base\Exception\NotFound;
|
use OxidEsales\GraphQL\Base\Exception\NotFound;
|
||||||
|
|
34
src/Category/Infrastructure/CategoryRepository.php
Normal file
34
src/Category/Infrastructure/CategoryRepository.php
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* All rights reserved.
|
||||||
|
* See LICENSE file for license details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace __Vendor__\GraphQL\__Package__\Category\Infrastructure;
|
||||||
|
|
||||||
|
use OxidEsales\Eshop\Application\Model\Category as CategoryEshopModel;
|
||||||
|
use __Vendor__\GraphQL\__Package__\Category\DataType\Category as CategoryDataType;
|
||||||
|
use OxidEsales\GraphQL\Base\Exception\NotFound;
|
||||||
|
|
||||||
|
final class CategoryRepository
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @throws NotFound
|
||||||
|
*/
|
||||||
|
public function category(string $id): CategoryDataType
|
||||||
|
{
|
||||||
|
/** @var CategoryEshopModel */
|
||||||
|
$category = oxNew(CategoryEshopModel::class);
|
||||||
|
|
||||||
|
if (!$category->load($id)) {
|
||||||
|
throw new NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
return new CategoryDataType(
|
||||||
|
$category
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
46
src/Category/Service/Category.php
Normal file
46
src/Category/Service/Category.php
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* All rights reserved.
|
||||||
|
* See LICENSE file for license details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace __Vendor__\GraphQL\__Package__\Category\Service;
|
||||||
|
|
||||||
|
use __Vendor__\GraphQL\__Package__\Category\DataType\Category as CategoryDataType;
|
||||||
|
use __Vendor__\GraphQL\__Package__\Category\Exception\CategoryNotFound;
|
||||||
|
use __Vendor__\GraphQL\__Package__\Category\Infrastructure\CategoryRepository;
|
||||||
|
use OxidEsales\GraphQL\Base\Exception\NotFound;
|
||||||
|
use OxidEsales\GraphQL\Base\Exception\InvalidLogin;
|
||||||
|
|
||||||
|
final class Category
|
||||||
|
{
|
||||||
|
/** @var CategoryRepository */
|
||||||
|
private $categoryRepository;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
CategoryRepository $categoryRepository
|
||||||
|
) {
|
||||||
|
$this->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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,37 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* All rights reserved.
|
|
||||||
* See LICENSE file for license details.
|
|
||||||
*/
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace __Vendor__\GraphQL\__Package__\Controller;
|
|
||||||
|
|
||||||
use OxidEsales\Eshop\Application\Model\Category as CategoryEshopModel;
|
|
||||||
use __Vendor__\GraphQL\__Package__\DataObject\Category as CategoryDataObject;
|
|
||||||
use __Vendor__\GraphQL\__Package__\Exception\CategoryNotFound;
|
|
||||||
use TheCodingMachine\GraphQLite\Annotations\Query;
|
|
||||||
|
|
||||||
class Category
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* category by ID
|
|
||||||
*
|
|
||||||
* @Query()
|
|
||||||
*/
|
|
||||||
public function category(string $id): CategoryDataObject
|
|
||||||
{
|
|
||||||
/** @var CategoryEshopModel */
|
|
||||||
$category = oxNew(CategoryEshopModel::class);
|
|
||||||
|
|
||||||
if (!$category->load($id)) {
|
|
||||||
throw CategoryNotFound::byId($id);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new CategoryDataObject(
|
|
||||||
$category
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,48 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* All rights reserved.
|
|
||||||
* See LICENSE file for license details.
|
|
||||||
*/
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace __Vendor__\GraphQL\__Package__\DataObject;
|
|
||||||
|
|
||||||
use OxidEsales\Eshop\Application\Model\Category as CategoryEshopModel;
|
|
||||||
use TheCodingMachine\GraphQLite\Annotations\Field;
|
|
||||||
use TheCodingMachine\GraphQLite\Annotations\Type;
|
|
||||||
use TheCodingMachine\GraphQLite\Types\ID;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @Type()
|
|
||||||
*/
|
|
||||||
class Category
|
|
||||||
{
|
|
||||||
/** @var CategoryEshopModel */
|
|
||||||
private $category;
|
|
||||||
|
|
||||||
public function __construct(
|
|
||||||
CategoryEshopModel $category
|
|
||||||
) {
|
|
||||||
$this->category = $category;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @Field
|
|
||||||
*/
|
|
||||||
public function getId(): ID
|
|
||||||
{
|
|
||||||
return new ID(
|
|
||||||
$this->category->getId()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @Field
|
|
||||||
*/
|
|
||||||
public function getTitle(): string
|
|
||||||
{
|
|
||||||
return (string) $this->category->getFieldData('oxtitle');
|
|
||||||
}
|
|
||||||
}
|
|
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace __Vendor__\GraphQL\__Package__\Framework;
|
namespace __Vendor__\GraphQL\__Package__\Shared\Service;
|
||||||
|
|
||||||
use OxidEsales\GraphQL\Base\Framework\NamespaceMapperInterface;
|
use OxidEsales\GraphQL\Base\Framework\NamespaceMapperInterface;
|
||||||
|
|
||||||
@ -16,14 +16,14 @@ class NamespaceMapper implements NamespaceMapperInterface
|
|||||||
public function getControllerNamespaceMapping(): array
|
public function getControllerNamespaceMapping(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'\\__Vendor__\\GraphQL\\__Package__\\Controller' => __DIR__ . '/../Controller/',
|
'\\__Vendor__\\GraphQL\\__Package__\\Category\\Controller' => __DIR__ . '/../../Category/Controller/',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTypeNamespaceMapping(): array
|
public function getTypeNamespaceMapping(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'\\__Vendor__\\GraphQL\\__Package__\\DataObject' => __DIR__ . '/../DataObject/',
|
'\\__Vendor__\\GraphQL\\__Package__\\Category\\DataType' => __DIR__ . '/../../Category/DataType/',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,24 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* All rights reserved.
|
|
||||||
* See LICENSE file for license details.
|
|
||||||
*/
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace __Vendor__\GraphQL\__Package__\Tests\Integration\Controller;
|
|
||||||
|
|
||||||
use OxidEsales\GraphQL\Base\Tests\Integration\TestCase;
|
|
||||||
|
|
||||||
class CategoryTest extends TestCase
|
|
||||||
{
|
|
||||||
public function testGetSingleCategoryWithoutParam(): void
|
|
||||||
{
|
|
||||||
$result = $this->execQuery('query { category }');
|
|
||||||
$this->assertSame(
|
|
||||||
400,
|
|
||||||
$result['status']
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
@ -7,13 +7,13 @@
|
|||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace __Vendor__\GraphQL\__Package__\Tests\Unit\Framework;
|
namespace __Vendor__\GraphQL\__Package__\Tests\Unit\Shared\Service;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
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
|
class NamespaceMapperTest extends TestCase
|
||||||
{
|
{
|
Loading…
Reference in New Issue
Block a user