Skip to content

Commit

Permalink
Replace some @inject annotations with constructor or method injection
Browse files Browse the repository at this point in the history
  • Loading branch information
cundd committed Aug 12, 2019
1 parent f6c26bf commit 94780af
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 84 deletions.
13 changes: 11 additions & 2 deletions Classes/Authentication/BasicAuthenticationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@ class BasicAuthenticationProvider extends AbstractAuthenticationProvider
/**
* Provider that will check the user credentials
*
* @var \Cundd\Rest\Authentication\UserProviderInterface
* @inject
* @var UserProviderInterface
*/
protected $userProvider;

/**
* BasicAuth Provider constructor
*
* @param UserProviderInterface $userProvider
*/
public function __construct(UserProviderInterface $userProvider)
{
$this->userProvider = $userProvider;
}

/**
* Tries to authenticate the current request
*
Expand Down
14 changes: 12 additions & 2 deletions Classes/Authentication/CredentialsAuthenticationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,28 @@

use Cundd\Rest\Handler\AuthHandler;
use Cundd\Rest\Http\RestRequestInterface;
use Cundd\Rest\SessionManager;

/**
* Authentication Provider for requests authenticated through the login route (/auth/login)
*/
class CredentialsAuthenticationProvider extends AbstractAuthenticationProvider
{
/**
* @var \Cundd\Rest\SessionManager
* @inject
* @var SessionManager
*/
protected $sessionManager;

/**
* Credentials Authentication Provider constructor
*
* @param SessionManager $sessionManager
*/
public function __construct(SessionManager $sessionManager)
{
$this->sessionManager = $sessionManager;
}

/**
* Tries to authenticate the current request
*
Expand Down
8 changes: 6 additions & 2 deletions Classes/Configuration/TypoScriptConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
class TypoScriptConfigurationProvider extends AbstractConfigurationProvider
{
/**
* @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
* @inject
* @var ConfigurationManager
*/
protected $configurationManager;

public function injectConfigurationManager(ConfigurationManager $configurationManager)
{
$this->configurationManager = $configurationManager;
}

/**
* Returns the settings read from the TypoScript
*
Expand Down
43 changes: 30 additions & 13 deletions Classes/DataProvider/VirtualObjectDataProvider.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<?php
declare(strict_types=1);


namespace Cundd\Rest\DataProvider;

use Cundd\Rest\Domain\Model\ResourceType;
use Cundd\Rest\ObjectManagerInterface;
use Cundd\Rest\VirtualObject\ConfigurationFactory;
use Cundd\Rest\VirtualObject\ConfigurationInterface;
use Cundd\Rest\VirtualObject\Exception\InvalidPropertyException;
use Cundd\Rest\VirtualObject\Exception\MissingConfigurationException;
use Cundd\Rest\VirtualObject\ObjectConverter;
use Cundd\Rest\VirtualObject\Persistence\Repository;
use Cundd\Rest\VirtualObject\Persistence\RepositoryInterface;
use Cundd\Rest\VirtualObject\VirtualObject;
use Psr\Log\LoggerInterface;

/**
* Data Provider for Virtual Objects
Expand All @@ -24,11 +26,30 @@ class VirtualObjectDataProvider extends DataProvider
protected $objectConverterMap = [];

/**
* @var \Cundd\Rest\VirtualObject\ConfigurationFactory
* @inject
* @var ConfigurationFactory
*/
protected $configurationFactory;

/**
* VirtualObjectDataProvider constructor.
*
* @param ConfigurationFactory $configurationFactory
* @param ObjectManagerInterface $objectManager
* @param ExtractorInterface $extractor
* @param IdentityProviderInterface $identityProvider
* @param LoggerInterface|null $logger
*/
public function __construct(
ConfigurationFactory $configurationFactory,
ObjectManagerInterface $objectManager,
ExtractorInterface $extractor,
IdentityProviderInterface $identityProvider,
LoggerInterface $logger = null
) {
parent::__construct($objectManager, $extractor, $identityProvider, $logger);
$this->configurationFactory = $configurationFactory;
}

/**
* Return the Object Converter with the matching configuration
*
Expand All @@ -54,10 +75,10 @@ public function getObjectConverterForResourceType(ResourceType $resourceType)
* Return the Configuration for the given resource type
*
* @param ResourceType $resourceType
* @throws \Cundd\Rest\VirtualObject\Exception\MissingConfigurationException
* @return ConfigurationInterface
* @throws MissingConfigurationException
*/
public function getConfigurationForResourceType(ResourceType $resourceType)
public function getConfigurationForResourceType(ResourceType $resourceType): ?ConfigurationInterface
{
$resourceTypeString = (string)$resourceType;
$virtualResourceTypeString = substr(
Expand All @@ -68,13 +89,9 @@ public function getConfigurationForResourceType(ResourceType $resourceType)
throw new MissingConfigurationException('Could not get configuration for empty resource type', 1395932408);
}

try {
return $this->configurationFactory->createFromTypoScriptForResourceType(
new ResourceType($virtualResourceTypeString)
);
} catch (MissingConfigurationException $exception) {
return null;
}
return $this->configurationFactory->createFromTypoScriptForResourceType(
new ResourceType($virtualResourceTypeString)
);
}

public function createModel(array $data, ResourceType $resourceType)
Expand All @@ -95,7 +112,7 @@ public function getRepositoryClassForResourceType(ResourceType $resourceType): s
public function getRepositoryForResourceType(ResourceType $resourceType)
{
$repositoryClass = $this->getRepositoryClassForResourceType($resourceType);
/** @var \Cundd\Rest\VirtualObject\Persistence\RepositoryInterface|\TYPO3\CMS\Extbase\Persistence\RepositoryInterface $repository */
/** @var RepositoryInterface|\TYPO3\CMS\Extbase\Persistence\RepositoryInterface $repository */
$repository = $this->objectManager->get($repositoryClass);
$repository->setConfiguration($this->getConfigurationForResourceType($resourceType));

Expand Down
46 changes: 32 additions & 14 deletions Classes/Handler/AuthHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

namespace Cundd\Rest\Handler;

use Cundd\Rest\Authentication\UserProviderInterface;
use Cundd\Rest\Http\RestRequestInterface;
use Cundd\Rest\RequestFactoryInterface;
use Cundd\Rest\Router\RouterInterface;
use Cundd\Rest\SessionManager;

/**
* Handler for the credentials authorization
Expand All @@ -27,32 +30,48 @@ class AuthHandler implements HandlerInterface, HandlerDescriptionInterface
const STATUS_FAILURE = 'login failure';

/**
* Current request
*
* @var RestRequestInterface
*/
protected $request;

/**
* @var \Cundd\Rest\SessionManager
* @inject
* @var SessionManager
*/
protected $sessionManager;

/**
* Provider that will check the user credentials
*
* @var \Cundd\Rest\Authentication\UserProviderInterface
* @inject
* @var UserProviderInterface
*/
protected $userProvider;

/**
* @var \Cundd\Rest\RequestFactoryInterface
* @inject
* Current request
*
* @var RestRequestInterface
* @deprecated will be removed in 5.0
*/
protected $request;

/**
* @var RequestFactoryInterface
* @deprecated will be removed in 5.0
*/
protected $requestFactory;

/**
* AuthHandler constructor.
*
* @param SessionManager $sessionManager
* @param UserProviderInterface $userProvider
* @param RequestFactoryInterface $requestFactory
*/
public function __construct(
SessionManager $sessionManager,
UserProviderInterface $userProvider,
?RequestFactoryInterface $requestFactory = null
) {
$this->sessionManager = $sessionManager;
$this->userProvider = $userProvider;
$this->requestFactory = $requestFactory;
}

/**
* Return the description of the handler
*
Expand All @@ -63,7 +82,6 @@ public function getDescription()
return 'Handler for separate authorization requests';
}


/**
* Returns the current status
*
Expand Down
27 changes: 22 additions & 5 deletions Classes/VirtualObject/Persistence/PersistenceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Cundd\Rest\VirtualObject\Persistence;

use Cundd\Rest\ObjectManagerInterface;
use Cundd\Rest\VirtualObject\ConfigurationInterface;
use Cundd\Rest\VirtualObject\Exception\MissingConfigurationException;
use Cundd\Rest\VirtualObject\ObjectConverter;
Expand All @@ -11,14 +12,12 @@
class PersistenceManager implements PersistenceManagerInterface
{
/**
* @var \Cundd\Rest\ObjectManager
* @inject
* @var ObjectManagerInterface
*/
protected $objectManager;

/**
* @var \Cundd\Rest\VirtualObject\Persistence\BackendInterface
* @inject
* @var BackendInterface
*/
protected $backend;

Expand All @@ -34,6 +33,25 @@ class PersistenceManager implements PersistenceManagerInterface
*/
protected $configuration;

/**
* Persistence Manager constructor
*
* @param ObjectManagerInterface $objectManager
* @param BackendInterface $backend
* @param ConfigurationInterface $configuration
*/
public function __construct(
ObjectManagerInterface $objectManager,
BackendInterface $backend,
?ConfigurationInterface $configuration = null
) {
$this->objectManager = $objectManager;
$this->backend = $backend;
if ($configuration) {
$this->setConfiguration($configuration);
}
}

public function registerObject(VirtualObject $object): VirtualObject
{
$identifierQuery = $this->getIdentifierColumnsOfObject($object);
Expand Down Expand Up @@ -111,7 +129,6 @@ public function getObjectByIdentifier($identifier)
$identifierProperty = $configuration->getIdentifier();
$identifierKey = $configuration->getSourceKeyForProperty($identifierProperty);


$objectConverter = $this->getObjectConverter();
$query = new Query([$identifierKey => $identifier]);

Expand Down
24 changes: 20 additions & 4 deletions Classes/VirtualObject/Persistence/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Cundd\Rest\VirtualObject\Persistence;

use Cundd\Rest\ObjectManager;
use Cundd\Rest\VirtualObject\ConfigurationInterface;
use Cundd\Rest\VirtualObject\Exception\MissingConfigurationException;
use Cundd\Rest\VirtualObject\VirtualObject;
Expand All @@ -14,8 +15,7 @@
class Repository implements RepositoryInterface
{
/**
* @var \Cundd\Rest\ObjectManager
* @inject
* @var ObjectManager
*/
protected $objectManager;

Expand All @@ -27,11 +27,27 @@ class Repository implements RepositoryInterface
protected $configuration;

/**
* @var \Cundd\Rest\VirtualObject\Persistence\PersistenceManager
* @inject
* @var PersistenceManager
*/
protected $persistenceManager;

/**
* Repository constructor.
*
* @param ObjectManager $objectManager
* @param PersistenceManager $persistenceManager
* @param ConfigurationInterface $configuration
*/
public function __construct(
ObjectManager $objectManager,
PersistenceManager $persistenceManager,
?ConfigurationInterface $configuration = null
) {
$this->objectManager = $objectManager;
$this->configuration = $configuration;
$this->persistenceManager = $persistenceManager;
}

public function registerObject(VirtualObject $object): VirtualObject
{
return $this->persistenceManager->registerObject($object);
Expand Down
34 changes: 34 additions & 0 deletions Tests/Fixtures/UserProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);

namespace Cundd\Rest\Tests\Fixtures;

use Cundd\Rest\Authentication\UserProviderInterface;

class UserProvider implements UserProviderInterface
{
public function checkCredentials(string $username, string $password): bool
{
return $username === $this->getApiUser() && $password === $this->getApiKey();
}

/**
* Correct user for the API
*
* @return string
*/
public static function getApiUser()
{
return 'daniel';
}

/**
* Correct API key
*
* @return string
*/
public static function getApiKey()
{
return 'api-key';
}
}
Loading

0 comments on commit 94780af

Please sign in to comment.