ModernDriver::getPackage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Buttress\Concrete\Service\Package\Driver;
4
5
use Buttress\Concrete\Client\Connection\Connection;
6
use Buttress\Concrete\Service\Package\PackageItem;
7
use Buttress\Concrete\Service\Package\PackageItemFactory;
8
use Buttress\Concrete\Service\Result;
9
use Buttress\Concrete\Exception\RuntimeException;
10
use Concrete\Core\Error\ErrorList\ErrorList;
11
use Concrete\Core\Foundation\ClassLoader;
12
use Concrete\Core\Package\BrokenPackage;
13
use Concrete\Core\Package\Package;
14
use Concrete\Core\Package\PackageService;
15
use Concrete\Core\Support\Facade\Database;
16
use Concrete\Core\Support\Facade\DatabaseORM;
17
use Concrete\Core\Support\Facade\Events;
18
use League\CLImate\CLImate;
19
20
class ModernDriver implements Driver
21
{
22
23
    /** @var \Buttress\Concrete\Client\Connection\ModernConnection */
24
    private $connection;
25
26
    /** @var \Buttress\Concrete\Service\Package\PackageItemFactory */
27
    private $factory;
28
29
    public function __construct(Connection $connection, PackageItemFactory $factory)
30
    {
31
        $this->connection = $connection;
0 ignored issues
show
Documentation Bug introduced by
$connection is of type object<Buttress\Concrete...\Connection\Connection>, but the property $connection was declared to be of type object<Buttress\Concrete...ction\ModernConnection>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
32
        $this->factory = $factory;
33
    }
34
35
    /**
36
     * Get a package object from an Item
37
     * @return Package|\Concrete\Core\Entity\Package
38
     */
39
    private function getPackage(PackageItem $item)
40
    {
41
        return Package::getClass($item->getHandle());
42
    }
43
44
    /**
45
     * Install a package
46
     *
47
     * @param PackageItem $item
48
     * @return \Buttress\Concrete\Service\Result
49
     */
50
    public function install(PackageItem $item)
51
    {
52
        if (!$this->connection->isConnected()) {
53
            return new Result(false, 'Not connected to a concrete5 site.');
0 ignored issues
show
Documentation introduced by
'Not connected to a 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...
54
        }
55
56
        if (!$package = $this->getPackage($item)) {
57
            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...
58
        }
59
60
        if ($package instanceof BrokenPackage) {
0 ignored issues
show
Bug introduced by
The class Concrete\Core\Package\BrokenPackage does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
61
            return new Result(false, $package->getInstallErrorMessage());
62
        }
63
64
        if ($package->showInstallOptionsScreen()) {
65
            return new Result(false,
66
                '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...
67
        }
68
69
        $loader = new ClassLoader();
70
        if (method_exists($loader, 'registerPackageCustomAutoloaders')) {
71
            $loader->registerPackageCustomAutoloaders($package);
72
        }
73
74
        if (is_object($tests = $this->testForInstall($package))) {
75
            return new Result(false, $tests->getList());
76
        }
77
78
        try {
79
            $app = $this->connection->getApplication();
80
81
            if ($app->bound(PackageService::class)) {
82
                $result = $app->make(PackageService::class)->install($package, []);
83
            } else {
84
                $result = $package->install([]);
85
            }
86
        } catch (\Exception $e) {
87
            return new Result(false, $e->getMessage());
0 ignored issues
show
Documentation introduced by
$e->getMessage() 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...
88
        }
89
90
        if ($result instanceof ErrorList) {
0 ignored issues
show
Bug introduced by
The class Concrete\Core\Error\ErrorList\ErrorList does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
91
            return new Result(false, $result->getList());
92
        }
93
94
        return new Result();
95
    }
96
97
    /**
98
     * Uninstall a package
99
     *
100
     * @param PackageItem $item
101
     * @return \Buttress\Concrete\Service\Result
102
     */
103
    public function uninstall(PackageItem $item)
104
    {
105
        if (!$this->connection->isConnected()) {
106
            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...
107
        }
108
109
        if (!$package = $this->getPackage($item)) {
110
            return new Result(false, 'Invalid Package');
0 ignored issues
show
Documentation introduced by
'Invalid Package' 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...
111
        }
112
113
        if ($package instanceof Package) {
0 ignored issues
show
Bug introduced by
The class Concrete\Core\Package\Package does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
114
            return new Result(false, 'That package doesn\'t look installed.');
0 ignored issues
show
Documentation introduced by
'That package doesn\'t look installed.' 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...
115
        }
116
117
        $controller = $package->getController();
118
        if (is_object($tests = $controller->testForUninstall())) {
119
            return new Result(false, $tests->getList());
120
        }
121
122
        $loader = new ClassLoader();
123
        $loader->registerPackageCustomAutoloaders($package);
124
125
        $result = $package->uninstall();
126
        if ($result instanceof ErrorList) {
0 ignored issues
show
Bug introduced by
The class Concrete\Core\Error\ErrorList\ErrorList does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
127
            return new Result(false, $result->getList());
128
        }
129
130
        return new Result;
131
    }
132
133
    /**
134
     * Test a package for install
135
     *
136
     * @param PackageItem $item
137
     * @return \Buttress\Concrete\Service\Result
138
     */
139
    public function test(PackageItem $item)
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
        if (!$package = $this->getPackage($item)) {
146
            return new Result(false, 'Invalid Package');
0 ignored issues
show
Documentation introduced by
'Invalid Package' 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...
147
        }
148
149
        return $package->getController()->testForInstall();
150
    }
151
152
    /**
153
     * Show information about a package
154
     *
155
     * @param PackageItem $package
156
     * @param \League\CLImate\CLImate $cli
157
     * @return \Buttress\Concrete\Service\Result
158
     */
159 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...
160
    {
161
        if (!$this->connection->isConnected()) {
162
            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...
163
        }
164
165
        $cli->flank($package->getHandle());
166
        $cli->out($package->isInstalled() ? 'Installed Version ' : 'Not Installed: ');
167
        $cli->bold()->inline($package->getVersion());
168
    }
169
170
    /**
171
     * Get a list of package item objects
172
     * @return PackageItem[]
173
     */
174
    public function all()
175
    {
176
        if (!$this->connection->isConnected()) {
177
            throw new RuntimeException('Not connected to concrete5 site.');
178
        }
179
180
        $installed = Package::getInstalledList();
181
        foreach ($installed as $item) {
182
            yield $this->factory->fromModern($item);
183
        }
184
185
        $notInstalled = Package::getAvailablePackages();
186
        foreach ($notInstalled as $item) {
187
            yield $this->factory->fromModern($item);
188
        }
189
    }
190
191
    private function testForInstall($package)
192
    {
193
        $method = new \ReflectionMethod($package, 'testForInstall');
194
        if ($method->isStatic()) {
195
            return \Package::testForInstall($package->getPackageHandle(), true);
196
        } else {
197
            return $package->testForInstall(true);
198
        }
199
    }
200
}
201