PackageHandler::handleUninstall()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Buttress\Concrete\CommandBus\Handler;
4
5
use Buttress\Concrete\Service\Package\Package;
6
use Buttress\Concrete\Service\Package\PackageItem;
7
use Buttress\Concrete\CommandBus\Command\Package\Install;
8
use Buttress\Concrete\CommandBus\Command\Package\ListPackages;
9
use Buttress\Concrete\CommandBus\Command\Package\Uninstall;
10
use Buttress\Concrete\Exception\RuntimeException;
11
12
class PackageHandler
13
{
14
15
    /** @var \Buttress\Concrete\Service\Package\Package */
16
    private $package;
17
18
    public function __construct(Package $package)
19
    {
20
        $this->package = $package;
21
    }
22
23 View Code Duplication
    public function handleInstall(Install $command)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
    {
25
        $result = $this->package->install((new PackageItem())->setHandle($command->getHandle()));
26
        if (!$result->success()) {
27
            $errors = $result->getErrors();
28
            throw new RuntimeException(reset($errors));
29
        }
30
    }
31
32 View Code Duplication
    public function handleUninstall(Uninstall $command)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
    {
34
        $result = $this->package->uninstall((new PackageItem())->setHandle($command->getHandle()));
35
        if (!$result->success()) {
36
            $errors = $result->getErrors();
37
            throw new RuntimeException(reset($errors));
38
        }
39
    }
40
41
    public function handleListPackages(ListPackages $command)
42
    {
43
        $packages = iterator_to_array($this->package->all());
44
        usort($packages, function(PackageItem $a, PackageItem $b) {
0 ignored issues
show
Unused Code introduced by
The parameter $b 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...
45
            return $a->isInstalled() ? 1 : -1;
46
        });
47
48
        if ($table = array_map([$this, 'listPackage'], $packages)) {
49
            $command->getCli()->table($table);
50
        } else {
51
            $command->getCli()->out('No packages found.');
52
        }
53
    }
54
55
    private function listPackage(PackageItem $item)
56
    {
57
        if ($item->isInstalled()) {
58
            $installed = sprintf('<green>%s</green>', $item->getInstalledVersion());
59
        } else {
60
            $installed = '<yellow>No</yellow>';
61
        }
62
63
        return [
64
            'Package Handle' => $item->getHandle(),
65
            'Version' => $item->getVersion(),
66
            'Installed' => $installed,
67
        ];
68
    }
69
}
70