Failed Conditions
Push — master ( d49c03...6343d0 )
by Bernhard
05:03
created

src/Api/Package/Package.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the puli/manager package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\Manager\Api\Package;
13
14
use Exception;
15
use Puli\Manager\Assert\Assert;
16
17
/**
18
 * A configured package.
19
 *
20
 * @since  1.0
21
 *
22
 * @author Bernhard Schussek <[email protected]>
23
 */
24
class Package
25
{
26
    /**
27
     * @var string
28
     */
29
    private $name;
30
31
    /**
32
     * @var PackageFile
33
     */
34
    private $packageFile;
35
36
    /**
37
     * @var string
38
     */
39
    private $installPath;
40
41
    /**
42
     * @var InstallInfo
43
     */
44
    private $installInfo;
45
46
    /**
47
     * @var int
48
     */
49
    private $state;
50
51
    /**
52
     * @var Exception[]
53
     */
54
    private $loadErrors;
55
56
    /**
57
     * Creates a new package.
58
     *
59
     * @param PackageFile|null $packageFile The package file or `null` if the
60
     *                                      package file could not be loaded.
61
     * @param string           $installPath The absolute install path.
62
     * @param InstallInfo|null $installInfo The install info of this package.
63
     * @param Exception[]      $loadErrors  The errors that happened during
64
     *                                      loading of the package, if any.
65
     */
66 436
    public function __construct(PackageFile $packageFile = null, $installPath, InstallInfo $installInfo = null, array $loadErrors = array())
67
    {
68 436
        Assert::absoluteSystemPath($installPath);
69 436
        Assert::allIsInstanceOf($loadErrors, 'Exception');
70
71
        // If a package name was set during installation, that name wins over
72
        // the predefined name in the puli.json file (if any)
73 436
        $this->name = $installInfo && null !== $installInfo->getPackageName()
74 173
            ? $installInfo->getPackageName()
75 422
            : ($packageFile ? $packageFile->getPackageName() : null);
76
77 436
        if (null === $this->name) {
78 83
            $this->name = $this->getDefaultName();
79
        }
80
81
        // The path is stored both here and in the install info. While the
82
        // install info contains the path as it is stored in the install file
83
        // (i.e. relative or absolute), the install path of the package is
84
        // always an absolute path.
85 436
        $this->installPath = $installPath;
86 436
        $this->installInfo = $installInfo;
87 436
        $this->packageFile = $packageFile;
88 436
        $this->loadErrors = $loadErrors;
89
90 436
        if (!file_exists($installPath)) {
91 248
            $this->state = PackageState::NOT_FOUND;
92 298
        } elseif (count($loadErrors) > 0) {
93 5
            $this->state = PackageState::NOT_LOADABLE;
94
        } else {
95 297
            $this->state = PackageState::ENABLED;
96
        }
97 436
    }
98
99
    /**
100
     * Returns the name of the package.
101
     *
102
     * @return string The name of the package.
103
     */
104 376
    public function getName()
105
    {
106 376
        return $this->name;
107
    }
108
109
    /**
110
     * Returns the absolute path at which the package is installed.
111
     *
112
     * @return string The absolute install path of the package.
113
     */
114 99
    public function getInstallPath()
115
    {
116 99
        return $this->installPath;
117
    }
118
119
    /**
120
     * Returns the package file of the package.
121
     *
122
     * @return PackageFile|null The package file or `null` if the file could not
0 ignored issues
show
Consider making the return type a bit more specific; maybe use PackageFile.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
123
     *                          be loaded.
124
     */
125 270
    public function getPackageFile()
126
    {
127 270
        return $this->packageFile;
128
    }
129
130
    /**
131
     * Returns the package's install info.
132
     *
133
     * @return InstallInfo The install info.
134
     */
135 27
    public function getInstallInfo()
136
    {
137 27
        return $this->installInfo;
138
    }
139
140
    /**
141
     * Returns the error that occurred during loading of the package.
142
     *
143
     * @return Exception[] The errors or an empty array if the package was
144
     *                     loaded successfully.
145
     */
146 11
    public function getLoadErrors()
147
    {
148 11
        return $this->loadErrors;
149
    }
150
151
    /**
152
     * Returns the state of the package.
153
     *
154
     * @return int One of the {@link PackageState} constants.
155
     */
156 3
    public function getState()
157
    {
158 3
        return $this->state;
159
    }
160
161
    /**
162
     * Returns whether the package is enabled.
163
     *
164
     * @return bool Returns `true` if the state is {@link PackageState::ENABLED}.
165
     *
166
     * @see PackageState::ENABLED
167
     */
168 5
    public function isEnabled()
169
    {
170 5
        return PackageState::ENABLED === $this->state;
171
    }
172
173
    /**
174
     * Returns whether the package was not found.
175
     *
176
     * @return bool Returns `true` if the state is {@link PackageState::NOT_FOUND}.
177
     *
178
     * @see PackageState::NOT_FOUND
179
     */
180 1
    public function isNotFound()
181
    {
182 1
        return PackageState::NOT_FOUND === $this->state;
183
    }
184
185
    /**
186
     * Returns whether the package was not loadable.
187
     *
188
     * @return bool Returns `true` if the state is {@link PackageState::NOT_LOADABLE}.
189
     *
190
     * @see PackageState::NOT_LOADABLE
191
     */
192 3
    public function isNotLoadable()
193
    {
194 3
        return PackageState::NOT_LOADABLE === $this->state;
195
    }
196
197
    /**
198
     * Returns the default name of a package.
199
     */
200 72
    protected function getDefaultName()
201
    {
202 72
        return null;
203
    }
204
}
205