Completed
Pull Request — master (#39)
by Wachter
23:26 queued 21:10
created

TaskExtension::getLockingStorageAliases()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
3
/*
4
 * This file is part of php-task library.
5
 *
6
 * (c) php-task
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Task\TaskBundle\DependencyInjection;
13
14
use Symfony\Component\Config\FileLocator;
15
use Symfony\Component\Config\Loader\LoaderInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
use Symfony\Component\DependencyInjection\Definition;
19
use Symfony\Component\DependencyInjection\Loader;
20
use Symfony\Component\DependencyInjection\Reference;
21
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
22
use Task\Event\Events;
23
use Task\TaskBundle\EventListener\DoctrineTaskExecutionListener;
24
25
/**
26
 * Container extension for php-task library.
27
 */
28
class TaskExtension extends Extension
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33 25
    public function load(array $configs, ContainerBuilder $container)
34
    {
35 25
        $configuration = $this->getConfiguration($configs, $container);
36 25
        $config = $this->processConfiguration($configuration, $configs);
0 ignored issues
show
Bug introduced by
It seems like $configuration defined by $this->getConfiguration($configs, $container) on line 35 can be null; however, Symfony\Component\Depend...:processConfiguration() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
37
38 25
        $container->setAlias('task.lock.storage', $configuration->getLockingStorageId($config['locking']['storage']));
39
40 25
        foreach (array_keys($config['locking']['storages']) as $key) {
41 25
            $container->setParameter('task.lock.storages.' . $key, $config['locking']['storages'][$key]);
42 25
        }
43
44 25
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
45 25
        $loader->load(sprintf('storage/%s.xml', $config['storage']));
46 25
        $loader->load('task_event_listener.xml');
47 25
        $loader->load('scheduler.xml');
48 25
        $loader->load('command.xml');
49
50 25
        $loader->load('locking/services.xml');
51 25
        if ($config['run']['mode'] === 'listener') {
52
            $loader->load('listener.xml');
53
        }
54
55 25
        $this->loadDoctrineAdapter($config['adapters']['doctrine'], $container);
56 25
        $this->loadLockingComponent($config['locking'], $loader);
57 25
    }
58
59
    /**
60
     * Load doctrine adapter.
61
     *
62
     * @param array $config
63
     * @param ContainerBuilder $container
64
     */
65 25
    private function loadDoctrineAdapter(array $config, ContainerBuilder $container)
66
    {
67 25
        if ($config['clear']) {
68 25
            $definition = new Definition(
69 25
                DoctrineTaskExecutionListener::class,
70 25
                [new Reference('doctrine.orm.entity_manager', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]
71 25
            );
72 25
            $definition->addTag(
73 25
                'kernel.event_listener',
74 25
                ['event' => Events::TASK_AFTER, 'method' => 'clearEntityManagerAfterTask']
75 25
            );
76 25
            $container->setDefinition('task.adapter.doctrine.execution_listener', $definition);
77 25
        }
78 25
    }
79
80
    /**
81
     * Load services for locking component.
82
     *
83
     * @param array $config
84
     * @param LoaderInterface $loader
85
     */
86 25
    private function loadLockingComponent(array $config, LoaderInterface $loader)
87
    {
88 25
        if (!$config['enabled']) {
89
            return $loader->load('locking/null.xml');
90
        }
91
92 25
        $loader->load('locking/services.xml');
93 25
    }
94
95
    /**
96
     * Find storage aliases and related ids.
97
     *
98
     * @param ContainerBuilder $container
99
     *
100
     * @return array
101
     */
102 25
    private function getLockingStorageAliases(ContainerBuilder $container)
103
    {
104 25
        $taggedServices = $container->findTaggedServiceIds('task.lock.storage');
105
106 25
        $result = [];
107 25
        foreach ($taggedServices as $id => $tags) {
108 25
            foreach ($tags as $tag) {
109 25
                $result[$tag['alias']] = $id;
110 25
            }
111 25
        }
112
113 25
        return $result;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 25
    public function getConfiguration(array $config, ContainerBuilder $container)
120
    {
121 25
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
122 25
        $loader->load('locking/storages.xml');
123
124 25
        return new Configuration($this->getLockingStorageAliases($container));
125
    }
126
}
127