Completed
Pull Request — master (#37)
by Douglas
04:50
created

AbstractCommand::outputHeader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 9
ccs 0
cts 8
cp 0
crap 6
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the AntiMattr MongoDB Migrations Library, a library by Matthew Fitzgerald.
5
 *
6
 * (c) 2014 Matthew Fitzgerald
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 AntiMattr\MongoDB\Migrations\Tools\Console\Command;
13
14
use AntiMattr\MongoDB\Migrations\Configuration\Configuration;
15
use AntiMattr\MongoDB\Migrations\OutputWriter;
16
use Doctrine\MongoDB\Connection;
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
/**
23
 * @author Matthew Fitzgerald <[email protected]>
24
 */
25
abstract class AbstractCommand extends Command
26
{
27
    /**
28
     * @var \AntiMattr\MongoDB\Migrations\Configuration\Configuration
29
     */
30
    private $configuration;
31
32
    /**
33
     * configure.
34
     */
35 18
    protected function configure()
36
    {
37 18
        $this->addOption(
38 18
            'configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a migrations configuration file.'
39
        );
40 18
        $this->addOption(
41 18
            'db-configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a database connection configuration file.'
42
        );
43 18
    }
44
45
    /**
46
     * @param \AntiMattr\MongoDB\Migrations\Configuration\Configuration $configuration
47
     * @param \Symfony\Component\Console\Output\OutputInterface         $output
48
     */
49
    protected function outputHeader(Configuration $configuration, OutputInterface $output)
50
    {
51
        $name = $configuration->getName();
52
        $name = $name ? $name : 'AntiMattr Database Migrations';
53
        $name = str_repeat(' ', 20) . $name . str_repeat(' ', 20);
54
        $output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>');
55
        $output->writeln('<question>' . $name . '</question>');
56
        $output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>');
57
        $output->writeln('');
58
    }
59
60
    /**
61
     * @param \AntiMattr\MongoDB\Migrations\Configuration\Configuration
62
     */
63 16
    public function setMigrationConfiguration(Configuration $config)
64
    {
65 16
        $this->configuration = $config;
66 16
    }
67
68
    /**
69
     * @param \Symfony\Component\Console\Output\InputInterface  $input
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Output\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
70
     * @param \Symfony\Component\Console\Output\OutputInterface $output
71
     *
72
     * @return \AntiMattr\MongoDB\Migrations\Configuration\Configuration
73
     */
74 16
    protected function getMigrationConfiguration(InputInterface $input, OutputInterface $output)
75
    {
76 16
        if (!$this->configuration) {
77
            $outputWriter = new OutputWriter(function ($message) use ($output) {
78
                return $output->writeln($message);
79
            });
80
81
            if ($this->getApplication()->getHelperSet()->has('dm')) {
82
                // Doctrine\MongoDB\Connection
83
                $conn = $this->getHelper('dm')->getDocumentManager()->getConnection();
84
            } elseif ($input->getOption('db-configuration')) {
85
                if (!file_exists($input->getOption('db-configuration'))) {
86
                    throw new \InvalidArgumentException('The specified connection file is not a valid file.');
87
                }
88
89
                $params = include $input->getOption('db-configuration');
90
                if (!is_array($params)) {
91
                    throw new \InvalidArgumentException('The connection file has to return an array with database configuration parameters.');
92
                }
93
                $conn = $this->createConnection($params);
94
            } else {
95
                throw new \InvalidArgumentException('You have to specify a --db-configuration file or pass a Database Connection as a dependency to the Migrations.');
96
            }
97
98
            if ($input->getOption('configuration')) {
99
                $info = pathinfo($input->getOption('configuration'));
100
                $namespace = 'AntiMattr\MongoDB\Migrations\Configuration';
101
                $class = 'xml' === $info['extension'] ? 'XmlConfiguration' : 'YamlConfiguration';
102
                $class = sprintf('%s\%s', $namespace, $class);
103
                $configuration = new $class($conn, $outputWriter);
104
                $configuration->load($input->getOption('configuration'));
105
            } else {
106
                $configuration = new Configuration($conn, $outputWriter);
107
            }
108
            $this->configuration = $configuration;
109
        }
110
111 16
        return $this->configuration;
112
    }
113
114
    /**
115
     * @param array $params
116
     *
117
     * @return \Doctrine\MongoDB\Connection
118
     */
119
    protected function createConnection($params)
120
    {
121
        $credentials = '';
122
        if (isset($params['password'])) {
123
            $credentials = ':' . $params['password'];
124
        }
125
        if (isset($params['user'])) {
126
            $credentials = $params['user'] . $credentials . '@';
127
        }
128
129
        $database = '';
130
        if (isset($params['dbname'])) {
131
            $database = '/' . $params['dbname'];
132
        }
133
134
        $server = sprintf(
135
            'mongodb://%s%s:%s%s',
136
            $credentials,
137
            (isset($params['host']) ? $params['host'] : 'localhost'),
138
            (isset($params['port']) ? $params['port'] : '27017'),
139
            $database
140
        );
141
142
        $options = [];
143
        if (!empty($params['options'])) {
144
            $options = $params['options'];
145
        }
146
147
        return new Connection($server, $options);
148
    }
149
}
150