TaskRegistry::findAll()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of bldr
5
 *
6
 * (c) Aaron Scherer <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE
10
 */
11
12
namespace Bldr\Registry;
13
14
use Bldr\Block\Core\Task\AbstractTask;
15
use Bldr\Exception\TaskNotFoundException;
16
use Bldr\Task\TaskInterface;
17
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18
19
/**
20
 * @author Aaron Scherer <[email protected]>
21
 */
22
class TaskRegistry
23
{
24
    /**
25
     * @type TaskInterface[]|AbstractTask[] $tasks
26
     */
27
    private $tasks;
28
29
    /**
30
     * @param EventDispatcherInterface       $dispatcher
31
     * @param TaskInterface[]|AbstractTask[] $tasks
32
     */
33
    public function __construct(EventDispatcherInterface $dispatcher, array $tasks)
34
    {
35
        foreach ($tasks as $task) {
36
            if ($task instanceof AbstractTask) {
37
                $task->configure();
38
            }
39
40
            if (method_exists($task, 'setEventDispatcher')) {
41
                $task->setEventDispatcher($dispatcher);
42
            }
43
44
            $this->tasks[$task->getName()] = $task;
45
        }
46
    }
47
48
    /**
49
     * @param string $type
50
     *
51
     * @throws TaskNotFoundException
52
     * @return TaskInterface|AbstractTask
53
     */
54
    public function findTaskByType($type)
55
    {
56
        foreach ($this->tasks as $task) {
57
            if ($task->getName() === $type) {
58
                return $task;
59
            }
60
        }
61
62
        throw new TaskNotFoundException($type);
63
    }
64
65
    /**
66
     * @return AbstractTask[]|TaskInterface[]
67
     */
68
    public function findAll()
69
    {
70
        return $this->tasks;
71
    }
72
}
73