InstallController   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 119
Duplicated Lines 31.93 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 8
dl 38
loc 119
ccs 0
cts 73
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A listPackages() 0 4 1
A install() 19 19 3
A uninstall() 19 19 3
A getDefinition() 0 10 3
A getAvailablePackages() 0 10 4
A confirmHandle() 0 17 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\Console\Command\Package;
4
5
use Buttress\Concrete\CommandBus\Command\Package\Install;
6
use Buttress\Concrete\CommandBus\Command\Package\ListPackages;
7
use Buttress\Concrete\CommandBus\Command\Package\Uninstall;
8
use Buttress\Concrete\Console\Command\PackageCommand;
9
use Buttress\Concrete\Locator\Site;
10
use League\CLImate\CLImate;
11
use League\Tactician\CommandBus;
12
13
class InstallController
14
{
15
16
    /** @var \Buttress\Concrete\Console\Command\PackageCommand */
17
    private $command;
18
19
    /** @var \League\Tactician\CommandBus */
20
    private $bus;
21
22
    /** @var \League\CLImate\CLImate */
23
    private $cli;
24
25
    public function __construct(PackageCommand $command, CommandBus $bus, CLImate $cli)
26
    {
27
        $this->command = $command;
28
        $this->bus = $bus;
29
        $this->cli = $cli;
30
    }
31
32
    /**
33
     * Handle package:list
34
     *
35
     * @param \Buttress\Concrete\Locator\Site $site
36
     */
37
    public function listPackages(Site $site)
0 ignored issues
show
Unused Code introduced by
The parameter $site 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...
38
    {
39
        $this->bus->handle((new ListPackages())->setCli($this->cli));
40
    }
41
42
    /**
43
     * Handle package:install
44
     *
45
     * @param \Buttress\Concrete\Locator\Site $site
46
     */
47 View Code Duplication
    public function install(Site $site)
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...
48
    {
49
        $definition = $this->getDefinition($site, 'package:install');
50
        $definition->parse();
51
        $handle = $definition->get('handle');
52
53
        if (!$handle) {
54
            $packages = iterator_to_array($this->getAvailablePackages($site));
55
            if (!$packages) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $packages of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
56
                $this->cli->error('No packages available to install');
57
            }
58
59
            $handle = $this->confirmHandle($packages);
60
        }
61
62
        $this->cli->info('Installing ' . $handle);
63
        $this->bus->handle((new Install())->setHandle($handle));
64
        $this->cli->green('Installed Successfully!');
65
    }
66
67
    /**
68
     * Handle package:uninstall
69
     *
70
     * @param \Buttress\Concrete\Locator\Site $site
71
     */
72 View Code Duplication
    public function uninstall(Site $site)
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...
73
    {
74
        $definition = $this->getDefinition($site, 'package:uninstall');
75
        $definition->parse();
76
        $handle = $definition->get('handle');
77
78
        if (!$handle) {
79
            $packages = iterator_to_array($this->getAvailablePackages($site));
80
            if (!$packages) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $packages of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
81
                $this->cli->error('No packages available to uninstall');
82
            }
83
84
            $handle = $this->confirmHandle($packages);
85
        }
86
87
        $this->cli->info('Uninstalling ' . $handle);
88
        $this->bus->handle((new Uninstall())->setHandle($handle));
89
        $this->cli->green('Uninstalled Successfully!');
90
    }
91
92
    private function getDefinition(Site $site, $command)
93
    {
94
        /** @var \Buttress\Concrete\Console\Command\Manager\CommandManager[] $definitions */
95
        $definitions = $this->command->getCommands($site);
96
        foreach ($definitions as $definition) {
97
            if ($definition->getCommand() === $command) {
98
                return $definition;
99
            }
100
        }
101
    }
102
103
    private function getAvailablePackages(Site $site)
104
    {
105
        $iterator = new \DirectoryIterator($site->getPath() . '/packages');
106
107
        foreach ($iterator as $item) {
108
            if (!$item->isDot() && $item->isDir()) {
109
                yield $item->getBasename();
110
            }
111
        }
112
    }
113
114
    private function confirmHandle($packages)
115
    {
116
117
        $green = $this->cli->green();
118
119
        if (count($packages) < 10) {
120
            $green->out($packages);
121
        } else {
122
            $green->columns($packages);
123
        }
124
125
        $input = $this->cli->input('Which package?');
126
        $input->accept($packages);
127
        $input->strict();
128
129
        return $input->prompt();
130
    }
131
}
132