Completed
Push — feature/EVO-1283-remove-bundle ( 4a3045 )
by Lucas
09:12
created

MongodbMigrateCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 9
c 2
b 0
f 1
lcom 1
cbo 8
dl 0
loc 118
ccs 0
cts 47
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A configure() 0 6 1
B execute() 0 35 5
A getConfiguration() 0 19 2
1
<?php
2
/**
3
 * graviton:mongodb:migrations:execute command
4
 */
5
6
namespace Graviton\MigrationBundle\Command;
7
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Input\ArrayInput;
12
use Symfony\Component\Finder\Finder;
13
use Graviton\MigrationBundle\Command\Helper\DocumentManager as DocumentManagerHelper;
14
use AntiMattr\MongoDB\Migrations\OutputWriter;
15
16
/**
17
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
18
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
19
 * @link     http://swisscom.ch
20
 */
21
class MongodbMigrateCommand extends Command
22
{
23
    /**
24
     * @var Finder
25
     */
26
    private $finder;
27
28
    /**
29
     * @var DocumentManagerHelper
30
     */
31
    private $documentManager;
32
33
    /**
34
     * @var string
35
     */
36
    private $databaseName;
37
38
    /**
39
     * @param Finder                $finder          finder that finds configs
40
     * @param DocumentManagerHelper $documentManager dm helper to get access to db in command
41
     * @param string                $databaseName    name of database where data is found in
42
     */
43
    public function __construct(Finder $finder, DocumentManagerHelper $documentManager, $databaseName)
44
    {
45
        $this->finder = $finder;
46
        $this->documentManager = $documentManager;
47
        $this->databaseName = $databaseName;
48
49
        parent::__construct();
50
    }
51
52
    /**
53
     * setup command
54
     *
55
     * @return void
56
     */
57
    protected function configure()
58
    {
59
        parent::configure();
60
61
        $this->setName('graviton:mongodb:migrate');
62
    }
63
64
    /**
65
     * call execute on found commands
66
     *
67
     * @param InputInterface  $input  user input
68
     * @param OutputInterface $output command output
69
     *
70
     * @return void
71
     */
72
    public function execute(InputInterface $input, OutputInterface $output)
73
    {
74
        $base = strpos(getcwd(), 'vendor/') === false ? getcwd() :  getcwd() . '/../../../../';
75
        $this->finder->in($base)->path('Resources/config')->name('/migrations.(xml|yml)/')->files();
76
77
        foreach ($this->finder as $file) {
78
            if (!$file->isFile()) {
79
                continue;
80
            }
81
82
            $output->writeln('Found '.$file->getRelativePathname());
83
84
            $command = $this->getApplication()->find('mongodb:migrations:migrate');
85
86
            $helperSet = $command->getHelperSet();
87
            $helperSet->set($this->documentManager, 'dm');
88
            $command->setHelperSet($helperSet);
89
90
            $arguments = $input->getArguments();
91
            $arguments['command'] = 'mongodb:migrations:migrate';
92
            $arguments['--configuration'] = $file->getPathname();
93
94
            $command->setMigrationConfiguration($this->getConfiguration($file->getPathname(), $output));
0 ignored issues
show
Documentation introduced by
$output is of type object<Symfony\Component...Output\OutputInterface>, but the function expects a object<Graviton\MigrationBundle\Command\Output>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
96
            $migrateInput = new ArrayInput($arguments);
97
            $returnCode = $command->run($migrateInput, $output);
98
99
            if ($returnCode !== 0) {
100
                $output->writeln(
101
                    '<error>Calling mongodb:migrations:migrate failed for '.$file->getRelativePathname().'</error>'
102
                );
103
                return $returnCode;
104
            }
105
        }
106
    }
107
108
    /**
109
     * get configration object for migration script
110
     *
111
     * This is based on antromattr/mongodb-migartion code but extends it so we can inject
112
     * non local stuff centrally.
113
     *
114
     * @param string $filepath path to configuration file
115
     * @param Output $output   ouput interface need by config parser to do stuff
116
     *
117
     * @return AntiMattr\MongoDB\Migrations\Configuration\Configuration
118
     */
119
    private function getConfiguration($filepath, $output)
120
    {
121
        $outputWriter = new OutputWriter(
122
            function ($message) use ($output) {
123
                return $output->writeln($message);
124
            }
125
        );
126
127
        $info = pathinfo($filepath);
128
        $namespace = 'AntiMattr\MongoDB\Migrations\Configuration';
129
        $class = $info['extension'] === 'xml' ? 'XmlConfiguration' : 'YamlConfiguration';
130
        $class = sprintf('%s\%s', $namespace, $class);
131
        $configuration = new $class($this->documentManager->getDocumentManager()->getConnection(), $outputWriter);
132
        $configuration->load($filepath);
133
134
        $configuration->setMigrationsDatabaseName($this->databaseName);
135
136
        return $configuration;
137
    }
138
}
139