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

VersionsCommand::dumpPackageVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 3
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