Passed
Pull Request — master (#41)
by
unknown
03:10
created

AbstractCommand   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Test Coverage

Coverage 18.18%

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 144
ccs 12
cts 66
cp 0.1818
rs 10
c 0
b 0
f 0
wmc 18

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setMigrationConfiguration() 0 3 1
A getDatabaseConnection() 0 31 5
A configure() 0 7 1
B createConnection() 0 29 7
A getMigrationConfiguration() 0 21 2
A outputHeader() 0 9 2
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\Configuration\ConfigurationBuilder;
16
use AntiMattr\MongoDB\Migrations\OutputWriter;
17
use MongoDB\Client;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
/**
24
 * @author Matthew Fitzgerald <[email protected]>
25
 */
26
abstract class AbstractCommand extends Command
27
{
28
    /**
29
     * @var \AntiMattr\MongoDB\Migrations\Configuration\Configuration
30
     */
31
    private $configuration;
32
33
    /**
34
     * configure.
35
     */
36 18
    protected function configure()
37
    {
38 18
        $this->addOption(
39 18
            'configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a migrations configuration file.'
40
        );
41 18
        $this->addOption(
42 18
            'db-configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a database connection configuration file.'
43
        );
44 18
    }
45
46
    /**
47
     * @param \AntiMattr\MongoDB\Migrations\Configuration\Configuration $configuration
48
     * @param \Symfony\Component\Console\Output\OutputInterface         $output
49
     */
50
    protected function outputHeader(Configuration $configuration, OutputInterface $output)
51
    {
52
        $name = $configuration->getName();
53
        $name = $name ? $name : 'AntiMattr Database Migrations';
54
        $name = str_repeat(' ', 20) . $name . str_repeat(' ', 20);
55
        $output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>');
56
        $output->writeln('<question>' . $name . '</question>');
57
        $output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>');
58
        $output->writeln('');
59
    }
60
61
    /**
62
     * @param \AntiMattr\MongoDB\Migrations\Configuration\Configuration
63
     */
64 16
    public function setMigrationConfiguration(Configuration $config)
65
    {
66 16
        $this->configuration = $config;
67 16
    }
68
69
    /**
70
     * @param \Symfony\Component\Console\Input\InputInterface   $input
71
     * @param \Symfony\Component\Console\Output\OutputInterface $output
72
     *
73
     * @return \AntiMattr\MongoDB\Migrations\Configuration\Configuration
74
     */
75 16
    protected function getMigrationConfiguration(
76
        InputInterface $input,
77
        OutputInterface $output
78
    ): Configuration {
79 16
        if (!$this->configuration) {
80
            $conn = $this->getDatabaseConnection($input);
81
82
            $outputWriter = new OutputWriter(function($message) use ($output) {
83
                return $output->writeln($message);
84
            });
85
86
            $migrationsConfigFile = $input->getOption('configuration');
87
88
            $this->configuration = ConfigurationBuilder::create()
89
                ->setConnection($conn)
90
                ->setOutputWriter($outputWriter)
91
                ->setOnDiskConfiguration($migrationsConfigFile)
92
                ->build();
93
        }
94
95 16
        return $this->configuration;
96
    }
97
98
    /**
99
     * @param InputInterface $input
100
     *
101
     * @return Client
102
     */
103
    protected function getDatabaseConnection(InputInterface $input): Client
104
    {
105
        // Default to document manager helper set
106
        if ($this->getApplication()->getHelperSet()->has('dm')) {
107
            return $this->getHelper('dm')
108
                ->getDocumentManager()
109
                ->getClient();
110
        }
111
112
        // PHP array file
113
        $dbConfiguration = $input->getOption('db-configuration');
114
115
        if (!$dbConfiguration) {
116
            throw new \InvalidArgumentException(
117
                'You have to specify a --db-configuration file or pass a Database Connection as a dependency to the Migrations.'
118
            );
119
        }
120
121
        if (!file_exists($dbConfiguration)) {
122
            throw new \InvalidArgumentException('The specified connection file is not a valid file.');
123
        }
124
125
        $dbConfigArr = include $dbConfiguration;
126
127
        if (!is_array($dbConfigArr)) {
128
            throw new \InvalidArgumentException(
129
                'The connection file has to return an array with database configuration parameters.'
130
            );
131
        }
132
133
        return $this->createConnection($dbConfigArr);
134
    }
135
136
    /**
137
     * @param array $params
138
     *
139
     * @return \MongoDB\Client
140
     */
141
    protected function createConnection($params)
142
    {
143
        $credentials = '';
144
        if (isset($params['password'])) {
145
            $credentials = ':' . $params['password'];
146
        }
147
        if (isset($params['user'])) {
148
            $credentials = $params['user'] . $credentials . '@';
149
        }
150
151
        $database = '';
152
        if (isset($params['dbname'])) {
153
            $database = '/' . $params['dbname'];
154
        }
155
156
        $server = sprintf(
157
            'mongodb://%s%s:%s%s',
158
            $credentials,
159
            (isset($params['host']) ? $params['host'] : 'localhost'),
160
            (isset($params['port']) ? $params['port'] : '27017'),
161
            $database
162
        );
163
164
        $options = [];
165
        if (!empty($params['options'])) {
166
            $options = $params['options'];
167
        }
168
169
        return new Client($server, $options);
170
    }
171
}
172