Completed
Pull Request — master (#13)
by Marijan
17:08
created

ExecutorPool::getExecutor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 8
cp 0
cc 2
eloc 7
nc 2
nop 1
crap 6
1
<?php
2
3
namespace TreeHouse\WorkerBundle;
4
5
use InvalidArgumentException;
6
use OutOfBoundsException;
7
use TreeHouse\WorkerBundle\Executor\ExecutorInterface;
8
9
class ExecutorPool
10
{
11
    /**
12
     * @var ExecutorInterface[]
13
     */
14
    protected $executors = [];
15
16
    /**
17
     * @param string $action
18
     *
19
     * @return bool
20
     */
21
    public function hasExecutor($action)
22
    {
23
        return array_key_exists($action, $this->executors);
24
    }
25
26
    /**
27
     * Add an executor.
28
     *
29
     * @param ExecutorInterface $executor
30
     *
31
     * @throws InvalidArgumentException
32
     */
33 View Code Duplication
    public function addExecutor(ExecutorInterface $executor)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        $action = $executor->getName();
36
37
        if ($this->hasExecutor($action)) {
38
            throw new InvalidArgumentException(
39
                sprintf(
40
                    'There is already an executor registered for action "%s".',
41
                    $action
42
                )
43
            );
44
        }
45
46
        $this->executors[$action] = $executor;
47
    }
48
49
    /**
50
     * Returns a registered executor for given action.
51
     *
52
     * @param string $action
53
     *
54
     * @return ExecutorInterface
55
     *
56
     * @throws OutOfBoundsException
57
     */
58 View Code Duplication
    public function getExecutor($action)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        if (!$this->hasExecutor($action)) {
61
            throw new OutOfBoundsException(
62
                sprintf(
63
                    'There is no executor registered for action "%s".',
64
                    $action
65
                )
66
            );
67
        }
68
69
        return $this->executors[$action];
70
    }
71
72
    /**
73
     * @return ExecutorInterface[]
74
     */
75
    public function getExecutors()
76
    {
77
        return $this->executors;
78
    }
79
}
80