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

InstallController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 114
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 8
dl 38
loc 114
ccs 0
cts 69
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 5 1
A install() 19 19 3
A uninstall() 19 19 3
A getDefinition() 0 10 3
A getAvailablePackages() 0 10 4
A confirmHandle() 0 10 1

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
        $command = new ListPackages();
40
        $this->bus->handle($command);
41
    }
42
43
    /**
44
     * Handle package:install
45
     *
46
     * @param \Buttress\Concrete\Locator\Site $site
47
     */
48 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...
49
    {
50
        $definition = $this->getDefinition($site, 'package:install');
51
        $definition->parse();
52
        $handle = $definition->get('handle');
53
54
        if (!$handle) {
55
            $packages = iterator_to_array($this->getAvailablePackages($site));
56
            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...
57
                $this->cli->error('No packages available to install');
58
            }
59
60
            $handle = $this->confirmHandle($packages);
61
        }
62
63
        $this->cli->info('Installing ' . $handle);
64
        $this->bus->handle((new Install())->setHandle($handle));
65
        $this->cli->green('Installed Successfully!');
66
    }
67
68
    /**
69
     * Handle package:uninstall
70
     *
71
     * @param \Buttress\Concrete\Locator\Site $site
72
     */
73 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...
74
    {
75
        $definition = $this->getDefinition($site, 'package:uninstall');
76
        $definition->parse();
77
        $handle = $definition->get('handle');
78
79
        if (!$handle) {
80
            $packages = iterator_to_array($this->getAvailablePackages($site));
81
            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...
82
                $this->cli->error('No packages available to install');
83
            }
84
85
            $handle = $this->confirmHandle($packages);
86
        }
87
88
        $this->cli->info('Installing ' . $handle);
89
        $this->bus->handle((new Uninstall())->setHandle($handle));
90
        $this->cli->green('Uninstalled Successfully!');
91
    }
92
93
    private function getDefinition(Site $site, $command)
94
    {
95
        /** @var \Buttress\Concrete\Console\Command\Manager\CommandManager[] $definitions */
96
        $definitions = $this->command->getCommands($site);
97
        foreach ($definitions as $definition) {
98
            if ($definition->getCommand() === $command) {
99
                return $definition;
100
            }
101
        }
102
    }
103
104
    private function getAvailablePackages(Site $site)
105
    {
106
        $iterator = new \DirectoryIterator($site->getPath() . '/packages');
107
108
        foreach ($iterator as $item) {
109
            if (!$item->isDot() && $item->isDir()) {
110
                yield $item->getBasename();
111
            }
112
        }
113
    }
114
115
    private function confirmHandle($packages)
116
    {
117
        $this->cli->green()->columns($packages)->br();
118
119
        $input = $this->cli->input('Which package?');
120
        $input->accept($packages);
121
        $input->strict();
122
123
        return $input->prompt();
124
    }
125
126
}
127