Completed
Pull Request — master (#2)
by Wachter
05:57
created

TaskCompilerPass::process()   C

Complexity

Conditions 7
Paths 11

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 7.0028

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 38
ccs 25
cts 26
cp 0.9615
rs 6.7273
cc 7
eloc 21
nc 11
nop 1
crap 7.0028
1
<?php
2
3
namespace Task\TaskBundle\DependencyInjection;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
8
/**
9
 * Compiler pass which collects worker services.
10
 *
11
 * @author @wachterjohannes <[email protected]>
12
 */
13
class TaskCompilerPass implements CompilerPassInterface
14
{
15
    const SCHEDULER_ID = 'task.scheduler';
16
    const INTERVAL_TAG = 'task.interval';
17
    const KEY_ATTRIBUTE = 'key';
18
    const INTERVAL_ATTRIBUTE = 'interval';
19
    const WORKLOAD_ATTRIBUTE = 'workload';
20
    const CREATE_FUNCTION_NAME = 'createTaskAndSchedule';
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 36
    public function process(ContainerBuilder $container)
26
    {
27 36
        if (!$container->has(self::SCHEDULER_ID)) {
28
            return;
29
        }
30
31 36
        $schedulerDefinition = $container->getDefinition(self::SCHEDULER_ID);
32
33 36
        $taggedServices = $container->findTaggedServiceIds(self::INTERVAL_TAG);
34 36
        foreach ($taggedServices as $id => $tags) {
35 6
            $handlerDefinition = $container->getDefinition($id);
36 6
            $tag = $handlerDefinition->getTag(HandlerCompilerPass::HANDLER_TAG);
37
38
            // TODO handle also multiple handler tag here
39 6
            $handler = $tag[0][HandlerCompilerPass::HANDLER_NAME_ATTRIBUTE];
40
41
            // remove all tasks with $id and not completed
42 6
            foreach ($tags as $attributes) {
43 6
                $interval = $attributes[self::INTERVAL_ATTRIBUTE];
44 6
                $workload = isset($attributes[self::WORKLOAD_ATTRIBUTE]) ? $attributes[self::WORKLOAD_ATTRIBUTE] : null;
45 6
                $key = isset($attributes[self::KEY_ATTRIBUTE]) ? $attributes[self::KEY_ATTRIBUTE] : null;
46
47 6
                if (!$key) {
48 6
                    $key = $handler . '_' . $interval . '_' . serialize($workload);
49 5
                }
50
51 6
                $schedulerDefinition->addMethodCall(
52 6
                    self::CREATE_FUNCTION_NAME,
53
                    [
54 6
                        $handler,
55 6
                        $workload,
56 6
                        $interval,
57 6
                        $key,
58
                    ]
59 5
                );
60 5
            }
61 30
        }
62 36
    }
63
}
64