Skip to content

Commit

Permalink
Refactoring Controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
philipsorst committed Dec 30, 2017
1 parent 56c59d2 commit 5628a96
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 231 deletions.
123 changes: 123 additions & 0 deletions Controller/AbstractCrudServiceRestResourceController.php
@@ -0,0 +1,123 @@
<?php

namespace Dontdrinkandroot\RestBundle\Controller;

use Dontdrinkandroot\RestBundle\Service\Normalizer;
use Dontdrinkandroot\RestBundle\Service\RestRequestParserInterface;
use Dontdrinkandroot\Service\CrudServiceInterface;
use Metadata\MetadataFactoryInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

/**
* @author Philip Washington Sorst <philip@sorst.net>
*/
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();
}
152 changes: 116 additions & 36 deletions Controller/AbstractRestResourceController.php
Expand Up @@ -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;
Expand All @@ -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}
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}

0 comments on commit 5628a96

Please sign in to comment.