Passed
Push — feature/config-shorthands ( 330e98...92b3e0 )
by Sebastian
03:44
created

Hook   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 113
rs 10
c 0
b 0
f 0
ccs 22
cts 22
cp 1
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setEnabled() 0 3 1
A __construct() 0 4 1
A getName() 0 3 1
A isEnabled() 0 3 1
A getJsonData() 0 9 3
A hasActions() 0 3 1
A addAction() 0 4 2
A getActions() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Config;
13
14
/**
15
 * Class Hook
16
 *
17
 * @package CaptainHook
18
 * @author  Sebastian Feldmann <[email protected]>
19
 * @link    https://github.com/captainhook-git/captainhook
20
 * @since   Class available since Release 0.9.0
21
 * @internal
22
 */
23
class Hook
24
{
25
    /**
26
     * Hook name e.g. pre-commit
27
     *
28
     * @var string
29
     */
30
    private string $name;
31
32
    /**
33
     * Is hook enabled
34
     *
35
     * @var bool
36
     */
37
    private bool $isEnabled;
38
39
    /**
40
     * List of Actions
41
     *
42
     * @var \CaptainHook\App\Config\Action[]
43
     */
44
    private $actions = [];
45
46
    /**
47
     * Hook constructor
48
     *
49
     * @param string $name
50
     * @param bool   $enabled
51
     */
52 158
    public function __construct(string $name, bool $enabled = false)
53
    {
54 158
        $this->name      = $name;
55 158
        $this->isEnabled = $enabled;
56
    }
57
58
    /**
59
     * Name getter
60
     *
61
     * @return string
62
     */
63 28
    public function getName(): string
64
    {
65 28
        return $this->name;
66
    }
67
68
    /**
69
     * Enable or disable the hook
70
     *
71
     * @param  bool $enabled
72
     * @return void
73
     */
74 61
    public function setEnabled(bool $enabled): void
75
    {
76 61
        $this->isEnabled = $enabled;
77
    }
78
79
    /**
80
     * Is this hook enabled
81
     *
82
     * @return bool
83
     */
84 44
    public function isEnabled(): bool
85
    {
86 44
        return $this->isEnabled;
87
    }
88
89
    /**
90
     * Check if a hook config has actions
91
     *
92
     * @return bool
93
     */
94 12
    public function hasActions(): bool
95
    {
96 12
        return !empty($this->actions);
97
    }
98
99
    /**
100
     * Add an action to the list
101
     *
102
     * @param \CaptainHook\App\Config\Action ...$actions
103
     * @return void
104
     */
105 54
    public function addAction(Action ...$actions): void
106
    {
107 54
        foreach ($actions as $action) {
108 52
            $this->actions[] = $action;
109
        }
110
    }
111
112
    /**
113
     * Return the action list
114
     *
115
     * @return \CaptainHook\App\Config\Action[]
116
     */
117 29
    public function getActions(): array
118
    {
119 29
        return $this->actions;
120
    }
121
122
    /**
123
     * Return config data
124
     *
125
     * @return array<string, mixed>
126
     */
127 13
    public function getJsonData(): array
128
    {
129 13
        $config = ['enabled' => $this->isEnabled, 'actions' => []];
130 13
        foreach ($this->actions as $action) {
131 8
            if (!$action->isIncluded()) {
132 8
                $config['actions'][] = $action->getJsonData();
133
            }
134
        }
135 13
        return $config;
136
    }
137
}
138