Completed
Pull Request — master (#39)
by Wachter
06:48 queued 02:58
created

TaskExtension::load()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3.1406

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 28
ccs 15
cts 20
cp 0.75
rs 8.8571
cc 3
eloc 18
nc 3
nop 2
crap 3.1406
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\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 = new Configuration($container->getParameterBag());
0 ignored issues
show
Unused Code introduced by
The call to Configuration::__construct() has too many arguments starting with $container->getParameterBag().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
36 25
        $config = $this->processConfiguration($configuration, $configs);
37
38 25
        $fileConfig = $config['locking']['storages']['file'];
39 25
        if (!array_key_exists('directory', $fileConfig)) {
40
            $exception = new InvalidConfigurationException();
41
            $exception->setPath('task.locking.storages.file.directory');
42
43
            throw $exception;
44
        }
45
46 25
        $container->setParameter('task.lock.storages.file', $config['locking']['storages']['file']);
47
48 25
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
49 25
        $loader->load(sprintf('storage/%s.xml', $config['storage']));
50 25
        $loader->load('task_event_listener.xml');
51 25
        $loader->load('scheduler.xml');
52 25
        $loader->load('locking.xml');
53 25
        $loader->load('command.xml');
54
55 25
        if ($config['run']['mode'] === 'listener') {
56
            $loader->load('listener.xml');
57
        }
58
59 25
        $this->loadDoctrineAdapter($config['adapters']['doctrine'], $container);
60 25
    }
61
62
    /**
63
     * Load doctrine adapter.
64
     *
65
     * @param array $config
66
     * @param ContainerBuilder $container
67
     */
68 25
    private function loadDoctrineAdapter(array $config, ContainerBuilder $container)
69
    {
70 25
        if ($config['clear']) {
71 25
            $definition = new Definition(
72 25
                DoctrineTaskExecutionListener::class,
73 25
                [new Reference('doctrine.orm.entity_manager', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]
74 25
            );
75 25
            $definition->addTag(
76 25
                'kernel.event_listener',
77 25
                ['event' => Events::TASK_AFTER, 'method' => 'clearEntityManagerAfterTask']
78 25
            );
79 25
            $container->setDefinition('task.adapter.doctrine.execution_listener', $definition);
80 25
        }
81 25
    }
82
}
83