ActionTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 61
ccs 0
cts 15
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A actionMatch() 0 8 2
A findAction() 0 12 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember/
7
 */
8
9
namespace flipbox\craft\ember\filters;
10
11
use craft\helpers\ArrayHelper;
12
13
/**
14
 * @author Flipbox Factory <[email protected]>
15
 * @since 2.0.0
16
 */
17
trait ActionTrait
18
{
19
    /**
20
     * @var array this property defines the a mapping for each action.
21
     * For each action that should only support limited set of values
22
     * you add a value with the action id as array key and an array value of
23
     * allowed status codes (e.g. 'create' => 'value1', 'delete' => 'value2').
24
     * If an action is not defined the default action property will be used.
25
     *
26
     * You can use `'*'` to stand for all actions. When an action is explicitly
27
     * specified, it takes precedence over the specification given by `'*'`.
28
     *
29
     * For example,
30
     *
31
     * ```php
32
     * [
33
     *   'create' => 'value1',
34
     *   'update' => 'value2',
35
     *   '*' => 'value3',
36
     * ]
37
     * ```
38
     */
39
    public $actions = [];
40
41
    /**
42
     * The default action value
43
     *
44
     * @var bool
45
     */
46
    public $default = null;
47
48
    /**
49
     * @param string $action
50
     * @return bool
51
     */
52
    protected function actionMatch(string $action): bool
53
    {
54
        if ($this->findAction($action) !== null) {
55
            return true;
56
        }
57
58
        return false;
59
    }
60
61
    /**
62
     * @param string $action
63
     * @return string|null
64
     */
65
    protected function findAction(string $action)
66
    {
67
        // Default
68
        $value = ArrayHelper::getValue($this->actions, '*', $this->default);
69
70
        // Look by specific action
71
        if (isset($this->actions[$action])) {
72
            $value = $this->actions[$action];
73
        }
74
75
        return $value;
76
    }
77
}
78