Completed
Push — master ( 62e188...0425f4 )
by Korvin
06:09
created

PackageHandler::getUninstalledPackage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 2
crap 12
1
<?php
2
3
namespace Buttress\Concrete\CommandBus\Handler\Modern;
4
5
use Buttress\Concrete\Adapter\ModernAdapter;
6
use Buttress\Concrete\CommandBus\Command\Package\Install;
7
use Buttress\Concrete\CommandBus\Command\Package\ListPackages;
8
use Buttress\Concrete\CommandBus\Command\Package\Uninstall;
9
use Buttress\Concrete\Exception\RuntimeException;
10
use Buttress\Concrete\Locator\Site;
11
use Concrete\Core\Package\PackageService;
12
use League\CLImate\CLImate;
13
use Psr\Log\LoggerInterface;
14
15
class PackageHandler
16
{
17
18
    protected $site;
19
20
    protected $adapter;
21
22
    protected $logger;
23
24
    protected $cli;
25
26
    public function __construct(Site $site, ModernAdapter $adapter, LoggerInterface $logger, CLImate $cli)
27
    {
28
        $this->site = $site;
29
        $this->adapter = $adapter;
30
        $this->logger = $logger;
31
        $this->cli = $cli;
32
    }
33
34
    public function handleListPackages(ListPackages $command)
0 ignored issues
show
Unused Code introduced by
The parameter $command is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
    {
36
        $service = $this->getService();
37
        $installed = $this->names($service->getInstalledList());
38
        $notInstalled = $this->names($service->getAvailablePackages());
39
40
        $this->cli->flank('<green>Installed Packages</green>');
41
        $this->outputList($installed);
42
43
44
        $this->cli->br()->flank('Available Packages');
45
        $this->outputList($notInstalled);
46
    }
47
48
    private function outputList(array $list)
49
    {
50
        if (count($list) > 8) {
51
            $this->cli->columns($list);
52
        } else {
53
            $this->cli->out($list);
0 ignored issues
show
Documentation introduced by
$list 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...
54
        }
55
    }
56
57
    private function names(array $packages)
58
    {
59
        return (array) array_map(function($package) {
60
            return $package->getPackageHandle();
61
        }, $packages);
62
    }
63
64
    public function handleInstall(Install $command)
65
    {
66
        $service = $this->getService();
67
        $handle = $command->getHandle();
68
69
        if ($package = $this->getUninstalledPackage($service, $handle)) {
70
            $service->install($package, []);
71
        } else {
72
            throw new RuntimeException('Invalid package handle "' . $handle . '"');
73
        }
74
    }
75
76
    public function handleUninstall(Uninstall $command)
77
    {
78
        $service = $this->getService();
79
        $handle = $command->getHandle();
80
        if ($package = $this->getInstalledPackage($service, $handle)) {
81
            $service->uninstall($package);
82
        } else {
83
            throw new RuntimeException('Invalid package handle "' . $handle . '"');
84
        }
85
    }
86
87
    private function getUninstalledPackage(PackageService $service, $handle)
88
    {
89
        $packages = $service->getAvailablePackages();
90
        foreach ($packages as $package) {
91
            if ($package->getPackageHandle() === $handle) {
92
                return $package;
93
            }
94
        }
95
    }
96
97
    private function getInstalledPackage(PackageService $service, $handle)
98
    {
99
        $packages = $service->getAvailablePackages(false);
100
        foreach ($packages as $package) {
101
            if ($package->getPackageHandle() === $handle) {
102
                return $package;
103
            }
104
        }
105
    }
106
107
    /**
108
     * @return PackageService
109
     */
110
    private function getService()
111
    {
112
        $this->adapter->attach();
113
        $application = $this->adapter->getApplication();
114
        $application->bind(LoggerInterface::class, $this->logger);
115
116
        /** @var PackageService $service */
117
        return $application->make(PackageService::class);
118
    }
119
120
121
}
122