diff --git a/Controller/AbstractCrudServiceRestResourceController.php b/Controller/AbstractCrudServiceRestResourceController.php new file mode 100644 index 0000000..273bdce --- /dev/null +++ b/Controller/AbstractCrudServiceRestResourceController.php @@ -0,0 +1,123 @@ + + */ +abstract class AbstractCrudServiceRestResourceController extends AbstractRestResourceController +{ + public function __construct( + RestRequestParserInterface $requestParser, + Normalizer $normalizer, + ValidatorInterface $validator, + RequestStack $requestStack, + MetadataFactoryInterface $metadataFactory, + PropertyAccessorInterface $propertyAccessor + ) { + parent::__construct( + $requestParser, + $normalizer, + $validator, + $requestStack, + $metadataFactory, + $propertyAccessor + ); + } + + /** + * {@inheritdoc} + */ + protected function listEntities(int $page = 1, int $perPage = 50) + { + return $this->getService()->findAllPaginated($page, $perPage); + } + + /** + * {@inheritdoc} + */ + protected function fetchEntity($id) + { + $entity = $this->getService()->find($id); + if (null === $entity) { + throw new NotFoundHttpException(); + } + + return $entity; + } + + /** + * {@inheritdoc} + */ + protected function createEntity($entity) + { + return $this->getService()->create($entity); + } + + /** + * {@inheritdoc} + */ + protected function updateEntity($entity) + { + return $this->getService()->update($entity); + } + + /** + * {@inheritdoc} + */ + protected function removeEntity($entity) + { + $this->getService()->remove($entity); + } + + /** + * {@inheritdoc} + */ + protected function listSubresource($entity, string $subresource, int $page = 1, int $perPage = 50) + { + return $this->getService()->findAssociationPaginated($entity, $subresource, $page, $perPage); + } + + /** + * {@inheritdoc} + */ + protected function createAssociation($parent, string $subresource, $entity) + { + return $this->getService()->createAssociation($parent, $subresource, $entity); + } + + /** + * {@inheritdoc} + */ + protected function addAssociation($parent, string $subresource, $subId) + { + $this->getService()->addAssociation($parent, $subresource, $subId); + } + + /** + * {@inheritdoc} + */ + protected function removeAssociation($parent, string $subresource, $subId = null) + { + $this->getService()->removeAssociation($parent, $subresource, $subId); + } + + protected function getServiceId() + { + return $this->getCurrentRequest()->attributes->get('_service'); + } + + /** + * @return CrudServiceInterface + */ + abstract protected function getService(); +} diff --git a/Controller/AbstractRestResourceController.php b/Controller/AbstractRestResourceController.php index 06def76..02fb6dc 100644 --- a/Controller/AbstractRestResourceController.php +++ b/Controller/AbstractRestResourceController.php @@ -8,7 +8,7 @@ use Dontdrinkandroot\RestBundle\Metadata\ClassMetadata; use Dontdrinkandroot\RestBundle\Metadata\PropertyMetadata; use Dontdrinkandroot\RestBundle\Service\Normalizer; -use Dontdrinkandroot\RestBundle\Service\RestRequestParser; +use Dontdrinkandroot\RestBundle\Service\RestRequestParserInterface; use Metadata\MetadataFactoryInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; @@ -27,6 +27,57 @@ */ abstract class AbstractRestResourceController implements RestResourceControllerInterface { + /** + * @var Normalizer + */ + private $normalizer; + + /** + * @var ValidatorInterface + */ + private $validator; + + /** + * @var RestRequestParserInterface + */ + private $requestParser; + + /** + * @var RequestStack + */ + private $requestStack; + + /** + * @var MetadataFactoryInterface + */ + private $metadataFactory; + + /** + * @var PropertyAccessorInterface + */ + private $propertyAccessor; + + /** + * @var AuthorizationCheckerInterface + */ + private $authorizationChecker; + + public function __construct( + RestRequestParserInterface $requestParser, + Normalizer $normalizer, + ValidatorInterface $validator, + RequestStack $requestStack, + MetadataFactoryInterface $metadataFactory, + PropertyAccessorInterface $propertyAccessor + ) { + $this->requestParser = $requestParser; + $this->normalizer = $normalizer; + $this->validator = $validator; + $this->requestStack = $requestStack; + $this->metadataFactory = $metadataFactory; + $this->propertyAccessor = $propertyAccessor; + } + /** * {@inheritdoc} */ @@ -372,6 +423,70 @@ protected function addPaginationHeaders(Response $response, int $page, int $perP ); } + /** + * {@inheritdoc} + */ + protected function getNormalizer() + { + return $this->normalizer; + } + + /** + * {@inheritdoc} + */ + protected function getValidator() + { + return $this->validator; + } + + /** + * {@inheritdoc} + */ + protected function getRequestParser() + { + return $this->requestParser; + } + + /** + * {@inheritdoc} + */ + protected function getRequestStack() + { + return $this->requestStack; + } + + /** + * {@inheritdoc} + */ + protected function getMetadataFactory() + { + return $this->metadataFactory; + } + + /** + * {@inheritdoc} + */ + protected function getPropertyAccessor() + { + return $this->propertyAccessor; + } + + /** + * {@inheritdoc} + */ + protected function getAuthorizationChecker(): ?AuthorizationCheckerInterface + { + return $this->authorizationChecker; + } + + /** + * @param AuthorizationCheckerInterface $authorizationChecker + */ + public function setAuthorizationChecker(AuthorizationCheckerInterface $authorizationChecker): void + { + $this->authorizationChecker = $authorizationChecker; + } + /** * @param int $page * @param int $perPage @@ -447,39 +562,4 @@ abstract protected function addAssociation($parent, string $subresource, $subId) * @return mixed */ abstract protected function removeAssociation($parent, string $subresource, $subId = null); - - /** - * @return Normalizer - */ - abstract protected function getNormalizer(); - - /** - * @return ValidatorInterface - */ - abstract protected function getValidator(); - - /** - * @return RestRequestParser - */ - abstract protected function getRequestParser(); - - /** - * @return RequestStack - */ - abstract protected function getRequestStack(); - - /** - * @return MetadataFactoryInterface - */ - abstract protected function getMetadataFactory(); - - /** - * @return PropertyAccessorInterface - */ - abstract protected function getPropertyAccessor(); - - /** - * @return AuthorizationCheckerInterface - */ - abstract protected function getAuthorizationChecker(); } diff --git a/Controller/CrudServiceRestResourceController.php b/Controller/CrudServiceRestResourceController.php index d2b6452..14fd9c5 100644 --- a/Controller/CrudServiceRestResourceController.php +++ b/Controller/CrudServiceRestResourceController.php @@ -2,98 +2,43 @@ namespace Dontdrinkandroot\RestBundle\Controller; +use Dontdrinkandroot\RestBundle\Service\Normalizer; +use Dontdrinkandroot\RestBundle\Service\RestRequestParserInterface; use Dontdrinkandroot\Service\CrudServiceInterface; -use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Metadata\MetadataFactoryInterface; +use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\PropertyAccess\PropertyAccessorInterface; +use Symfony\Component\Validator\Validator\ValidatorInterface; -/** - * @author Philip Washington Sorst - */ -abstract class CrudServiceRestResourceController extends AbstractRestResourceController +class CrudServiceRestResourceController extends AbstractCrudServiceRestResourceController { - /** - * {@inheritdoc} - */ - protected function listEntities(int $page = 1, int $perPage = 50) - { - return $this->getService()->findAllPaginated($page, $perPage); - } - - /** - * {@inheritdoc} - */ - protected function fetchEntity($id) - { - $entity = $this->getService()->find($id); - if (null === $entity) { - throw new NotFoundHttpException(); - } - - return $entity; - } - - /** - * {@inheritdoc} - */ - protected function createEntity($entity) - { - return $this->getService()->create($entity); - } - - /** - * {@inheritdoc} - */ - protected function updateEntity($entity) - { - return $this->getService()->update($entity); - } - - /** - * {@inheritdoc} - */ - protected function removeEntity($entity) - { - $this->getService()->remove($entity); + private $service; + + public function __construct( + RestRequestParserInterface $requestParser, + Normalizer $normalizer, + ValidatorInterface $validator, + RequestStack $requestStack, + MetadataFactoryInterface $metadataFactory, + PropertyAccessorInterface $propertyAccessor, + CrudServiceInterface $service + ) { + parent::__construct( + $requestParser, + $normalizer, + $validator, + $requestStack, + $metadataFactory, + $propertyAccessor + ); + $this->service = $service; } /** - * {@inheritdoc} - */ - protected function listSubresource($entity, string $subresource, int $page = 1, int $perPage = 50) - { - return $this->getService()->findAssociationPaginated($entity, $subresource, $page, $perPage); - } - - /** - * {@inheritdoc} - */ - protected function createAssociation($parent, string $subresource, $entity) - { - return $this->getService()->createAssociation($parent, $subresource, $entity); - } - - /** - * {@inheritdoc} - */ - protected function addAssociation($parent, string $subresource, $subId) - { - $this->getService()->addAssociation($parent, $subresource, $subId); - } - - /** - * {@inheritdoc} + * @return CrudServiceInterface */ - protected function removeAssociation($parent, string $subresource, $subId = null) + protected function getService() { - $this->getService()->removeAssociation($parent, $subresource, $subId); + return $this->service; } - - protected function getServiceId() - { - return $this->getCurrentRequest()->attributes->get('_service'); - } - - /** - * @return CrudServiceInterface - */ - abstract protected function getService(); } diff --git a/Controller/DoctrineRestResourceController.php b/Controller/DoctrineRestResourceController.php index b4b6c6b..41d2ef9 100644 --- a/Controller/DoctrineRestResourceController.php +++ b/Controller/DoctrineRestResourceController.php @@ -9,69 +9,35 @@ use Metadata\MetadataFactoryInterface; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; -use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; use Symfony\Component\Validator\Validator\ValidatorInterface; /** * @author Philip Washington Sorst */ -class DoctrineRestResourceController extends CrudServiceRestResourceController +class DoctrineRestResourceController extends AbstractCrudServiceRestResourceController { - /** - * @var Normalizer - */ - private $normalizer; - - /** - * @var ValidatorInterface - */ - private $validator; - - /** - * @var RestRequestParserInterface - */ - private $requestParser; - - /** - * @var RequestStack - */ - private $requestStack; - - /** - * @var MetadataFactoryInterface - */ - private $metadataFactory; - - /** - * @var PropertyAccessorInterface - */ - private $propertyAccessor; - - /** - * @var AuthorizationCheckerInterface - */ - private $authorizationChecker; - /** * @var EntityManagerInterface */ private $entityManager; public function __construct( - EntityManagerInterface $entityManager, RestRequestParserInterface $requestParser, Normalizer $normalizer, ValidatorInterface $validator, RequestStack $requestStack, MetadataFactoryInterface $metadataFactory, - PropertyAccessorInterface $propertyAccessor + PropertyAccessorInterface $propertyAccessor, + EntityManagerInterface $entityManager ) { - $this->normalizer = $normalizer; - $this->validator = $validator; - $this->requestParser = $requestParser; - $this->requestStack = $requestStack; - $this->metadataFactory = $metadataFactory; - $this->propertyAccessor = $propertyAccessor; + parent::__construct( + $requestParser, + $normalizer, + $validator, + $requestStack, + $metadataFactory, + $propertyAccessor + ); $this->entityManager = $entityManager; } @@ -95,70 +61,6 @@ protected function getService(): CrudServiceInterface return $repository; } - /** - * {@inheritdoc} - */ - protected function getNormalizer() - { - return $this->normalizer; - } - - /** - * {@inheritdoc} - */ - protected function getValidator() - { - return $this->validator; - } - - /** - * {@inheritdoc} - */ - protected function getRequestParser() - { - return $this->requestParser; - } - - /** - * {@inheritdoc} - */ - protected function getRequestStack() - { - return $this->requestStack; - } - - /** - * {@inheritdoc} - */ - protected function getMetadataFactory() - { - return $this->metadataFactory; - } - - /** - * {@inheritdoc} - */ - protected function getPropertyAccessor() - { - return $this->propertyAccessor; - } - - /** - * {@inheritdoc} - */ - protected function getAuthorizationChecker(): ?AuthorizationCheckerInterface - { - return $this->authorizationChecker; - } - - /** - * @param AuthorizationCheckerInterface $authorizationChecker - */ - public function setAuthorizationChecker(AuthorizationCheckerInterface $authorizationChecker): void - { - $this->authorizationChecker = $authorizationChecker; - } - /** * {@inheritdoc} */ diff --git a/Resources/config/services.yml b/Resources/config/services.yml index b7b27f2..a1dca94 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -78,13 +78,13 @@ services: ddr_rest.controller.doctrine: class: Dontdrinkandroot\RestBundle\Controller\DoctrineRestResourceController arguments: - - '@doctrine.orm.entity_manager' - '@ddr_rest.parser.request' - '@ddr_rest.normalizer' - '@validator' - '@request_stack' - '@ddr_rest.metadata.factory' - '@property_accessor' + - '@doctrine.orm.entity_manager' calls: - [setAuthorizationChecker, ['@?security.authorization_checker']] public: true \ No newline at end of file