ActionHandlerPool::getHandlerForAction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * PHP version 5.4 and 8
5
 *
6
 * @category  Action
7
 * @package   Payever\ThirdParty
8
 * @author    payever GmbH <[email protected]>
9
 * @author    Hennadii.Shymanskyi <[email protected]>
10
 * @copyright 2017-2021 payever GmbH
11
 * @license   MIT <https://opensource.org/licenses/MIT>
12
 * @link      https://docs.payever.org/shopsystems/api/getting-started
13
 */
14
15
namespace Payever\ExternalIntegration\ThirdParty\Action;
16
17
/**
18
 * @SuppressWarnings(PHPMD.MissingImport)
19
 */
20
class ActionHandlerPool
21
{
22
    /** @var ActionHandlerInterface[] */
23
    protected $handlers;
24
25
    /**
26
     * @param ActionHandlerInterface[] $handlers
27
     */
28
    public function __construct(array $handlers = [])
29
    {
30
        $this->handlers = [];
31
        foreach ($handlers as $handler) {
32
            $this->registerActionHandler($handler);
33
        }
34
    }
35
36
    /**
37
     * @param ActionHandlerInterface $handler
38
     *
39
     * @return static
40
     */
41
    public function registerActionHandler(ActionHandlerInterface $handler)
42
    {
43
        $this->handlers[$handler->getSupportedAction()] = $handler;
44
45
        return $this;
46
    }
47
48
    /**
49
     * @param string $action on of @see {ActionEnum}
50
     *
51
     * @return ActionHandlerInterface
52
     * @throws \RuntimeException when can't find corresponding handler
53
     */
54
    public function getHandlerForAction($action)
55
    {
56
        if (isset($this->handlers[$action])) {
57
            return $this->handlers[$action];
58
        }
59
60
        throw new \RuntimeException(sprintf('No handler registered for %s action', $action));
61
    }
62
}
63