PackageHandler   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 27.59 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 8
dl 16
loc 58
ccs 0
cts 45
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handleInstall() 8 8 2
A handleUninstall() 8 8 2
A handleListPackages() 0 13 3
A listPackage() 0 14 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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