Completed
Push — master ( 60e59a...334051 )
by Adam
06:35
created

ListCommand::execute()   C

Complexity

Conditions 7
Paths 30

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 33
rs 6.7272
cc 7
eloc 18
nc 30
nop 2
1
<?php
2
/**
3
 * ListCommand.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:Packages!
9
 * @subpackage     Commands
10
 * @since          2.0.0
11
 *
12
 * @date           19.07.16
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Packages\Commands;
18
19
use Symfony\Component\Console\Input;
20
use Symfony\Component\Console\Output;
21
22
use IPub;
23
use IPub\Packages;
24
use IPub\Packages\DependencyResolver;
25
use IPub\Packages\Exceptions;
26
27
/**
28
 * List packages command
29
 *
30
 * @package        iPublikuj:Packages!
31
 * @subpackage     Commands
32
 *
33
 * @author         Josef Kříž <[email protected]>
34
 * @author         Adam Kadlec <[email protected]>
35
 */
36
final class ListCommand extends Command
37
{
38
	/**
39
	 * @return void
40
	 */
41
	protected function configure()
42
	{
43
		$this
44
			->setName('ipub:packages:list')
45
			->setDescription('List of installed packages.');
46
	}
47
48
	/**
49
	 * @param Input\InputInterface $input
50
	 * @param Output\OutputInterface $output
51
	 *
52
	 * @return void
53
	 */
54
	protected function execute(Input\InputInterface $input, Output\OutputInterface $output)
55
	{
56
		// Register available
57
		foreach ($this->packageManager->registerAvailable() as $item) {
58
			foreach ($item as $name => $action) {
0 ignored issues
show
Bug introduced by
The expression $item of type string is not traversable.
Loading history...
59
				$output->writeln(sprintf('<info>%s : %s</info>', $action, $name));
60
			}
61
		}
62
63
		try {
64
			$packages = $this->repository->getPackages();
65
66
			$maxLength = 0;
67
68
			foreach ($packages as $package) {
69
				$length = strlen($package->getName());
70
71
				$maxLength = $maxLength > $length ? $maxLength : $length;
72
			}
73
74
			foreach ($packages as $package) {
75
				$output->writeln(sprintf(
76
					'<info>%'. $maxLength .'s</info> | status: <comment>%-12s</comment> | version: <comment>%s</comment>',
77
					$package->getName(),
78
					$this->packageManager->getStatus($package),
79
					$this->packageManager->getVersion($package)
80
				));
81
			}
82
83
		} catch (Exceptions\InvalidArgumentException $e) {
84
			$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
85
		}
86
	}
87
}
88