Completed
Pull Request — master (#39)
by Wachter
24:21
created

TaskExtension   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 8
dl 0
loc 99
ccs 24
cts 26
cp 0.9231
rs 10

5 Methods

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