AttemptsCounter::addAction()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 3
rs 10
1
<?php
2
3
namespace kdn\attemptsCounter;
4
5
/**
6
 * Class AttemptsCounter.
7
 * @package kdn\attemptsCounter
8
 */
9
class AttemptsCounter
10
{
11
    /**
12
     * @var array<string, Action> actions;
13
     * keys are action names, values are action objects
14
     */
15
    protected $actions = [];
16
17
    /**
18
     * Adds a new action to the counter object.
19
     * @param Action $action the action
20
     * @param bool $overwrite whether to overwrite existing action data in case of name conflict
21
     * @return $this the counter object.
22
     */
23 3
    public function addAction($action, $overwrite = true)
24
    {
25 3
        if (array_key_exists($action->getName(), $this->actions) && !$overwrite) {
26 1
            return $this;
27
        }
28
29 3
        $this->actions[$action->getName()] = $action;
30
31 3
        return $this;
32
    }
33
34
    /**
35
     * Get an action by name.
36
     * @param int|string $actionName the action name
37
     * @return Action the action object.
38
     * @throws Exception
39
     */
40 4
    public function getAction($actionName)
41
    {
42 4
        if (!array_key_exists($actionName, $this->actions)) {
43 1
            throw new Exception('Unknown action name "' . $actionName . '" specified.');
44
        }
45
46 3
        return $this->actions[$actionName];
47
    }
48
}
49