Skip to content

Commit

Permalink
adopted Broadway coding standard
Browse files Browse the repository at this point in the history
  • Loading branch information
othillo committed Apr 3, 2020
1 parent 8f7c987 commit 8ceb61b
Show file tree
Hide file tree
Showing 25 changed files with 163 additions and 112 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ composer.lock
/Tests/Functional/logs
.phpunit.result.cache
var/
.php_cs.cache
13 changes: 13 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$config = require 'vendor/broadway/coding-standard/.php_cs.dist';

$config->setFinder(
\PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude([
__DIR__ . '/vendor/'
])
);

return $config;
2 changes: 2 additions & 0 deletions Annotations/Toggle.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Qandidate\Bundle\ToggleBundle\Annotations;

/**
Expand Down
4 changes: 3 additions & 1 deletion Context/UserContextFactory.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the qandidate-labs/qandidate-toggle-bundle package.
*
Expand All @@ -23,7 +25,7 @@ public function __construct(TokenStorageInterface $tokenStorage)
}

/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function createContext()
{
Expand Down
10 changes: 5 additions & 5 deletions DataCollector/ToggleCollector.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the qandidate-labs/qandidate-toggle-bundle package.
*
Expand Down Expand Up @@ -35,8 +37,6 @@ class ToggleCollector extends DataCollector

/**
* ToggleCollector constructor.
* @param ToggleManager $toggleManager
* @param ContextFactory $contextFactory
*/
public function __construct(ToggleManager $toggleManager, ContextFactory $contextFactory)
{
Expand All @@ -47,8 +47,8 @@ public function __construct(ToggleManager $toggleManager, ContextFactory $contex
/**
* Collects data for the given Request and Response.
*
* @param Request $request A Request instance
* @param Response $response A Response instance
* @param Request $request A Request instance
* @param Response $response A Response instance
* @param \Exception $exception An Exception instance
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
Expand Down Expand Up @@ -91,6 +91,6 @@ public function getName()

public function reset()
{
$this->data = array();
$this->data = [];
}
}
10 changes: 6 additions & 4 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the qandidate-labs/qandidate-toggle-bundle package.
*
Expand All @@ -17,7 +19,7 @@
class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
Expand All @@ -28,7 +30,7 @@ public function getConfigTreeBuilder()
$rootNode
->children()
->enumNode('persistence')
->values(array('in_memory', 'redis', 'factory', 'config'))
->values(['in_memory', 'redis', 'factory', 'config'])
->defaultValue('in_memory')
->end()
->arrayNode('collection_factory')
Expand Down Expand Up @@ -75,8 +77,8 @@ public function getConfigTreeBuilder()
->end()
->validate()
->ifTrue(function ($v) {
if (isset($v['persistence']) && $v['persistence'] === 'factory') {
return ! isset($v['collection_factory']['service_id'], $v['collection_factory']['method']);
if (isset($v['persistence']) && 'factory' === $v['persistence']) {
return !isset($v['collection_factory']['service_id'], $v['collection_factory']['method']);
}

return false;
Expand Down
18 changes: 10 additions & 8 deletions DependencyInjection/QandidateToggleExtension.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the qandidate-labs/qandidate-toggle-bundle package.
*
Expand All @@ -23,7 +25,7 @@
class QandidateToggleExtension extends Extension
{
/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
Expand All @@ -45,28 +47,28 @@ public function load(array $configs, ContainerBuilder $container)
case 'factory' === $config['persistence']:
$collection = 'factory';
$definition = new Definition(InMemoryCollection::class);
$definition->setFactory(array(
$definition->setFactory([
new Reference($config['collection_factory']['service_id']),
$config['collection_factory']['method']
));
$config['collection_factory']['method'],
]);

$container->setDefinition('qandidate.toggle.collection.factory', $definition);

break;
case 'config' === $config['persistence']:
$collection = 'factory';
$definition = $container->getDefinition('qandidate.toggle.collection.in_memory');
$definition->setFactory(array(
$definition->setFactory([
new Reference('qandidate.toggle.collection.serializer.in_memory'),
'deserialize'
));
'deserialize',
]);
$definition->addArgument($config['toggles']);

$container->setDefinition('qandidate.toggle.collection.factory', $definition);
break;
}

$container->setAlias('qandidate.toggle.collection', new Alias('qandidate.toggle.collection.' . $collection, true));
$container->setAlias('qandidate.toggle.collection', new Alias('qandidate.toggle.collection.'.$collection, true));

$contextFactoryService = 'qandidate.toggle.user_context_factory';
if (null !== $config['context_factory']) {
Expand Down
28 changes: 15 additions & 13 deletions EventListener/ToggleListener.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php

declare(strict_types=1);

namespace Qandidate\Bundle\ToggleBundle\EventListener;

use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\Util\ClassUtils;
use Qandidate\Bundle\ToggleBundle\Annotations\Toggle;
use Qandidate\Toggle\Context;
use Qandidate\Toggle\ToggleManager;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Qandidate\Toggle\ToggleManager;
use Qandidate\Toggle\Context;
use Qandidate\Bundle\ToggleBundle\Annotations\Toggle;

class ToggleListener
{
Expand All @@ -18,35 +20,35 @@ class ToggleListener

public function __construct(Reader $reader, ToggleManager $toggleManager, Context $context)
{
$this->reader = $reader;
$this->toggleManager = $toggleManager;
$this->context = $context;
$this->reader = $reader;
$this->toggleManager = $toggleManager;
$this->context = $context;
}

public function onKernelController(FilterControllerEvent $event)
{
$controller = $event->getController();

if (is_array($controller)) {
$class = ClassUtils::getClass($controller[0]);
$object = new \ReflectionClass($class);
$method = $object->getMethod($controller[1]);
$class = ClassUtils::getClass($controller[0]);
$object = new \ReflectionClass($class);
$method = $object->getMethod($controller[1]);
} else {
$object = new \ReflectionClass($controller);
$method = $object->getMethod('__invoke');
$object = new \ReflectionClass($controller);
$method = $object->getMethod('__invoke');
}

foreach ($this->reader->getClassAnnotations($object) as $annotation) {
if ($annotation instanceof Toggle) {
if (! $this->toggleManager->active($annotation->name, $this->context)) {
if (!$this->toggleManager->active($annotation->name, $this->context)) {
throw new NotFoundHttpException();
}
}
}

foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
if ($annotation instanceof Toggle) {
if (! $this->toggleManager->active($annotation->name, $this->context)) {
if (!$this->toggleManager->active($annotation->name, $this->context)) {
throw new NotFoundHttpException();
}
}
Expand Down
3 changes: 2 additions & 1 deletion QandidateToggleBundle.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the qandidate-labs/qandidate-toggle-bundle package.
*
Expand All @@ -15,5 +17,4 @@

class QandidateToggleBundle extends Bundle
{

}
27 changes: 14 additions & 13 deletions Resources/doc/example/index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -8,7 +10,7 @@
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\RouteCollectionBuilder;

require __DIR__ . '/vendor/autoload.php';
require __DIR__.'/vendor/autoload.php';

class AppKernel extends Kernel
{
Expand All @@ -34,7 +36,6 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
'providers' => [
'my_custom_provider' => [
'memory' => [],

],
],
'firewalls' => [
Expand All @@ -48,28 +49,28 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
'persistence' => 'config',
'toggles' => [
'always-active-feature' => [
'name' => 'always-active-feature',
'name' => 'always-active-feature',
'status' => 'always-active',
],
'inactive-feature' => [
'name' => 'inactive-feature',
'status' => 'inactive',
],
'conditionally-active' => [
'name' => 'conditionally-active',
'name' => 'conditionally-active',
'status' => 'conditionally-active',
'conditions' => [
[
'name' => 'operator-condition',
'key' => 'user_id',
'key' => 'user_id',
'operator' => [
'name' => 'greater-than',
'name' => 'greater-than',
'value' => 42,
]
]
],
],
],
]
]
],
],
]);
}

Expand All @@ -81,12 +82,12 @@ protected function configureRoutes(RouteCollectionBuilder $routes)
public function indexAction()
{
$toggleManager = $this->getContainer()->get('qandidate.toggle.manager');
$context = new \Qandidate\Toggle\Context();
$context = new \Qandidate\Toggle\Context();
$context->set('user_id', 43);

$output = array_map(function(\Qandidate\Toggle\Toggle $toggle) use ($toggleManager, $context) {
$output = array_map(function (\Qandidate\Toggle\Toggle $toggle) use ($toggleManager, $context) {
return [
'name' => $toggle->getName(),
'name' => $toggle->getName(),
'active' => $toggleManager->active($toggle->getName(), $context),
];
}, $toggleManager->all());
Expand Down
4 changes: 3 additions & 1 deletion Tests/Context/UserContextFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the qandidate-labs/qandidate-toggle-bundle package.
*
Expand All @@ -21,7 +23,7 @@ class UserContextFactoryTest extends TestCase
public function setUp(): void
{
$this->tokenStorage = new TokenStorage();
$this->contextFactory = new UserContextFactory($this->tokenStorage);
$this->contextFactory = new UserContextFactory($this->tokenStorage);
}

/**
Expand Down
Loading

0 comments on commit 8ceb61b

Please sign in to comment.