Passed
Push — master ( 3a6da4...ca4c8c )
by Sebastian
03:44
created

Editor::checkChange()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace CaptainHook\App\Runner\Config;
11
12
use CaptainHook\App\Config;
13
use CaptainHook\App\Exception;
14
use CaptainHook\App\Hook\Util;
15
use CaptainHook\App\Runner;
16
17
/**
18
 * Class Editor
19
 *
20
 * @package CaptainHook
21
 * @author  Sebastian Feldmann <[email protected]>
22
 * @link    https://github.com/captainhookphp/captainhook
23
 * @since   Class available since Release 4.1.1
24
 */
25
class Editor extends Runner
26
{
27
    /**
28
     * @var string
29
     */
30
    private $hookToEdit;
31
32
    /**
33
     * The name of the change that will be applied to the configuration
34
     *
35
     * @var string
36
     */
37
    private $change;
38
39
    /**
40
     * Set the hook that should be changed
41
     *
42
     * @param  string $hook
43
     * @return \CaptainHook\App\Runner\Config\Editor
44
     * @throws \CaptainHook\App\Exception\InvalidHookName
45
     */
46 7
    public function setHook(string $hook) : Editor
47
    {
48 7
        if (!Util::isValid($hook)) {
49 1
            throw new Exception\InvalidHookName('Invalid hook name \'' . $hook . '\'');
50
        }
51 6
        $this->hookToEdit = $hook;
52 6
        return $this;
53
    }
54
55
    /**
56
     * Set the name of the change that should be performed on the configuration
57
     *
58
     * @param  string $change
59
     * @return \CaptainHook\App\Runner\Config\Editor
60
     */
61 6
    public function setChange(string $change) : Editor
62
    {
63 6
        $this->change = $change;
64 6
        return $this;
65
    }
66
67
    /**
68
     * Executes the Runner
69
     *
70
     * @return void
71
     * @throws \RuntimeException
72
     */
73 7
    public function run(): void
74
    {
75 7
        if (!$this->config->isLoadedFromFile()) {
76 1
            throw new \RuntimeException('No configuration to edit');
77
        }
78
79 6
        $this->checkHook();
80 5
        $this->checkChange();
81
82 4
        $change = $this->createChange();
83 3
        $change->applyTo($this->config);
84 3
        Config\Util::writeToDisk($this->config);
85
86 3
        $this->io->write('Configuration successfully updated');
87 3
    }
88
89
    /**
90
     * Create an config edit command
91
     *
92
     * @return \CaptainHook\App\Runner\Config\Change
93
     * @throws \RuntimeException
94
     */
95 4
    private function createChange() : Change
96
    {
97 4
        $changeClass = '\\CaptainHook\\App\\Runner\\Config\\Change\\' . $this->change;
98 4
        if (!class_exists($changeClass)) {
99 1
            throw new \RuntimeException('Invalid change requested');
100
        }
101
102 3
        return new $changeClass($this->io, $this->hookToEdit);
103
    }
104
105
    /**
106
     * Makes sure a hook is selected
107
     *
108
     * @return void
109
     * @throws \RuntimeException
110
     */
111 6
    private function checkHook() : void
112
    {
113 6
        if (empty($this->hookToEdit)) {
114 1
            throw new \RuntimeException('No hook set');
115
        }
116 5
    }
117
118
    /**
119
     * Makes sure a command is set
120
     *
121
     * @return void
122
     * @throws \RuntimeException
123
     */
124 5
    private function checkChange() : void
125
    {
126 5
        if (empty($this->change)) {
127 1
            throw new \RuntimeException('No change set');
128
        }
129 4
    }
130
}
131