Completed
Push — feature/EVO-1283-remove-bundle ( 1f92e6...df5f55 )
by Lucas
08:11
created

injectContainerToMigrations()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.6667
cc 3
eloc 5
nc 3
nop 2
crap 12
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\DependencyInjection\ContainerInterface;
13
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
14
use Symfony\Component\Finder\Finder;
15
use Graviton\MigrationBundle\Command\Helper\DocumentManager as DocumentManagerHelper;
16
use AntiMattr\MongoDB\Migrations\OutputWriter;
17
18
/**
19
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
20
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
21
 * @link     http://swisscom.ch
22
 */
23
class MongodbMigrateCommand extends Command
24
{
25
    /**
26
     * @var ContainerInterface
27
     */
28
    private $container;
29
    
30
    /**
31
     * @var Finder
32
     */
33
    private $finder;
34
35
    /**
36
     * @var DocumentManagerHelper
37
     */
38
    private $documentManager;
39
40
    /**
41
     * @var string
42
     */
43
    private $databaseName;
44
45
    /**
46
     * @param ContainerInterface    $container       container instance for injecting into aware migrations
47
     * @param Finder                $finder          finder that finds configs
48
     * @param DocumentManagerHelper $documentManager dm helper to get access to db in command
49
     * @param string                $databaseName    name of database where data is found in
50
     */
51
    public function __construct(
52
        ContainerInterface $container,
53
        Finder $finder,
54
        DocumentManagerHelper $documentManager,
55
        $databaseName
56
    ) {
57
        $this->container = $container;
58
        $this->finder = $finder;
59
        $this->documentManager = $documentManager;
60
        $this->databaseName = $databaseName;
61
62
        parent::__construct();
63
    }
64
65
    /**
66
     * setup command
67
     *
68
     * @return void
69
     */
70
    protected function configure()
71
    {
72
        parent::configure();
73
74
        $this->setName('graviton:mongodb:migrate');
75
    }
76
77
    /**
78
     * call execute on found commands
79
     *
80
     * @param InputInterface  $input  user input
81
     * @param OutputInterface $output command output
82
     *
83
     * @return void
84
     */
85
    public function execute(InputInterface $input, OutputInterface $output)
86
    {
87
        $base = strpos(getcwd(), 'vendor/') === false ? getcwd() :  getcwd() . '/../../../../';
88
        $this->finder->in($base)->path('Resources/config')->name('/migrations.(xml|yml)/')->files();
89
90
        foreach ($this->finder as $file) {
91
            if (!$file->isFile()) {
92
                continue;
93
            }
94
95
            $output->writeln('Found '.$file->getRelativePathname());
96
97
            $command = $this->getApplication()->find('mongodb:migrations:migrate');
98
99
            $helperSet = $command->getHelperSet();
100
            $helperSet->set($this->documentManager, 'dm');
101
            $command->setHelperSet($helperSet);
102
103
            $configuration = $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...
104
            self::injectContainerToMigrations($this->container, $configuration->getMigrations());
105
            $command->setMigrationConfiguration($configuration);
106
107
            $arguments = $input->getArguments();
108
            $arguments['command'] = 'mongodb:migrations:migrate';
109
            $arguments['--configuration'] = $file->getPathname();
110
111
            $migrateInput = new ArrayInput($arguments);
112
            $returnCode = $command->run($migrateInput, $output);
113
114
            if ($returnCode !== 0) {
115
                $output->writeln(
116
                    '<error>Calling mongodb:migrations:migrate failed for '.$file->getRelativePathname().'</error>'
117
                );
118
                return $returnCode;
119
            }
120
        }
121
    }
122
123
    /**
124
     * get configration object for migration script
125
     *
126
     * This is based on antromattr/mongodb-migartion code but extends it so we can inject
127
     * non local stuff centrally.
128
     *
129
     * @param string $filepath path to configuration file
130
     * @param Output $output   ouput interface need by config parser to do stuff
131
     *
132
     * @return AntiMattr\MongoDB\Migrations\Configuration\Configuration
133
     */
134
    private function getConfiguration($filepath, $output)
135
    {
136
        $outputWriter = new OutputWriter(
137
            function ($message) use ($output) {
138
                return $output->writeln($message);
139
            }
140
        );
141
142
        $info = pathinfo($filepath);
143
        $namespace = 'AntiMattr\MongoDB\Migrations\Configuration';
144
        $class = $info['extension'] === 'xml' ? 'XmlConfiguration' : 'YamlConfiguration';
145
        $class = sprintf('%s\%s', $namespace, $class);
146
        $configuration = new $class($this->documentManager->getDocumentManager()->getConnection(), $outputWriter);
147
148
        // register databsae name before loading to ensure that loading does not fail
149
        $configuration->setMigrationsDatabaseName($this->databaseName);
150
151
        // load additional config from migrations.(yml|xml)
152
        $configuration->load($filepath);
153
154
        return $configuration;
155
    }
156
    
157
    /**
158
     * Injects the container to migrations aware of it
159
     *
160
     * @param ContainerInterface $container container to inject into container aware migrations
161
     * @param array              $versions  versions that might need injecting a container
162
     *
163
     * @return void
164
     */
165
    private static function injectContainerToMigrations(ContainerInterface $container, array $versions)
166
    {
167
        foreach ($versions as $version) {
168
            $migration = $version->getMigration();
169
            if ($migration instanceof ContainerAwareInterface) {
170
                $migration->setContainer($container);
171
            }
172
        }
173
    }
174
}
175