Completed
Pull Request — master (#26)
by Stepan
05:53
created

VersionsCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 0
cbo 4
dl 0
loc 52
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A execute() 0 19 3
A dumpPackageVersion() 0 11 1
1
<?php
2
3
namespace PackageVersions;
4
5
use Composer\Command\BaseCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * Class CommandProvider
12
 */
13
class VersionsCommand extends BaseCommand
14
{
15
    protected function configure()
16
    {
17
        $this
18
            ->setName('versions')
19
            ->addArgument('package', InputArgument::OPTIONAL, 'Package name to display version for')
20
        ;
21
    }
22
23
    /**
24
     * @param InputInterface $input
25
     * @param OutputInterface $output
26
     * @return integer
27
     */
28
    protected function execute(InputInterface $input, OutputInterface $output) : int
29
    {
30
        if ($packageName = $input->getArgument('package')) {
31
            $output->writeln('Installed version:');
32
            $this->dumpPackageVersion($output, $packageName, Versions::getVersion($packageName));
33
34
            return 0;
35
        }
36
37
        $output->writeln('Installed versions:');
38
        $versionsSorted = \PackageVersions\Versions::VERSIONS;
39
        ksort($versionsSorted);
40
41
        foreach ($versionsSorted as $packageName => $version) {
42
            $this->dumpPackageVersion($output, $packageName, $version);
43
        }
44
45
        return 0;
46
    }
47
48
    /**
49
     * @param OutputInterface $output
50
     * @param string $packageName
51
     * @param string $version
52
     */
53
    private function dumpPackageVersion(OutputInterface $output, string $packageName, string $version)
54
    {
55
        list($version, $hash) = explode('@', $version);
56
57
        $output->writeln(sprintf(
58
            '<info>%s</info>: %s@%s',
59
            $packageName,
60
            $version,
61
            $hash
62
        ));
63
    }
64
}
65