Passed
Branch master (776013)
by payever
03:51
created

ActionHandlerPool   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 10
dl 0
loc 44
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getHandlerForAction() 0 7 2
A registerActionHandler() 0 5 1
1
<?php
2
/**
3
 * PHP version 5.4 and 7
4
 *
5
 * @package   Payever\ThirdParty
6
 * @author    Hennadii.Shymanskyi <[email protected]>
7
 * @copyright 2017-2019 payever GmbH
8
 * @license   MIT <https://opensource.org/licenses/MIT>
9
 */
10
11
namespace Payever\ExternalIntegration\ThirdParty\Action;
12
13
/**
14
 * PHP version 5.4 and 7
15
 *
16
 * @package   Payever\ThirdParty
17
 * @author    payever GmbH <[email protected]>
18
 * @author    Hennadii.Shymanskyi <[email protected]>
19
 * @copyright 2017-2019 payever GmbH
20
 * @license   MIT <https://opensource.org/licenses/MIT>
21
 */
22
class ActionHandlerPool
23
{
24
    /** @var ActionHandlerInterface[] */
25
    protected $handlers;
26
27
    /**
28
     * ActionHandlerPool constructor.
29
     * @param ActionHandlerInterface[] $handlers
30
     */
31
    public function __construct(array $handlers = array())
32
    {
33
        $this->handlers = array();
34
35
        foreach ($handlers as $handler) {
36
            $this->registerActionHandler($handler);
37
        }
38
    }
39
40
    /**
41
     * @param ActionHandlerInterface $handler
42
     *
43
     * @return static
44
     */
45
    public function registerActionHandler(ActionHandlerInterface $handler)
46
    {
47
        $this->handlers[$handler->getSupportedAction()] = $handler;
48
49
        return $this;
50
    }
51
52
    /**
53
     * @param string $action on of @see {Action}
54
     *
55
     * @return ActionHandlerInterface
56
     *
57
     * @throws \RuntimeException when can't find corresponding handler
58
     */
59
    public function getHandlerForAction($action)
60
    {
61
        if (isset($this->handlers[$action])) {
62
            return $this->handlers[$action];
63
        }
64
65
        throw new \RuntimeException(sprintf("No handler registered for %s action", $action));
66
    }
67
}
68