DispatcherFinder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Deployee\Plugins\Deploy\Dispatcher;
4
5
use Deployee\Components\Container\ContainerInterface;
6
use Deployee\Plugins\Deploy\Definitions\Tasks\TaskDefinitionInterface;
7
use Deployee\Plugins\Deploy\Exception\DispatcherException;
8
9
class DispatcherFinder
10
{
11
    /**
12
     * @var ContainerInterface
13
     */
14
    private $container;
15
16
    /**
17
     * @param ContainerInterface $container
18
     */
19
    public function __construct(ContainerInterface $container)
20
    {
21
        $this->container = $container;
22
    }
23
24
25
    /**
26
     * @param TaskDefinitionInterface $taskDefinition
27
     * @return TaskDefinitionDispatcherInterface
28
     * @throws DispatcherException
29
     */
30
    public function findTaskDispatcherByDefinition(TaskDefinitionInterface $taskDefinition): TaskDefinitionDispatcherInterface
31
    {
32
        /* @var DispatcherCollection $dispatcherCollection */
33
        $dispatcherCollection = $this->container->get(DispatcherCollection::class);
34
35
        foreach($dispatcherCollection->toArray() as $dispatcher){
36
            if($dispatcher->canDispatchTaskDefinition($taskDefinition) === true){
37
                return $dispatcher;
38
            }
39
        }
40
41
        throw new DispatcherException(sprintf('No dispatcher found for %s task', get_class($taskDefinition)));
42
    }
43
}