Completed
Push — master ( 9eb332...ef5bef )
by Arne
02:27
created

ShowIndexCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 2
cbo 7
dl 0
loc 66
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
B executeConfigured() 0 54 7
1
<?php
2
3
namespace Storeman\Cli\Command;
4
5
use Storeman\Cli\Helper\DisplayIndexHelper;
6
use Storeman\Storeman;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class ShowIndexCommand extends AbstractCommand
12
{
13
    protected function configure()
14
    {
15
        parent::configure();
16
17
        $this->setName('show-index');
18
        $this->setDescription('Displays an index');
19
        $this->addArgument('revision', InputArgument::OPTIONAL, 'Revision number, \'latest\' or \'local\'. Defaults to \'latest\'.', 'latest');
20
    }
21
22
    protected function executeConfigured(InputInterface $input, OutputInterface $output, Storeman $storeman): int
23
    {
24
        $revision = $input->getArgument('revision');
25
        $index = null;
26
27
        if ($revision === 'latest')
28
        {
29
            $revision = $storeman->getLastRevision();
30
31
            if ($revision === null)
32
            {
33
                $this->consoleStyle->writeln("Could not find any past synchronizations to show.");
34
35
                return 0;
36
            }
37
        }
38
        elseif ($revision === 'local')
39
        {
40
            $index = $storeman->getLocalIndex();
41
        }
42
        elseif (ctype_digit($revision))
43
        {
44
            $revision = intval($revision);
45
        }
46
        else
47
        {
48
            $this->consoleStyle->error("Argument 'revision' invalid.");
49
50
            return 1;
51
        }
52
53
        if ($index === null)
54
        {
55
            assert(is_int($revision));
56
57
            $vaults = $storeman->getVaultContainer();
58
            $vault = $vaults->getPrioritizedVault($vaults->getVaultsHavingRevision($revision));
59
60
            if ($vault === null)
61
            {
62
                $this->consoleStyle->error("Could not find requested revision {$revision} in any vault.");
63
64
                return 1;
65
            }
66
67
            $index = $vault->loadRemoteIndex($revision);
68
        }
69
70
        /** @var DisplayIndexHelper $displayIndexHelper */
71
        $displayIndexHelper = $this->getHelper('displayIndex');
72
        $displayIndexHelper->displayIndex($index, $output);
0 ignored issues
show
Bug introduced by
It seems like $index can be null; however, displayIndex() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
73
74
        return 0;
75
    }
76
}
77