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

ExecutorPool   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 39.44 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 28
loc 71
rs 10
c 0
b 0
f 0
ccs 0
cts 22
cp 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hasExecutor() 0 4 1
A addExecutor() 15 15 2
A getExecutor() 13 13 2
A getExecutors() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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