Skip to content

Commit

Permalink
Breaking changes: Remove object properties from the Object Manager an…
Browse files Browse the repository at this point in the history
…d change signatures

- Remove `$dataProvider` property and return an instance from the container on each call to the getter
- Remove `$authenticationProvider` property and return an instance from the container on each call to the getter
- Remove `$accessController` property and return an instance from the container on each call to the getter
- Change `getDataProvider()` to take a Request instance as argument
- Change `getAuthenticationProvider()` to take a Request instance as argument
- Change `getAccessController()` to take a Request instance as argument
- Change `getHandler()` to take a Request instance as argument
- Change `CrudHandler::getDataProvider()` to require the Request as second argument
  • Loading branch information
cundd committed May 11, 2019
1 parent 16d1ffb commit fab4139
Show file tree
Hide file tree
Showing 11 changed files with 378 additions and 232 deletions.
15 changes: 8 additions & 7 deletions Classes/Access/AbstractAccessController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@
use Cundd\Rest\Configuration\Access;
use Cundd\Rest\Http\RestRequestInterface;
use Cundd\Rest\Log\LoggerInterface;
use Cundd\Rest\ObjectManager;
use Cundd\Rest\ObjectManagerInterface;
use Exception;

abstract class AbstractAccessController implements AccessControllerInterface
{
/**
* @var ObjectManager
* @var ObjectManagerInterface
*/
protected $objectManager;

/**
* AbstractAccessController constructor
*
* @param ObjectManager $objectManager
* @param ObjectManagerInterface $objectManager
*/
public function __construct(ObjectManager $objectManager)
public function __construct(ObjectManagerInterface $objectManager)
{
$this->objectManager = $objectManager;
}
Expand All @@ -30,13 +31,13 @@ public function __construct(ObjectManager $objectManager)
*
* @param RestRequestInterface $request
* @return Access
* @throws \Exception
* @throws Exception
*/
protected function checkAuthentication(RestRequestInterface $request): Access
{
try {
$isAuthenticated = $this->objectManager->getAuthenticationProvider()->authenticate($request);
} catch (\Exception $exception) {
$isAuthenticated = $this->objectManager->getAuthenticationProvider($request)->authenticate($request);
} catch (Exception $exception) {
$this->objectManager->get(LoggerInterface::class)->logException($exception);
throw $exception;
}
Expand Down
20 changes: 12 additions & 8 deletions Classes/Access/ConfigurationBasedAccessController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

namespace Cundd\Rest\Access;

use Cundd\Rest\Access\Exception\InvalidConfigurationException;
use Cundd\Rest\Configuration\Access;
use Cundd\Rest\Configuration\ConfigurationProviderInterface;
use Cundd\Rest\Configuration\ResourceConfiguration;
use Cundd\Rest\Domain\Model\ResourceType;
use Cundd\Rest\Http\RestRequestInterface;
use Cundd\Rest\ObjectManager;
use Cundd\Rest\ObjectManagerInterface;
use OutOfBoundsException;

/**
* The class determines the access for the current request
Expand All @@ -21,26 +23,28 @@ class ConfigurationBasedAccessController extends AbstractAccessController
const ACCESS_NOT_REQUIRED = ['OPTIONS'];

/**
* @var \Cundd\Rest\Configuration\TypoScriptConfigurationProvider
* @var ConfigurationProviderInterface
*/
protected $configurationProvider;

/**
* ConfigurationBasedAccessController constructor
*
* @param ConfigurationProviderInterface $configurationProvider
* @param ObjectManager $objectManager
* @param ObjectManagerInterface $objectManager
*/
public function __construct(ConfigurationProviderInterface $configurationProvider, ObjectManager $objectManager)
{
public function __construct(
ConfigurationProviderInterface $configurationProvider,
ObjectManagerInterface $objectManager
) {
parent::__construct($objectManager);
$this->configurationProvider = $configurationProvider;
}

/**
* @param RestRequestInterface $request
* @return Access
* @throws \Exception
* @throws InvalidConfigurationException
*/
public function getAccess(RestRequestInterface $request): Access
{
Expand Down Expand Up @@ -68,7 +72,7 @@ public function getConfigurationForResourceType(ResourceType $resourceType)
*
* @param RestRequestInterface $request
* @return bool
* @throws Exception\InvalidConfigurationException
* @throws InvalidConfigurationException
*/
public function requestNeedsAuthentication(RestRequestInterface $request): bool
{
Expand Down Expand Up @@ -109,7 +113,7 @@ protected function getAccessConfiguration(RestRequestInterface $request): Access
return $configuration->getRead();

default:
throw new \OutOfBoundsException('Request is neither write, read, nor a preflight request');
throw new OutOfBoundsException('Request is neither write, read, nor a preflight request');
}
}
}
7 changes: 2 additions & 5 deletions Classes/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ public function __construct(
public function processRequest(ServerRequestInterface $request): ResponseInterface
{
$this->requestFactory->registerCurrentRequest($request);
if (method_exists($this->objectManager, 'reassignRequest')) {
$this->objectManager->reassignRequest();
}

return $this->dispatch($this->requestFactory->getRequest());
}
Expand Down Expand Up @@ -155,7 +152,7 @@ private function callHandler(RestRequestInterface $request)
$this->logger->logRequest(sprintf('path: "%s" method: "%s"', $requestPath, $request->getMethod()));

// If a path is given let the handler build up the routes
$this->objectManager->getHandler()->configureRoutes($resultConverter, $request);
$this->objectManager->getHandler($request)->configureRoutes($resultConverter, $request);

ErrorHandler::registerHandler();

Expand Down Expand Up @@ -256,7 +253,7 @@ private function dispatchInternal(RestRequestInterface $request): ResponseInterf
}

// Checks if the request needs authentication
$access = $this->objectManager->getAccessController()->getAccess($request);
$access = $this->objectManager->getAccessController($request)->getAccess($request);
switch (true) {
case $access->isAllowed():
case $access->isAuthorized():
Expand Down
42 changes: 21 additions & 21 deletions Classes/Handler/CrudHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

use Cundd\Rest\DataProvider\DataProviderInterface;
use Cundd\Rest\DataProvider\Utility;
use Cundd\Rest\Domain\Model\ResourceType;
use Cundd\Rest\Http\RestRequestInterface;
use Cundd\Rest\Log\LoggerInterface;
use Cundd\Rest\ObjectManagerInterface;
use Cundd\Rest\ResponseFactoryInterface;
use Cundd\Rest\Router\Route;
use Cundd\Rest\Router\RouterInterface;
use Exception;
use Iterator;
use IteratorAggregate;
use LimitIterator;

/**
Expand All @@ -22,12 +24,12 @@ class CrudHandler implements CrudHandlerInterface, HandlerDescriptionInterface
/**
* Object Manager
*
* @var \Cundd\Rest\ObjectManagerInterface
* @var ObjectManagerInterface
*/
protected $objectManager;

/**
* @var \Cundd\Rest\ResponseFactoryInterface
* @var ResponseFactoryInterface
*/
protected $responseFactory;

Expand Down Expand Up @@ -61,7 +63,7 @@ public function getDescription()
public function getProperty(RestRequestInterface $request, string $identifier, string $propertyKey)
{
$resourceType = $request->getResourceType();
$dataProvider = $this->getDataProvider($resourceType);
$dataProvider = $this->getDataProvider($request);
$model = $dataProvider->fetchModel($identifier, $resourceType);
if (!$model) {
return $this->responseFactory->createErrorResponse(null, 404, $request);
Expand All @@ -73,7 +75,7 @@ public function getProperty(RestRequestInterface $request, string $identifier, s
public function show(RestRequestInterface $request, string $identifier)
{
$resourceType = $request->getResourceType();
$dataProvider = $this->getDataProvider($resourceType);
$dataProvider = $this->getDataProvider($request);
$model = $dataProvider->fetchModel($identifier, $resourceType);
if (!$model) {
return $this->responseFactory->createErrorResponse(null, 404, $request);
Expand All @@ -93,15 +95,15 @@ public function create(RestRequestInterface $request)
}

$resourceType = $request->getResourceType();
$dataProvider = $this->getDataProvider($resourceType);
$dataProvider = $this->getDataProvider($request);
$model = $dataProvider->createModel($data, $resourceType);
if (!$model) {
return $this->responseFactory->createErrorResponse(
'Could not create model from data',
400,
$request
);
} elseif ($model instanceof \Exception) {
} elseif ($model instanceof Exception) {
return $this->responseFactory->createErrorResponse($model->getMessage(), 400, $request);
}

Expand All @@ -114,7 +116,7 @@ public function create(RestRequestInterface $request)
public function update(RestRequestInterface $request, string $identifier)
{
$resourceType = $request->getResourceType();
$dataProvider = $this->getDataProvider($resourceType);
$dataProvider = $this->getDataProvider($request);

$data = $request->getSentData();
$data['__identity'] = $identifier;
Expand All @@ -133,7 +135,7 @@ public function update(RestRequestInterface $request, string $identifier)
400,
$request
);
} elseif ($model instanceof \Exception) {
} elseif ($model instanceof Exception) {
return $this->responseFactory->createErrorResponse($model->getMessage(), 400, $request);
}

Expand All @@ -146,7 +148,7 @@ public function update(RestRequestInterface $request, string $identifier)
public function delete(RestRequestInterface $request, string $identifier)
{
$resourceType = $request->getResourceType();
$dataProvider = $this->getDataProvider($resourceType);
$dataProvider = $this->getDataProvider($request);
$this->logger->logRequest('delete request', ['identifier' => $identifier]);
$model = $dataProvider->fetchModel($identifier, $resourceType);
if (!$model) {
Expand All @@ -160,7 +162,7 @@ public function delete(RestRequestInterface $request, string $identifier)
public function listAll(RestRequestInterface $request)
{
$resourceType = $request->getResourceType();
$dataProvider = $this->getDataProvider($resourceType);
$dataProvider = $this->getDataProvider($request);
$allModels = $dataProvider->fetchAllModels($resourceType);

return $this->prepareResult(
Expand All @@ -174,7 +176,7 @@ public function countAll(RestRequestInterface $request)
{
$resourceType = $request->getResourceType();

return $this->getDataProvider($resourceType)->countAllModels($resourceType);
return $this->getDataProvider($request)->countAllModels($resourceType);
}

/**
Expand Down Expand Up @@ -202,16 +204,14 @@ public function configureRoutes(RouterInterface $router, RestRequestInterface $r
}

/**
* Returns the Data Provider
* Return the Data Provider
*
* @param ResourceType $resourceType
* @param RestRequestInterface $request
* @return DataProviderInterface
*/
protected function getDataProvider(
/** @noinspection PhpUnusedParameterInspection */
ResourceType $resourceType
) {
return $this->objectManager->getDataProvider();
protected function getDataProvider(RestRequestInterface $request)
{
return $this->objectManager->getDataProvider($request);
}

/**
Expand Down Expand Up @@ -253,10 +253,10 @@ protected function sliceResults($models)
if (is_array($models)) {
return array_slice($models, 0, $limit, true);
}
if ($models instanceof \IteratorAggregate) {
if ($models instanceof IteratorAggregate) {
$models = $models->getIterator();
}
if ($models instanceof \Iterator) {
if ($models instanceof Iterator) {
return iterator_to_array(new LimitIterator($models, 0, $limit));
}

Expand Down
Loading

0 comments on commit fab4139

Please sign in to comment.