Skip to content

Commit

Permalink
move containers transform service definition to contant
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed May 25, 2018
1 parent b7fe27b commit 9f403da
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
27 changes: 19 additions & 8 deletions spec/Middleware/ExpressiveFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ErrorHeroModule\Spec\Middleware;

use Aura\Di\Container as AuraContainer;
use Aura\Di\ContainerBuilder as AuraContainerBuilder;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\PDOMySql\Driver;
use Doctrine\ORM\EntityManager;
Expand Down Expand Up @@ -173,7 +174,7 @@

it('throws RuntimeException when using Symfony Container but no "db" config', function () {

$container = Double::instance(['extends' => SymfonyContainerBuilder::class, 'methods' => '__construct']);
$container = new SymfonyContainerBuilder();
allow($container)->toReceive('get')->with('config')
->andReturn([]);

Expand All @@ -190,15 +191,20 @@
$actual = function () use ($container) {
$this->factory($container);
};
expect($actual)->toThrow(new RuntimeException('db config is required for build "ErrorHeroModuleLogger" service by Symfony Container'));
expect($actual)->toThrow(new RuntimeException(
sprintf(
'db config is required for build "ErrorHeroModuleLogger" service by %s Container',
SymfonyContainerBuilder::class
)
));

});

it('returns Expressive Middleware instance with create services first for Symfony Container and db name found in adapters', function () {

$config = $this->config;
$config['log']['ErrorHeroModuleLogger']['writers'][0]['options']['db'] = 'my-adapter';
$container = Double::instance(['extends' => SymfonyContainerBuilder::class, 'methods' => '__construct']);
$container = new SymfonyContainerBuilder();
allow($container)->toReceive('get')->with('config')
->andReturn($config);

Expand All @@ -219,7 +225,7 @@

it('returns Expressive Middleware instance with create services first for Symfony Container and db name not found in adapters, which means use "Zend\Db\Adapter\Adapter" name', function () {

$container = Double::instance(['extends' => SymfonyContainerBuilder::class, 'methods' => '__construct']);
$container = new SymfonyContainerBuilder();
allow($container)->toReceive('get')->with('config')
->andReturn($this->config);

Expand All @@ -240,7 +246,7 @@

it('throws RuntimeException when using Aura Container but no "db" config', function () {

$container = Double::instance(['extends' => AuraContainer::class, 'methods' => '__construct']);
$container = (new AuraContainerBuilder())->newInstance();
allow($container)->toReceive('get')->with('config')
->andReturn([]);

Expand All @@ -257,15 +263,20 @@
$actual = function () use ($container) {
$this->factory($container);
};
expect($actual)->toThrow(new RuntimeException('db config is required for build "ErrorHeroModuleLogger" service by Aura Container'));
expect($actual)->toThrow(new RuntimeException(
sprintf(
'db config is required for build "ErrorHeroModuleLogger" service by %s Container',
AuraContainer::class
)
));

});

it('returns Expressive Middleware instance with create services first for Aura Container and db name found in adapters', function () {

$config = $this->config;
$config['log']['ErrorHeroModuleLogger']['writers'][0]['options']['db'] = 'my-adapter';
$container = Double::instance(['extends' => AuraContainer::class, 'methods' => '__construct']);
$container = (new AuraContainerBuilder())->newInstance();
allow($container)->toReceive('get')->with('config')
->andReturn($config);

Expand All @@ -286,7 +297,7 @@

it('returns Expressive Middleware instance with create services first for Aura Container and db name not found in adapters, which means use "Zend\Db\Adapter\Adapter" name', function () {

$container = Double::instance(['extends' => AuraContainer::class, 'methods' => '__construct']);
$container = (new AuraContainerBuilder())->newInstance();
allow($container)->toReceive('get')->with('config')
->andReturn($this->config);

Expand Down
1 change: 0 additions & 1 deletion src/Handler/Logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ private function collectErrorExceptionExtraData(array $collectedExceptionData) :
. '$ php ' . $request->getScriptName() . ' ' . $request->toString();
} else {
Assertion::isInstanceOf($request, HttpRequest::class);

$uri = $request->getUri();
$this->serverUrl = $uri->getScheme() . '://' . $uri->getHost();
$port = $uri->getPort();
Expand Down
2 changes: 1 addition & 1 deletion src/Listener/Mvc.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ private function showDefaultViewWhenDisplayErrorSetttingIsDisabled(MvcEvent $e)

$request = $e->getRequest();
Assertion::isInstanceOf($request, Request::class);

$isXmlHttpRequest = $request->isXmlHttpRequest();

if ($isXmlHttpRequest === true &&
isset($this->errorHeroModuleConfig['display-settings']['ajax']['message'])
) {
Expand Down
29 changes: 14 additions & 15 deletions src/Middleware/ExpressiveFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

class ExpressiveFactory
{
private const CONTAINERS_TRANSFORM = [
SymfonyContainerBuilder::class => SymfonyService::class,
AuraContainer::class => AuraService::class,
];

private function createMiddlewareInstance(ContainerInterface $container, array $configuration) : Expressive
{
return new Expressive(
Expand All @@ -27,14 +32,14 @@ private function createMiddlewareInstance(ContainerInterface $container, array $
);
}

private function verifyConfig($configuration, string $containerType = 'Symfony') : array
private function verifyConfig($configuration, string $containerClass) : array
{
$configuration = (array) $configuration;
if (! isset($configuration['db'])) {
throw new RuntimeException(
sprintf(
'db config is required for build "ErrorHeroModuleLogger" service by %s Container',
$containerType
$containerClass
)
);
}
Expand All @@ -57,27 +62,21 @@ public function __invoke(ContainerInterface $container) : Expressive
return $this->createMiddlewareInstance($container, $configuration);
}

if ($container instanceof SymfonyContainerBuilder) {
$configuration = $this->verifyConfig($configuration, 'Symfony');

return $this->createMiddlewareInstance(
SymfonyService::transform($container, $configuration),
$configuration
);
}

if ($container instanceof AuraContainer) {
$configuration = $this->verifyConfig($configuration, 'Aura');
$containerClass = get_class($container);
if (in_array($containerClass, array_keys(self::CONTAINERS_TRANSFORM), true)) {
$configuration = $this->verifyConfig($configuration, $containerClass);
/** @var \ErrorHeroModule\Transformer\TransformerInterface $transformer */
$transformer = self::CONTAINERS_TRANSFORM[$containerClass];

return $this->createMiddlewareInstance(
AuraService::transform($container, $configuration),
$transformer::transform($container, $configuration),
$configuration
);
}

throw new RuntimeException(sprintf(
'container "%s" is unsupported',
get_class($container)
$containerClass
));
}
}

0 comments on commit 9f403da

Please sign in to comment.