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

Init::writeConfig()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 0
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 14
nc 5
nop 1
crap 20
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 \Symfony\Component\Console\Input\InputInterface   $input  Interface implemented by all input classes.
61
     * @param \Symfony\Component\Console\Output\OutputInterface $output Interface implemented by all output classes.
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 Phinx's config file.
77
     *
78
     * @param \Symfony\Component\Console\Input\InputInterface $input Interface implemented by all input classes.
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 (!$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 "$path" already exists.');
106 1
        }
107
108
        // Dir is invalid
109
        throw new InvalidArgumentException('Invalid path "$path" for config file.');
110
    }
111
112
    /**
113
     * Writes Phinx's config in provided $path
114
     *
115
     * @param string $path Config file's path.
116
     *
117
     * @throws \InvalidArgumentException
118
     * @throws \RuntimeException
119
     * @return void
120
     */
121
    protected function writeConfig($path)
122
    {
123
        // Check if dir is writable
124
        $dirname = dirname($path);
125
        if (!is_writable($dirname)) {
126
            throw new InvalidArgumentException(sprintf(
127
                'The directory "%s" is not writable',
128
                $dirname
129
            ));
130
        }
131
132
        // load the config template
133
        if (is_dir(__DIR__ . '/../../../data/Phinx')) {
134
            $contents = file_get_contents(__DIR__ . '/../../../data/Phinx/phinx.yml');
135
        } else {
136
            $contents = file_get_contents(__DIR__ . '/../../../../phinx.yml');
137
        }
138
139
        if (file_put_contents($path, $contents) === false) {
140
            throw new RuntimeException(sprintf(
141
                'The file "%s" could not be written to',
142
                $path
143
            ));
144
        }
145
    }
146
}
147