Completed
Push — master ( 59ae9e...3c93b7 )
by Korvin
02:29
created

LegacyDriver   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 157
Duplicated Lines 6.37 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 6
dl 10
loc 157
ccs 0
cts 88
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getPackage() 0 7 3
C install() 0 34 7
B uninstall() 0 25 5
A test() 0 15 3
A show() 10 10 3
A all() 0 12 3

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\Service\Package\Driver;
4
5
use Buttress\Concrete\Client\Connection\Connection;
6
use Buttress\Concrete\Exception\RuntimeException;
7
use Buttress\Concrete\Service\Package\PackageItem;
8
use Buttress\Concrete\Service\Package\PackageItemFactory;
9
use Buttress\Concrete\Service\Result;
10
use League\CLImate\CLImate;
11
use Loader;
12
use Package;
13
14
class LegacyDriver implements Driver
15
{
16
17
    /** @var \Buttress\Concrete\Client\Connection\Connection */
18
    private $connection;
19
20
    /** @var \Buttress\Concrete\Service\Package\PackageItemFactory */
21
    private $factory;
22
23
    public function __construct(Connection $connection, PackageItemFactory $factory)
24
    {
25
        $this->connection = $connection;
26
        $this->factory = $factory;
27
    }
28
29
    /**
30
     * @param \Buttress\Concrete\Service\Package\PackageItem $item
31
     * @return Package|null
32
     */
33
    private function getPackage(PackageItem $item)
34
    {
35
        if (!$package = Package::getByHandle($item->getHandle())) {
36
            $package = Loader::package($item->getHandle());
37
        }
38
        return is_object($package) ? $package : null;
39
    }
40
41
    /**
42
     * Install a package
43
     *
44
     * @param \Buttress\Concrete\Service\Package\PackageItem $item
45
     * @return \Buttress\Concrete\Service\Result
46
     */
47
    public function install(PackageItem $item)
48
    {
49
        if (!$this->connection->isConnected()) {
50
            return new Result(false, 'Not connected to concrete5 site.');
0 ignored issues
show
Documentation introduced by
'Not connected to concrete5 site.' is of type string, but the function expects a array.

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...
51
        }
52
53
        if (!$package = $this->getPackage($item)) {
54
            return new Result(false, 'Invalid package handle.');
0 ignored issues
show
Documentation introduced by
'Invalid package handle.' is of type string, but the function expects a array.

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...
55
        }
56
57
        // Fill the item with real data
58
        $item = $this->factory->fromLegacy($package);
59
60
        if ($item->isInstalled()) {
61
            return new Result(false, sprintf('<underline><bold>%s</bold></underline> is already installed.', $package->getPackageName()));
0 ignored issues
show
Documentation introduced by
sprintf('<underline><bol...kage->getPackageName()) is of type string, but the function expects a array.

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...
62
        }
63
64
        if ($package->showInstallOptionsScreen()) {
65
            return new Result(false, 'Install options are not currently supported. Please install through the dashboard.');
0 ignored issues
show
Documentation introduced by
'Install options are not...through the dashboard.' is of type string, but the function expects a array.

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...
66
        }
67
68
        $tests = $this->test($item);
69
        if (!$tests->success()) {
70
            return $tests;
71
        }
72
73
        try {
74
            $package->install([]);
75
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class Buttress\Concrete\Service\Package\Driver\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
76
            return new Result(false, [$e->getMessage()]);
77
        }
78
79
        return new Result();
80
    }
81
82
    /**
83
     * Uninstall a package
84
     *
85
     * @param PackageItem $item
86
     * @return \Buttress\Concrete\Service\Result
87
     */
88
    public function uninstall(PackageItem $item)
89
    {
90
        if (!$this->connection->isConnected()) {
91
            return new Result(false, 'Not connected to concrete5 site.');
0 ignored issues
show
Documentation introduced by
'Not connected to concrete5 site.' is of type string, but the function expects a array.

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...
92
        }
93
94
        if (!$package = $this->getPackage($item)) {
95
            return new Result(false, 'Invalid package handle.');
0 ignored issues
show
Documentation introduced by
'Invalid package handle.' is of type string, but the function expects a array.

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...
96
        }
97
98
        // Fill the item with real data
99
        $item = $this->factory->fromLegacy($package);
100
101
        if (!$item->isInstalled()) {
102
            return new Result(false, sprintf('<underline><bold>%s</bold></underline> hasn\'t been installed yet.', $package->getPackageName()));
0 ignored issues
show
Documentation introduced by
sprintf('<underline><bol...kage->getPackageName()) is of type string, but the function expects a array.

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...
103
        }
104
105
        try {
106
            $package->uninstall();
107
        } catch (\Exception $e) {
108
            return new Result(false, [$e->getMessage()]);
109
        }
110
111
        return new Result();
112
    }
113
114
    /**
115
     * Test a package for install
116
     *
117
     * @param PackageItem $package
118
     * @return \Buttress\Concrete\Service\Result
119
     */
120
    public function test(PackageItem $package)
121
    {
122
        if (!$this->connection->isConnected()) {
123
            return new Result(false, 'Not connected to concrete5 site.');
0 ignored issues
show
Documentation introduced by
'Not connected to concrete5 site.' is of type string, but the function expects a array.

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...
124
        }
125
126
        $package = $this->getPackage($package);
127
        $errors = [];
128
129
        if (is_array($tests = $package->testForInstall($package->getPackageHandle(), false))) {
130
            $errors = Package::mapError($tests);
131
        }
132
133
        return new Result((bool) !$errors, $errors);
134
    }
135
136
    /**
137
     * Show information about a package
138
     *
139
     * @param PackageItem $package
140
     * @return \Buttress\Concrete\Service\Result
141
     */
142 View Code Duplication
    public function show(PackageItem $package, CLImate $cli)
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...
143
    {
144
        if (!$this->connection->isConnected()) {
145
            return new Result(false, 'Not connected to concrete5 site.');
0 ignored issues
show
Documentation introduced by
'Not connected to concrete5 site.' is of type string, but the function expects a array.

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...
146
        }
147
148
        $cli->flank($package->getHandle());
149
        $cli->out($package->isInstalled() ? 'Installed Version ' : 'Not Installed: ');
150
        $cli->bold()->inline($package->getVersion());
151
    }
152
153
    /**
154
     * Get a list of package item objects
155
     * @return PackageItem[]
156
     * @throws \Buttress\Concrete\Exception\RuntimeException
157
     */
158
    public function all()
159
    {
160
        if (!$this->connection->isConnected()) {
161
            throw new RuntimeException('Not connected to concrete5 site.');
162
        }
163
164
        $packages = Package::getAvailablePackages(false);
165
166
        foreach ($packages as $package) {
167
            yield $this->factory->fromLegacy($package);
168
        }
169
    }
170
}
171