Completed
Pull Request — master (#39)
by Wachter
06:07
created

TaskExtension::getConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
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'], $container, $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
     * @param ContainerBuilder $container
86
     */
87 25
    private function loadLockingComponent(array $config, ContainerBuilder $container, LoaderInterface $loader)
88
    {
89 25
        if (!$config['enabled']) {
90
            return $loader->load('locking/null.xml');
91
        }
92
93 25
        $loader->load('locking/services.xml');
94 25
        $container->setParameter('task.lock.ttl', $config['ttl']);
95 25
    }
96
97
    /**
98
     * Find storage aliases and related ids.
99
     *
100
     * @param ContainerBuilder $container
101
     *
102
     * @return array
103
     */
104 25
    private function getLockingStorageAliases(ContainerBuilder $container)
105
    {
106 25
        $taggedServices = $container->findTaggedServiceIds('task.lock.storage');
107
108 25
        $result = [];
109 25
        foreach ($taggedServices as $id => $tags) {
110 25
            foreach ($tags as $tag) {
111 25
                $result[$tag['alias']] = $id;
112 25
            }
113 25
        }
114
115 25
        return $result;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 25
    public function getConfiguration(array $config, ContainerBuilder $container)
122
    {
123 25
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
124 25
        $loader->load('locking/storages.xml');
125
126 25
        return new Configuration($this->getLockingStorageAliases($container));
127
    }
128
}
129