Completed
Push — master ( a5a753...454108 )
by Wachter
07:05
created

TaskExtension   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 7
dl 0
loc 47
ccs 26
cts 28
cp 0.9286
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 20 2
A loadDoctrineAdapter() 0 14 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\FileLocator;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Symfony\Component\DependencyInjection\Loader;
19
use Symfony\Component\DependencyInjection\Reference;
20
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
21
use Task\Event\Events;
22
use Task\TaskBundle\EventListener\DoctrineTaskExecutionListener;
23
24
/**
25
 * Container extension for php-task library.
26
 */
27
class TaskExtension extends Extension
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32 29
    public function load(array $configs, ContainerBuilder $container)
33
    {
34 29
        $configuration = new Configuration();
35 29
        $config = $this->processConfiguration($configuration, $configs);
36
37 29
        $container->setParameter('task.system_tasks', $config['system_tasks']);
38 29
        $container->setParameter('task.storage', $config['storage']);
39
40 29
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
41 29
        $loader->load(sprintf('storage/%s.xml', $config['storage']));
42 29
        $loader->load('task_event_listener.xml');
43 29
        $loader->load('scheduler.xml');
44 29
        $loader->load('command.xml');
45
46 29
        if ($config['run']['mode'] === 'listener') {
47
            $loader->load('listener.xml');
48
        }
49
50 29
        $this->loadDoctrineAdapter($config['adapters']['doctrine'], $container);
51 29
    }
52
53
    /**
54
     * Load doctrine adapter.
55
     *
56
     * @param array $config
57
     * @param ContainerBuilder $container
58
     */
59 29
    private function loadDoctrineAdapter(array $config, ContainerBuilder $container)
60
    {
61 29
        if ($config['clear']) {
62 29
            $definition = new Definition(
63 29
                DoctrineTaskExecutionListener::class,
64 29
                [new Reference('doctrine.orm.entity_manager', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]
65 29
            );
66 29
            $definition->addTag(
67 29
                'kernel.event_listener',
68 29
                ['event' => Events::TASK_AFTER, 'method' => 'clearEntityManagerAfterTask']
69 29
            );
70 29
            $container->setDefinition('task.adapter.doctrine.execution_listener', $definition);
71 29
        }
72 29
    }
73
}
74