Completed
Push — feature/service-wrapper-for-sw... ( 555800...5fd474 )
by Samuel
09:29
created

injectContainerToMigrations()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6667
cc 3
eloc 5
nc 3
nop 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\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
        $this->finder->in(
88
            strpos(getcwd(), 'vendor/') === false ? getcwd() :  getcwd() . '/../../../../'
89
        )->path('Resources/config')->name('/migrations.(xml|yml)/')->files();
90
91
        foreach ($this->finder as $file) {
92
            if (!$file->isFile()) {
93
                continue;
94
            }
95
96
            $output->writeln('Found '.$file->getRelativePathname());
97
98
            $command = $this->getApplication()->find('mongodb:migrations:migrate');
99
100
            $helperSet = $command->getHelperSet();
101
            $helperSet->set($this->documentManager, 'dm');
102
            $command->setHelperSet($helperSet);
103
104
            $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...
105
            self::injectContainerToMigrations($this->container, $configuration->getMigrations());
106
            $command->setMigrationConfiguration($configuration);
107
108
            $arguments = $input->getArguments();
109
            $arguments['command'] = 'mongodb:migrations:migrate';
110
            $arguments['--configuration'] = $file->getPathname();
111
112
            $migrateInput = new ArrayInput($arguments);
113
            $returnCode = $command->run($migrateInput, $output);
114
115
            if ($returnCode !== 0) {
116
                $output->writeln(
117
                    '<error>Calling mongodb:migrations:migrate failed for '.$file->getRelativePathname().'</error>'
118
                );
119
                return $returnCode;
120
            }
121
        }
122
    }
123
124
    /**
125
     * get configration object for migration script
126
     *
127
     * This is based on antromattr/mongodb-migartion code but extends it so we can inject
128
     * non local stuff centrally.
129
     *
130
     * @param string $filepath path to configuration file
131
     * @param Output $output   ouput interface need by config parser to do stuff
132
     *
133
     * @return AntiMattr\MongoDB\Migrations\Configuration\Configuration
134
     */
135
    private function getConfiguration($filepath, $output)
136
    {
137
        $outputWriter = new OutputWriter(
138
            function ($message) use ($output) {
139
                return $output->writeln($message);
140
            }
141
        );
142
143
        $info = pathinfo($filepath);
144
        $namespace = 'AntiMattr\MongoDB\Migrations\Configuration';
145
        $class = $info['extension'] === 'xml' ? 'XmlConfiguration' : 'YamlConfiguration';
146
        $class = sprintf('%s\%s', $namespace, $class);
147
        $configuration = new $class($this->documentManager->getDocumentManager()->getConnection(), $outputWriter);
148
149
        // register databsae name before loading to ensure that loading does not fail
150
        $configuration->setMigrationsDatabaseName($this->databaseName);
151
152
        // load additional config from migrations.(yml|xml)
153
        $configuration->load($filepath);
154
155
        return $configuration;
156
    }
157
    
158
    /**
159
     * Injects the container to migrations aware of it
160
     *
161
     * @param ContainerInterface $container container to inject into container aware migrations
162
     * @param array              $versions  versions that might need injecting a container
163
     *
164
     * @return void
165
     */
166
    private static function injectContainerToMigrations(ContainerInterface $container, array $versions)
167
    {
168
        foreach ($versions as $version) {
169
            $migration = $version->getMigration();
170
            if ($migration instanceof ContainerAwareInterface) {
171
                $migration->setContainer($container);
172
            }
173
        }
174
    }
175
}
176