Completed
Pull Request — master (#1266)
by
unknown
01:36
created

Init::resolvePath()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 29
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7.3329

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 10
cts 15
cp 0.6667
rs 8.439
c 0
b 0
f 0
cc 6
eloc 12
nc 12
nop 1
crap 7.3329
1
<?php
2
/**
3
 * Phinx
4
 *
5
 * (The MIT license)
6
 * Copyright (c) 2015 Rob Morgan
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated * documentation files (the "Software"), to
10
 * deal in the Software without restriction, including without limitation the
11
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12
 * sell copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
 * IN THE SOFTWARE.
25
 *
26
 * @package    Phinx
27
 * @subpackage Phinx\Console
28
 */
29
namespace Phinx\Console\Command;
30
31
use InvalidArgumentException;
32
use RuntimeException;
33
use Symfony\Component\Console\Command\Command;
34
use Symfony\Component\Console\Input\InputArgument;
35
use Symfony\Component\Console\Input\InputInterface;
36
use Symfony\Component\Console\Output\OutputInterface;
37
38
class Init extends Command
39
{
40
    const FILE_NAME = 'phinx.yml';
41 35
42
    /**
43 35
     * {@inheritdoc}
44 35
     */
45 35
    protected function configure()
46 35
    {
47 35
        $this->setName('init')
48 35
            ->setDescription('Initialize the application for Phinx')
49
            ->addArgument('path', InputArgument::OPTIONAL, 'Which path should we initialize for Phinx?')
50 35
            ->setHelp(sprintf(
51 35
                '%sInitializes the application for Phinx%s',
52
                PHP_EOL,
53
                PHP_EOL
54
            ));
55
    }
56
57
    /**
58
     * Initializes the application.
59
     *
60
     * @param InputInterface  $input
61
     * @param OutputInterface $output
62 2
     *
63
     * @throws RuntimeException
64
     * @throws InvalidArgumentException
65 2
     * @return void
66
     */
67 2
    protected function execute(InputInterface $input, OutputInterface $output)
68
    {
69
        $path = $this->resolvePath($input);
70
        $this->writeConfig($path);
71 2
72
        $output->writeln("<info>created</info> {$path}");
73 2
    }
74
75
    /**
76
     * Return valid $path for Phing's config file.
77
     *
78
     * @param InputInterface $input
79
     *
80
     * @return string
81 2
     */
82 2
    protected function resolvePath(InputInterface $input)
83
    {
84 2
        // get the migration path from the config
85 1
        $path = $input->getArgument('path');
86 1
87
        // Fallback
88 1
        if (empty($path)) {
89
            $path = getcwd() . DIRECTORY_SEPARATOR . self::FILE_NAME;
90
        }
91
92 1
        // Adding file name if necessary
93
        if (is_dir($path)) {
94
            $path .= DIRECTORY_SEPARATOR . self::FILE_NAME;
95 1
        }
96
97
        // Check if path is available
98 1
        $dirname = dirname($path);
99
        if (is_dir($dirname) && !is_file($path)) {
100
            return $path;
101
        }
102
103
        // Path is valid, but file already exists
104
        if (is_file($path)) {
105 1
            throw new InvalidArgumentException('Config file already exists.');
106 1
        }
107
108
        // Dir is invalid
109
        throw new InvalidArgumentException('Invalid path for config file.');
110
    }
111
112
    /**
113
     * Writes Phing's config in provided $path
114
     *
115
     * @param string $path
116
     */
117
    protected function writeConfig($path)
118
    {
119
        // load the config template
120
        if (is_dir(__DIR__ . '/../../../data/Phinx')) {
121
            $contents = file_get_contents(__DIR__ . '/../../../data/Phinx/phinx.yml');
122
        } else {
123
            $contents = file_get_contents(__DIR__ . '/../../../../phinx.yml');
124
        }
125
126
        if (file_put_contents($path, $contents) === false) {
127
            throw new RuntimeException(sprintf(
128
                'The file "%s" could not be written to',
129
                $path
130
            ));
131
        }
132
    }
133
}
134