Passed
Push — master ( 7bee4d...457c92 )
by Sebastian
01:54
created

Creator::ensureForce()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

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