VersionCommand   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 93.1%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 36
c 1
b 0
f 0
dl 0
loc 69
ccs 27
cts 29
cp 0.931
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 19 1
A createMigration() 0 3 1
B execute() 0 32 10
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\Exception\UnknownVersionException;
16
use AntiMattr\MongoDB\Migrations\Migration;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
/**
23
 * @author Matthew Fitzgerald <[email protected]>
24
 */
25
class VersionCommand extends AbstractCommand
26
{
27
    protected static $defaultName = 'mongodb:migrations:version';
28
29 6
    protected function configure()
30
    {
31
        $this
32 6
            ->setDescription('Manually add and delete migration versions from the version table.')
33 6
            ->addArgument('version', InputArgument::REQUIRED, 'The version to add or delete.', null)
34 6
            ->addOption('add', null, InputOption::VALUE_NONE, 'Add the specified version.')
35 6
            ->addOption('delete', null, InputOption::VALUE_NONE, 'Delete the specified version.')
36 6
            ->setHelp(<<<'EOT'
37 6
The <info>%command.name%</info> command allows you to manually add and delete migration versions from the version table:
38
39
    <info>%command.full_name% YYYYMMDDHHMMSS --add</info>
40
41
If you want to delete a version you can use the <comment>--delete</comment> option:
42
43
    <info>%command.full_name% YYYYMMDDHHMMSS --delete</info>
44
EOT
45
        );
46
47 6
        parent::configure();
48 6
    }
49
50
    /**
51
     * @param \Symfony\Component\Console\Input\InputInterface
52
     * @param \Symfony\Component\Console\Output\OutputInterface
53
     *
54
     * @throws UnknownVersionException Throws exception if migration version does not exist
55
     * @throws \InvalidArgumentException
56
     */
57 6
    public function execute(InputInterface $input, OutputInterface $output)
58
    {
59 6
        $configuration = $this->getMigrationConfiguration($input, $output);
60 6
        $migration = $this->createMigration($configuration);
0 ignored issues
show
Unused Code introduced by
The assignment to $migration is dead and can be removed.
Loading history...
61
62 6
        if (false === $input->getOption('add') && false === $input->getOption('delete')) {
63 1
            throw new \InvalidArgumentException('You must specify whether you want to --add or --delete the specified version.');
64
        }
65
66 5
        $version = $input->getArgument('version');
67 5
        $markMigrated = $input->getOption('add') ? true : false;
68
69 5
        if (!$configuration->hasVersion($version)) {
0 ignored issues
show
Bug introduced by
It seems like $version can also be of type string[]; however, parameter $version of AntiMattr\MongoDB\Migrat...iguration::hasVersion() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
        if (!$configuration->hasVersion(/** @scrutinizer ignore-type */ $version)) {
Loading history...
70 1
            throw new UnknownVersionException($version);
0 ignored issues
show
Bug introduced by
It seems like $version can also be of type string[]; however, parameter $message of AntiMattr\MongoDB\Migrat...xception::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
            throw new UnknownVersionException(/** @scrutinizer ignore-type */ $version);
Loading history...
71
        }
72
73 4
        $version = $configuration->getVersion($version);
0 ignored issues
show
Bug introduced by
It seems like $version can also be of type string[]; however, parameter $version of AntiMattr\MongoDB\Migrat...iguration::getVersion() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
        $version = $configuration->getVersion(/** @scrutinizer ignore-type */ $version);
Loading history...
74 4
        if ($markMigrated && $configuration->hasVersionMigrated($version)) {
75 1
            throw new \InvalidArgumentException(sprintf('The version "%s" already exists in the version collection.', $version));
76
        }
77
78 3
        if (!$markMigrated && !$configuration->hasVersionMigrated($version)) {
79 1
            throw new \InvalidArgumentException(sprintf('The version "%s" does not exists in the version collection.', $version));
80
        }
81
82 2
        if ($markMigrated) {
83 1
            $version->markMigrated();
84
        } else {
85 1
            $version->markNotMigrated();
86
        }
87
88 2
        return 0;
89
    }
90
91
    protected function createMigration(Configuration $configuration)
92
    {
93
        return new Migration($configuration);
94
    }
95
}
96