Completed
Pull Request — master (#2)
by Wachter
05:49 queued 01:29
created

TaskCompilerPass   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 7
c 2
b 0
f 2
lcom 0
cbo 2
dl 0
loc 51
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C process() 0 38 7
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
    public function process(ContainerBuilder $container)
26
    {
27
        if (!$container->has(self::SCHEDULER_ID)) {
28
            return;
29
        }
30
31
        $schedulerDefinition = $container->getDefinition(self::SCHEDULER_ID);
32
33
        $taggedServices = $container->findTaggedServiceIds(self::INTERVAL_TAG);
34
        foreach ($taggedServices as $id => $tags) {
35
            $handlerDefinition = $container->getDefinition($id);
36
            $tag = $handlerDefinition->getTag(HandlerCompilerPass::HANDLER_TAG);
37
38
            // TODO handle also multiple handler tag here
39
            $handler = $tag[0][HandlerCompilerPass::HANDLER_NAME_ATTRIBUTE];
40
41
            // remove all tasks with $id and not completed
42
            foreach ($tags as $attributes) {
43
                $interval = $attributes[self::INTERVAL_ATTRIBUTE];
44
                $workload = isset($attributes[self::WORKLOAD_ATTRIBUTE]) ? $attributes[self::WORKLOAD_ATTRIBUTE] : null;
45
                $key = isset($attributes[self::KEY_ATTRIBUTE]) ? $attributes[self::KEY_ATTRIBUTE] : null;
46
47
                if (!$key) {
48
                    $key = $handler . '_' . $interval . '_' . serialize($workload);
49
                }
50
51
                $schedulerDefinition->addMethodCall(
52
                    self::CREATE_FUNCTION_NAME,
53
                    [
54
                        $handler,
55
                        $workload,
56
                        $interval,
57
                        $key,
58
                    ]
59
                );
60
            }
61
        }
62
    }
63
}
64