Completed
Push — feature/service-wrapper-for-sw... ( faf928...5dac9b )
by Samuel
13:45 queued 01:09
created

MongodbMigrateCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
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
14
/**
15
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
16
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
17
 * @link     http://swisscom.ch
18
 */
19
class MongodbMigrateCommand extends Command
20
{
21
    /**
22
     * @var Finder
23
     */
24
    private $finder;
25
26
    /**
27
     * @param Finder $finder finder that finds configs
28
     */
29
    public function __construct(Finder $finder)
30
    {
31
        $this->finder = $finder;
32
33
        parent::__construct();
34
    }
35
36
    /**
37
     * setup command
38
     *
39
     * @return void
40
     */
41
    protected function configure()
42
    {
43
        parent::configure();
44
45
        $this->setName('graviton:mongodb:migrate');
46
    }
47
48
    /**
49
     * call execute on found commands
50
     *
51
     * @param InputInterface  $input  user input
52
     * @param OutputInterface $output command output
53
     *
54
     * @return void
55
     */
56
    public function execute(InputInterface $input, OutputInterface $output)
57
    {
58
        $this->finder->in(
59
            strpos(getcwd(), 'vendor/') === false ? getcwd() :  getcwd() . '/../../../../'
60
        )->path('Resources/config')->name('/migrations.(xml|yml)/')->files();
61
62
        foreach ($this->finder as $file) {
63
            if (!$file->isFile()) {
64
                continue;
65
            }
66
67
            $output->writeln('Found '.$file->getRelativePathname());
68
69
            $command = $this->getApplication()->find('mongodb:migrations:migrate');
70
71
            $arguments = $input->getArguments();
72
            $arguments['command'] = 'mongodb:migrations:migrate';
73
            $arguments['--configuration'] = $file->getPathname();
74
75
            $migrateInput = new ArrayInput($arguments);
76
            $returnCode = $command->run($migrateInput, $output);
77
78
            if ($returnCode !== 0) {
79
                $output->writeln(
80
                    '<error>Calling mongodb:migrations:migrate failed for '.$file->getRelativePathname().'</error>'
81
                );
82
                return $returnCode;
83
            }
84
        }
85
    }
86
}
87