Completed
Push — master ( 59a5b3...55d26a )
by Korvin
02:10
created

PackageHandler::handleListPackages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Buttress\Concrete\CommandBus\Handler\Legacy;
4
5
use Buttress\Concrete\Adapter\LegacyAdapter;
6
use Buttress\Concrete\CommandBus\Command\Package\Install;
7
use Buttress\Concrete\CommandBus\Command\Package\Uninstall;
8
use Buttress\Concrete\Exception\RuntimeException;
9
use League\CLImate\CLImate;
10
use Package;
11
12
class PackageHandler
13
{
14
15
    /** @var \Buttress\Concrete\Adapter\LegacyAdapter */
16
    private $adapter;
17
18
    /** @var \League\CLImate\CLImate */
19
    private $cli;
20
21
    public function __construct(LegacyAdapter $adapter, CLImate $cli)
22
    {
23
        $this->adapter = $adapter;
24
        $this->cli = $cli;
25
    }
26
27
    public function handleListPackages()
28
    {
29
        $this->adapter->attach();
30
31
        $installed = Package::getInstalledHandles();
32
        $notInstalled = $this->names(Package::getAvailablePackages());
33
34
        $this->cli->flank('<green>Installed Packages</green>');
35
        $this->outputList($installed);
36
37
        $this->cli->br()->flank('Available Packages');
38
        $this->outputList($notInstalled);
39
    }
40
41
    public function handleInstall(Install $install)
42
    {
43
        $this->adapter->attach();
44
        $handle = $install->getHandle();
45
        $package = \Loader::package($handle);
46
47
        if ($tests = Package::testForInstall($handle, true)) {
48
            if (is_array($tests)) {
49
                $errors = Package::mapError($tests);
50
                throw new RuntimeException(array_shift($errors));
51
            }
52
53
            $package->install();
54
        }
55
    }
56
57
    public function handleUninstall(Uninstall $uninstall)
58
    {
59
        $this->adapter->attach();
60
        $handle = $uninstall->getHandle();
61
        if ($package = Package::getByHandle($handle)) {
62
            $package->uninstall();
63
        } else {
64
            throw new RuntimeException('Invalid Package');
65
        }
66
    }
67
68
    private function outputList(array $packages)
69
    {
70
        if (count($packages) < 10) {
71
            $this->cli->out($packages);
0 ignored issues
show
Documentation introduced by
$packages is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72
        } else {
73
            $this->cli->columns($packages);
74
        }
75
    }
76
77
    private function names(array $packages)
78
    {
79
        return array_map(function(Package $package) {
80
            return $package->getPackageHandle();
81
        }, $packages);
82
    }
83
84
}
85