LegacyDriver::install()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 31
ccs 0
cts 22
cp 0
rs 8.439
c 1
b 0
f 0
cc 6
eloc 15
nc 6
nop 1
crap 42
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
        // Let exceptions fall through
74
        $package->install();
75
76
        return new Result();
77
    }
78
79
    /**
80
     * Uninstall a package
81
     *
82
     * @param PackageItem $item
83
     * @return \Buttress\Concrete\Service\Result
84
     */
85
    public function uninstall(PackageItem $item)
86
    {
87
        if (!$this->connection->isConnected()) {
88
            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...
89
        }
90
91
        if (!$package = $this->getPackage($item)) {
92
            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...
93
        }
94
95
        // Fill the item with real data
96
        $item = $this->factory->fromLegacy($package);
97
98
        if (!$item->isInstalled()) {
99
            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...
100
        }
101
102
        try {
103
            $package->uninstall();
104
        } catch (\Exception $e) {
105
            return new Result(false, [$e->getMessage()]);
106
        }
107
108
        return new Result();
109
    }
110
111
    /**
112
     * Test a package for install
113
     *
114
     * @param PackageItem $package
115
     * @return \Buttress\Concrete\Service\Result
116
     */
117
    public function test(PackageItem $package)
118
    {
119
        if (!$this->connection->isConnected()) {
120
            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...
121
        }
122
123
        $package = $this->getPackage($package);
124
        $errors = [];
125
126
        if (is_array($tests = $package->testForInstall($package->getPackageHandle(), false))) {
127
            $errors = Package::mapError($tests);
128
        }
129
130
        return new Result((bool) !$errors, $errors);
131
    }
132
133
    /**
134
     * Show information about a package
135
     *
136
     * @param PackageItem $package
137
     * @return \Buttress\Concrete\Service\Result
138
     */
139 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...
140
    {
141
        if (!$this->connection->isConnected()) {
142
            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...
143
        }
144
145
        $cli->flank($package->getHandle());
146
        $cli->out($package->isInstalled() ? 'Installed Version ' : 'Not Installed: ');
147
        $cli->bold()->inline($package->getVersion());
148
    }
149
150
    /**
151
     * Get a list of package item objects
152
     * @return PackageItem[]
153
     * @throws \Buttress\Concrete\Exception\RuntimeException
154
     */
155
    public function all()
156
    {
157
        if (!$this->connection->isConnected()) {
158
            throw new RuntimeException('Not connected to concrete5 site.');
159
        }
160
161
        $packages = Package::getAvailablePackages(false);
162
163
        foreach ($packages as $package) {
164
            yield $this->factory->fromLegacy($package);
165
        }
166
    }
167
}
168