Completed
Push — master ( a45cae...481637 )
by Daniel
39:55
created

TaskExtension::loadDoctrineAdapter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 9
nc 2
nop 2
crap 6
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 15
{
29
    /**
30 15
     * {@inheritdoc}
31 15
     */
32
    public function load(array $configs, ContainerBuilder $container)
33 15
    {
34 15
        $configuration = new Configuration();
35 15
        $config = $this->processConfiguration($configuration, $configs);
36 15
37 15
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
38
        $loader->load(sprintf('storage/%s.xml', $config['storage']));
39 15
        $loader->load('task_event_listener.xml');
40
        $loader->load('scheduler.xml');
41
        $loader->load('command.xml');
42
43 15
        if ($config['run']['mode'] === 'listener') {
44
            $loader->load('listener.xml');
45 15
        }
46 15
47 15
        $this->loadDoctrineAdapter($config['adapters']['doctrine'], $container);
48 15
    }
49 15
50 15
    /**
51
     * Load doctrine adapter.
52
     *
53
     * @param array $config
54
     * @param ContainerBuilder $container
55
     */
56
    private function loadDoctrineAdapter(array $config, ContainerBuilder $container)
57
    {
58
        if ($config['clear']) {
59
            $definition = new Definition(
60
                DoctrineTaskExecutionListener::class,
61
                [new Reference('doctrine.orm.entity_manager', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]
62
            );
63
            $definition->addTag(
64
                'kernel.event_listener',
65
                ['event' => Events::TASK_AFTER, 'method' => 'clearEntityManagerAfterTask']
66
            );
67
            $container->setDefinition('task.adapter.doctrine.execution_listener', $definition);
68
        }
69
    }
70
}
71