Completed
Push — master ( 7381e1...0d723c )
by Korvin
02:16
created

ModernDriver::getPackage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
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 League\CLImate\CLImate;
16
17
class ModernDriver implements Driver
18
{
19
20
    /** @var \Buttress\Concrete\Client\Connection\ModernConnection */
21
    private $connection;
22
23
    /** @var \Buttress\Concrete\Service\Package\PackageItemFactory */
24
    private $factory;
25
26
    public function __construct(Connection $connection, PackageItemFactory $factory)
27
    {
28
        $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...
29
        $this->factory = $factory;
30
    }
31
32
    /**
33
     * Get a package object from an Item
34
     * @return Package|\Concrete\Core\Entity\Package
35
     */
36
    private function getPackage(PackageItem $item)
37
    {
38
        return Package::getClass($item->getHandle());
39
    }
40
41
    /**
42
     * Install a package
43
     *
44
     * @param 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 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...
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
        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...
58
            return new Result(false, $package->getInstallErrorMessage());
59
        }
60
61
        if ($package->showInstallOptionsScreen()) {
62
            return new Result(false,
63
                '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...
64
        }
65
66
        $loader = new ClassLoader();
67
        $loader->registerPackageCustomAutoloaders($package);
68
69
        if (is_object($tests = $package->testForInstall())) {
70
            return new Result(false, $tests->getList());
71
        }
72
73
        try {
74
            $packageService = $this->connection->getApplication()->make(PackageService::class);
75
            $result = $packageService->install($package, []);
76
        } catch (\Exception $e) {
77
            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...
78
        }
79
80
        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...
81
            return new Result(false, $result->getList());
82
        }
83
84
        return new Result();
85
    }
86
87
    /**
88
     * Uninstall a package
89
     *
90
     * @param PackageItem $item
91
     * @return \Buttress\Concrete\Service\Result
92
     */
93
    public function uninstall(PackageItem $item)
94
    {
95
        if (!$this->connection->isConnected()) {
96
            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...
97
        }
98
99
        if (!$package = $this->getPackage($item)) {
100
            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...
101
        }
102
103
        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...
104
            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...
105
        }
106
107
        $controller = $package->getController();
108
        if (is_object($tests = $controller->testForUninstall())) {
109
            return new Result(false, $tests->getList());
110
        }
111
112
        $loader = new ClassLoader();
113
        $loader->registerPackageCustomAutoloaders($package);
114
115
        $result = $package->uninstall();
116
        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...
117
            return new Result(false, $result->getList());
118
        }
119
120
        return new Result;
121
    }
122
123
    /**
124
     * Test a package for install
125
     *
126
     * @param PackageItem $item
127
     * @return \Buttress\Concrete\Service\Result
128
     */
129
    public function test(PackageItem $item)
130
    {
131
        if (!$this->connection->isConnected()) {
132
            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...
133
        }
134
135
        if (!$package = $this->getPackage($item)) {
136
            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...
137
        }
138
139
        return $package->getController()->testForInstall();
140
    }
141
142
    /**
143
     * Show information about a package
144
     *
145
     * @param PackageItem $package
146
     * @param \League\CLImate\CLImate $cli
147
     * @return \Buttress\Concrete\Service\Result
148
     */
149 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...
150
    {
151
        if (!$this->connection->isConnected()) {
152
            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...
153
        }
154
155
        $cli->flank($package->getHandle());
156
        $cli->out($package->isInstalled() ? 'Installed Version ' : 'Not Installed: ');
157
        $cli->bold()->inline($package->getVersion());
158
    }
159
160
    /**
161
     * Get a list of package item objects
162
     * @return PackageItem[]
163
     */
164
    public function all()
165
    {
166
        if (!$this->connection->isConnected()) {
167
            throw new RuntimeException('Not connected to concrete5 site.');
168
        }
169
170
        $installed = Package::getInstalledList();
171
        foreach ($installed as $item) {
172
            yield $this->factory->fromModern($item);
173
        }
174
175
        $notInstalled = Package::getAvailablePackages();
176
        foreach ($notInstalled as $item) {
177
            yield $this->factory->fromModern($item);
178
        }
179
    }
180
}
181