Completed
Push — master ( 1c341d...381036 )
by Wachter
06:54
created

TaskExtension::loadDoctrineAdapter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
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\Definition\Exception\InvalidConfigurationException;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\Config\Loader\LoaderInterface;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
use Symfony\Component\DependencyInjection\Definition;
20
use Symfony\Component\DependencyInjection\Loader;
21
use Symfony\Component\DependencyInjection\Reference;
22
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
23
use Task\Event\Events;
24
use Task\TaskBundle\EventListener\DoctrineTaskExecutionListener;
25
26
/**
27
 * Container extension for php-task library.
28
 */
29
class TaskExtension extends Extension
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34 2
    public function load(array $configs, ContainerBuilder $container)
35
    {
36 2
        $configuration = $this->getConfiguration($configs, $container);
37 2
        $config = $this->processConfiguration($configuration, $configs);
0 ignored issues
show
Bug introduced by
It seems like $configuration defined by $this->getConfiguration($configs, $container) on line 36 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...
38
39 2
        $container->setParameter('task.system_tasks', $config['system_tasks']);
40
41 2
        $container->setAlias('task.lock.storage', $configuration->getLockingStorageId($config['locking']['storage']));
42 2
        foreach (array_keys($config['locking']['storages']) as $key) {
43 2
            $container->setParameter('task.lock.storages.' . $key, $config['locking']['storages'][$key]);
44
        }
45
46 2
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
47 2
        $loader->load(sprintf('storage/%s.xml', $config['storage']));
48 2
        $loader->load('task_event_listener.xml');
49 2
        $loader->load('scheduler.xml');
50 2
        $loader->load('command.xml');
51 2
        $loader->load('locking/services.xml');
52
53 2
        if ($config['run']['mode'] === 'listener') {
54
            $loader->load('listener.xml');
55
        }
56
57 2
        $this->loadDoctrineAdapter($config['adapters']['doctrine'], $container);
58 2
        $this->loadLockingComponent($config['locking'], $container, $loader);
59 2
        $this->loadExecutorComponent($config['executor'], $container, $loader);
60 2
    }
61
62
    /**
63
     * Load doctrine adapter.
64
     *
65
     * @param array $config
66
     * @param ContainerBuilder $container
67
     */
68 2
    private function loadDoctrineAdapter(array $config, ContainerBuilder $container)
69
    {
70 2
        if ($config['clear']) {
71 2
            $definition = new Definition(
72 2
                DoctrineTaskExecutionListener::class,
73 2
                [new Reference('doctrine.orm.entity_manager', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]
74
            );
75 2
            $definition->addTag(
76 2
                'kernel.event_listener',
77 2
                ['event' => Events::TASK_AFTER, 'method' => 'clearEntityManagerAfterTask']
78
            );
79 2
            $container->setDefinition('task.adapter.doctrine.execution_listener', $definition);
80
        }
81 2
    }
82
83
    /**
84
     * Load services for locking component.
85
     *
86
     * @param array $config
87
     * @param LoaderInterface $loader
88
     * @param ContainerBuilder $container
89
     */
90 2
    private function loadLockingComponent(array $config, ContainerBuilder $container, LoaderInterface $loader)
91
    {
92 2
        if (!$config['enabled'] || 'null' === $config['storage']) {
93
            return $loader->load('locking/null.xml');
94
        }
95
96 2
        $loader->load('locking/services.xml');
97 2
        $container->setParameter('task.lock.ttl', $config['ttl']);
98 2
    }
99
100
    /**
101
     * Load services for executor component.
102
     *
103
     * @param array $config
104
     * @param LoaderInterface $loader
105
     * @param ContainerBuilder $container
106
     */
107 2
    private function loadExecutorComponent(array $config, ContainerBuilder $container, LoaderInterface $loader)
108
    {
109 2
        $loader->load('executor/' . $config['type'] . '.xml');
110 2
        $container->setAlias('task.executor', 'task.executor.' . $config['type']);
111
112 2
        if (!array_key_exists($config['type'], $config)) {
113
            return;
114
        }
115
116 2
        foreach ($config[$config['type']] as $key => $value) {
117 2
            $container->setParameter('task.executor.' . $key, $value);
118
        }
119
120 2
        if (!file_exists($container->getParameter('task.executor.console_path'))) {
121
            throw new InvalidConfigurationException(
122
                'Console file does not exists! Given in "task.executor.seperate.console".'
123
            );
124
        }
125 2
    }
126
127
    /**
128
     * Find storage aliases and related ids.
129
     *
130
     * @param ContainerBuilder $container
131
     *
132
     * @return array
133
     */
134 2
    private function getLockingStorageAliases(ContainerBuilder $container)
135
    {
136 2
        $taggedServices = $container->findTaggedServiceIds('task.lock.storage');
137
138 2
        $result = ['null'];
139 2
        foreach ($taggedServices as $id => $tags) {
140 2
            foreach ($tags as $tag) {
141 2
                $result[$tag['alias']] = $id;
142
            }
143
        }
144
145 2
        return $result;
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151 2
    public function getConfiguration(array $config, ContainerBuilder $container)
152
    {
153 2
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
154 2
        $loader->load('locking/storages.xml');
155
156 2
        return new Configuration($this->getLockingStorageAliases($container));
0 ignored issues
show
Documentation introduced by
$this->getLockingStorageAliases($container) is of type array<?,integer|string>, but the function expects a array<integer,object<string>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
157
    }
158
}
159