Creator   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 158
ccs 39
cts 39
cp 1
rs 10
c 1
b 0
f 0
wmc 16

10 Methods

Rating   Name   Duplication   Size   Complexity  
A ensureForce() 0 4 3
A extend() 0 4 2
A isExtending() 0 3 1
A run() 0 12 1
A force() 0 4 1
A getHookSetup() 0 5 2
A advanced() 0 4 1
A getConfigToManipulate() 0 9 2
A setExecutable() 0 4 1
A getExecutable() 0 3 2
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\Runner\Config;
13
14
use CaptainHook\App\Config;
15
use CaptainHook\App\Runner;
16
use RuntimeException;
17
18
/**
19
 * Class Configurator
20
 *
21
 * @package CaptainHook
22
 * @author  Sebastian Feldmann <[email protected]>
23
 * @link    https://github.com/captainhook-git/captainhook
24
 * @since   Class available since Release 0.9.0
25
 */
26
class Creator extends Runner
27
{
28
    /**
29
     * Force mode
30
     *
31
     * @var bool
32
     */
33
    private bool $force = false;
34
35
    /**
36
     * Extend existing config or create new one
37
     *
38
     * @var string
39
     */
40
    private string $mode = 'create';
41
42
    /**
43
     * Use express setup mode
44
     *
45
     * @var bool
46
     */
47
    private bool $advanced = false;
48
49
    /**
50
     * Path to the currently executed 'binary'
51
     *
52
     * @var string
53
     */
54
    protected string $executable = '';
55
56
    /**
57
     * Execute the configurator
58
     *
59
     * @return void
60
     */
61 5
    public function run(): void
62
    {
63 5
        $config = $this->getConfigToManipulate();
64 4
        $setup  = $this->getHookSetup();
65
66 4
        $setup->configureHooks($config);
67 4
        Config\Util::writeToDisk($config);
68
69 3
        $this->io->write(
70 3
            [
71 3
                '<info>Configuration created successfully</info>',
72 3
                'Run <comment>\'' . $this->getExecutable() . ' install\'</comment> to activate your hook configuration',
73 3
            ]
74 3
        );
75
    }
76
77
    /**
78
     * Force mode setter
79
     *
80
     * @param  bool $force
81
     * @return \CaptainHook\App\Runner\Config\Creator
82
     */
83 2
    public function force(bool $force): Creator
84
    {
85 2
        $this->force = $force;
86 2
        return $this;
87
    }
88
89
    /**
90
     * Set configuration mode
91
     *
92
     * @param  bool $extend
93
     * @return \CaptainHook\App\Runner\Config\Creator
94
     */
95 4
    public function extend(bool $extend): Creator
96
    {
97 4
        $this->mode = $extend ? 'extend' : 'create';
98 4
        return $this;
99
    }
100
101
    /**
102
     * Set configuration speed
103
     *
104
     * @param  bool $advanced
105
     * @return \CaptainHook\App\Runner\Config\Creator
106
     */
107 5
    public function advanced(bool $advanced): Creator
108
    {
109 5
        $this->advanced = $advanced;
110 5
        return $this;
111
    }
112
113
    /**
114
     * Set the currently executed 'binary'
115
     *
116
     * @param  string $executable
117
     * @return \CaptainHook\App\Runner\Config\Creator
118
     */
119 2
    public function setExecutable(string $executable): Creator
120
    {
121 2
        $this->executable = $executable;
122 2
        return $this;
123
    }
124
125
    /**
126
     * Return config to handle
127
     *
128
     * @return \CaptainHook\App\Config
129
     */
130 5
    public function getConfigToManipulate(): Config
131
    {
132 5
        if (!$this->isExtending()) {
133
            // make sure the force option is set if the configuration file exists
134 3
            $this->ensureForce();
135
            // create a blank configuration to overwrite the old one
136 2
            return new Config($this->config->getPath());
137
        }
138 2
        return $this->config;
139
    }
140
141
    /**
142
     * Return the setup handler to ask the user questions
143
     *
144
     * @return \CaptainHook\App\Runner\Config\Setup
145
     */
146 4
    private function getHookSetup(): Setup
147
    {
148 4
        return $this->advanced
149 2
            ? new Setup\Advanced($this->io)
150 4
            : new Setup\Express($this->io);
151
    }
152
153
    /**
154
     * Should the config file be extended
155
     *
156
     * @return bool
157
     */
158 5
    private function isExtending(): bool
159
    {
160 5
        return 'extend' === $this->mode;
161
    }
162
163
    /**
164
     * Make sure force mode is set if config file exists
165
     *
166
     * @return void
167
     * @throws \RuntimeException
168
     */
169 3
    private function ensureForce(): void
170
    {
171 3
        if ($this->config->isLoadedFromFile() && !$this->force) {
172 1
            throw new RuntimeException('Configuration file exists, use -f to overwrite, or -e to extend');
173
        }
174
    }
175
176
    /**
177
     * Return path to currently executed 'binary'
178
     *
179
     * @return string
180
     */
181 3
    private function getExecutable(): string
182
    {
183 3
        return !empty($this->executable) ? $this->executable : 'vendor/bin/captainhook';
184
    }
185
}
186