InitHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
c 2
b 0
f 0
lcom 0
cbo 5
dl 0
loc 36
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 27 3
1
<?php
2
3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license. For more information, see
18
 * <http://www.doctrine-project.org>.
19
 */
20
21
namespace Baleen\Cli\CommandBus\Config;
22
23
/**
24
 * Handles the config:init command.
25
 *
26
 * @author Gabriel Somoza <[email protected]>
27
 */
28
class InitHandler
29
{
30
    /**
31
     * Handle an InitMessage. Creates an end-user configuration file using default values. If the file already exists
32
     * it simply exists without doing anything.
33
     *
34
     * @param InitMessage $message
35
     */
36
    public function handle(InitMessage $message)
37
    {
38
        $output = $message->getOutput();
39
        $configStorage = $message->getConfigStorage();
40
41
        if ($configStorage->isInitialized($message->getConfig())) {
42
            $output->writeln(sprintf(
43
                '%s is already initialised!',
44
                $message->getCliCommand()->getApplication()->getName()
45
            ));
46
47
            return;
48
        }
49
50
        $result = $configStorage->write($message->getConfig());
51
52
        if ($result !== false) {
53
            $msg = sprintf('Config file created at "<info>%s</info>".', $message->getConfig()->getFileName());
54
        } else {
55
            $msg = sprintf(
56
                '<error>Error: Could not create and write file "<info>%s</info>". '.
57
                'Please check file and directory permissions.</error>',
58
                $message->getConfig()->getFileName()
59
            );
60
        }
61
        $output->writeln($msg);
62
    }
63
}
64