Completed
Pull Request — master (#34)
by Wachter
03:38
created

TaskExtension::loadDoctrineAdapter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

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