Completed
Pull Request — master (#40)
by Wachter
05:27
created

TaskExtension   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 93.55%

Importance

Changes 0
Metric Value
wmc 14
c 0
b 0
f 0
lcom 1
cbo 8
dl 0
loc 124
ccs 58
cts 62
cp 0.9355
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B load() 0 27 3
A loadDoctrineAdapter() 0 14 2
A loadLockingComponent() 0 9 2
A loadExecutorComponent() 0 13 3
A getLockingStorageAliases() 0 13 3
A getConfiguration() 0 7 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 30
    public function load(array $configs, ContainerBuilder $container)
34
    {
35 30
        $configuration = $this->getConfiguration($configs, $container);
36 30
        $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 30
        $container->setParameter('task.system_tasks', $config['system_tasks']);
39
40 30
        $container->setAlias('task.lock.storage', $configuration->getLockingStorageId($config['locking']['storage']));
41 30
        foreach (array_keys($config['locking']['storages']) as $key) {
42 30
            $container->setParameter('task.lock.storages.' . $key, $config['locking']['storages'][$key]);
43 30
        }
44
45 30
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
46 30
        $loader->load(sprintf('storage/%s.xml', $config['storage']));
47 30
        $loader->load('task_event_listener.xml');
48 30
        $loader->load('scheduler.xml');
49 30
        $loader->load('command.xml');
50 30
        $loader->load('locking/services.xml');
51
52 30
        if ($config['run']['mode'] === 'listener') {
53
            $loader->load('listener.xml');
54
        }
55
56 30
        $this->loadDoctrineAdapter($config['adapters']['doctrine'], $container);
57 30
        $this->loadLockingComponent($config['locking'], $container, $loader);
58 30
        $this->loadExecutorComponent($config['executor'], $container, $loader);
59 30
    }
60
61
    /**
62
     * Load doctrine adapter.
63
     *
64
     * @param array $config
65
     * @param ContainerBuilder $container
66
     */
67 30
    private function loadDoctrineAdapter(array $config, ContainerBuilder $container)
68
    {
69 30
        if ($config['clear']) {
70 30
            $definition = new Definition(
71 30
                DoctrineTaskExecutionListener::class,
72 30
                [new Reference('doctrine.orm.entity_manager', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]
73 30
            );
74 30
            $definition->addTag(
75 30
                'kernel.event_listener',
76 30
                ['event' => Events::TASK_AFTER, 'method' => 'clearEntityManagerAfterTask']
77 30
            );
78 30
            $container->setDefinition('task.adapter.doctrine.execution_listener', $definition);
79 30
        }
80 30
    }
81
82
    /**
83
     * Load services for locking component.
84
     *
85
     * @param array $config
86
     * @param LoaderInterface $loader
87
     * @param ContainerBuilder $container
88
     */
89 30
    private function loadLockingComponent(array $config, ContainerBuilder $container, LoaderInterface $loader)
90
    {
91 30
        if (!$config['enabled']) {
92
            return $loader->load('locking/null.xml');
93
        }
94
95 30
        $loader->load('locking/services.xml');
96 30
        $container->setParameter('task.lock.ttl', $config['ttl']);
97 30
    }
98
99
    /**
100
     * Load services for executor component.
101
     *
102
     * @param array $config
103
     * @param LoaderInterface $loader
104
     * @param ContainerBuilder $container
105
     */
106 30
    private function loadExecutorComponent(array $config, ContainerBuilder $container, LoaderInterface $loader)
107
    {
108 30
        $loader->load('executor/' . $config['type'] . '.xml');
109 30
        $container->setAlias('task.executor', 'task.executor.' . $config['type']);
110
111 30
        if (!array_key_exists($config['type'], $config)) {
112
            return;
113
        }
114
115 30
        foreach ($config[$config['type']] as $key => $value) {
116 30
            $container->setParameter('task.executor.' . $key, $value);
117 30
        }
118 30
    }
119
120
    /**
121
     * Find storage aliases and related ids.
122
     *
123
     * @param ContainerBuilder $container
124
     *
125
     * @return array
126
     */
127 30
    private function getLockingStorageAliases(ContainerBuilder $container)
128
    {
129 30
        $taggedServices = $container->findTaggedServiceIds('task.lock.storage');
130
131 30
        $result = [];
132 30
        foreach ($taggedServices as $id => $tags) {
133 30
            foreach ($tags as $tag) {
134 30
                $result[$tag['alias']] = $id;
135 30
            }
136 30
        }
137
138 30
        return $result;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144 30
    public function getConfiguration(array $config, ContainerBuilder $container)
145
    {
146 30
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
147 30
        $loader->load('locking/storages.xml');
148
149 30
        return new Configuration($this->getLockingStorageAliases($container));
150
    }
151
}
152