ScheduleRegistry   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 80
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setReader() 0 4 1
A addTask() 0 4 1
A getSchedules() 0 10 2
A getAnnotation() 0 11 1
A guardAgainstInvalidAnnotation() 0 12 2
1
<?php
2
3
namespace Glooby\TaskBundle\Schedule;
4
5
use Glooby\TaskBundle\Annotation\Schedule;
6
use Doctrine\Common\Annotations\Reader;
7
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
8
9
/**
10
 * @author Emil Kilhage
11
 */
12
class ScheduleRegistry
13
{
14
    const ANNOTATION_CLASS = Schedule::class;
15
16
    use ContainerAwareTrait;
17
18
    /**
19
     * @var array
20
     */
21
    private $tasks = [];
22
23
    /**
24
     * @var Reader
25
     */
26
    private $reader;
27
28
    /**
29
     * @param Reader $reader
30
     */
31
    public function setReader(Reader $reader)
32
    {
33
        $this->reader = $reader;
34
    }
35
36
    /**
37
     * @param string $id
38
     */
39
    public function addTask($id)
40
    {
41
        $this->tasks[] = $id;
42
    }
43
44
    /**
45
     * @return Schedule[]
46
     */
47
    public function getSchedules()
48
    {
49
        $schedules = [];
50
51
        foreach ($this->tasks as $taskId) {
52
            $schedules[$taskId] = $this->getAnnotation($taskId);
53
        }
54
55
        return $schedules;
56
    }
57
58
    /**
59
     * @param string $taskId
60
     *
61
     * @return array
62
     */
63
    private function getAnnotation($taskId)
64
    {
65
        $task = $this->container->get($taskId);
66
67
        $reflectionObject = new \ReflectionObject($task);
68
        $annotation = $this->reader->getClassAnnotation($reflectionObject, self::ANNOTATION_CLASS);
69
70
        $this->guardAgainstInvalidAnnotation($annotation, $task);
71
72
        return $annotation;
73
    }
74
75
    /**
76
     * @param $annotation
77
     * @param $task
78
     */
79
    private function guardAgainstInvalidAnnotation($annotation, $task)
80
    {
81
        if (!is_a($annotation, self::ANNOTATION_CLASS)) {
82
            throw new \InvalidArgumentException(
83
                sprintf(
84
                    'class %s is missing the Schedule annotation %s',
85
                    get_class($task),
86
                    self::ANNOTATION_CLASS
87
                )
88
            );
89
        }
90
    }
91
}
92