AttemptsCounter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 38
ccs 9
cts 9
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addAction() 0 9 3
A getAction() 0 7 2
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