Failed Conditions
Pull Request — master (#47)
by Mateusz
24:21
created

CacheFile::clearPackageFiles()   A

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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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\Cache;
13
14
use InvalidArgumentException;
15
use Puli\Manager\Api\Package\InstallInfo;
16
use Puli\Manager\Api\Package\PackageFile;
17
use Puli\Manager\Assert\Assert;
18
19
/**
20
 * Stores combined packages configuration.
21
 *
22
 * @since  1.0
23
 *
24
 * @author Mateusz Sojda <[email protected]>
25
 */
26
class CacheFile
27
{
28
    /**
29
     * @var string|null
30
     */
31
    private $path;
32
33
    /**
34
     * @var PackageFile[]
35
     */
36
    private $packageFiles = array();
37
38
    /**
39
     * @var InstallInfo[]
40
     */
41
    private $installInfos = array();
42
43
    /**
44
     * Creates new CacheFile.
45
     *
46
     * @param string|null $path The path where the cache file is stored.
47
     */
48
    public function __construct($path = null)
49
    {
50
        Assert::nullOrAbsoluteSystemPath($path);
51
52
        $this->path = $path;
53
    }
54
55
    /**
56
     * Returns the path to the cache file.
57
     *
58
     * @return string|null The path or `null` if this file is not stored on the
59
     *                     file system.
60
     */
61
    public function getPath()
62
    {
63
        return $this->path;
64
    }
65
66
    /**
67
     * Sets the package files.
68
     *
69
     * @param PackageFile[] $packageFiles The package files.
70
     */
71
    public function setPackageFiles(array $packageFiles)
72
    {
73
        $this->packageFiles = $packageFiles;
74
    }
75
76
    /**
77
     * Adds a package to the cache file.
78
     *
79
     * @param PackageFile $packageFile The added package file.
80
     */
81
    public function addPackageFile(PackageFile $packageFile)
82
    {
83
        $this->packageFiles[$packageFile->getPackageName()] = $packageFile;
84
85
        ksort($this->packageFiles);
86
    }
87
88
    /**
89
     * Removes a package file from the cache file.
90
     *
91
     * @param string $packageName The package name.
92
     */
93
    public function removePackageFile($packageName)
94
    {
95
        unset($this->packageFiles[$packageName]);
96
    }
97
98
    /**
99
     * Removes all package files from the cache file.
100
     */
101
    public function clearPackageFiles()
102
    {
103
        $this->packageFiles = array();
104
    }
105
106
    /**
107
     * Returns the package file with the given name.
108
     *
109
     * @param string $packageName The package name.
110
     *
111
     * @return PackageFile The package with the passed name.
112
     *
113
     * @throws InvalidArgumentException If the package was not found.
114
     */
115 View Code Duplication
    public function getPackageFile($packageName)
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...
116
    {
117
        Assert::packageName($packageName);
118
119
        if (!isset($this->packageFiles[$packageName])) {
120
            throw new InvalidArgumentException(sprintf("Could not find a package named %s.", $packageName));
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Could not find a package named %s. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
121
        }
122
123
        return $this->packageFiles[$packageName];
124
    }
125
126
    /**
127
     * Returns the package files in the cache file.
128
     *
129
     * @return PackageFile[] The package files in the cache file.
130
     */
131
    public function getPackageFiles()
132
    {
133
        return $this->packageFiles;
134
    }
135
136
    /**
137
     * Returns whether a package with the given name exists.
138
     *
139
     * @param string $packageName The package name.
140
     *
141
     * @return bool Whether a package with this name exists.
142
     */
143
    public function hasPackageFile($packageName)
144
    {
145
        return isset($this->packageFiles[$packageName]);
146
    }
147
148
    /**
149
     * Returns whether a cache file contains any package files.
150
     *
151
     * @return bool Whether a cache file contains any package files.
152
     */
153
    public function hasPackageFiles()
154
    {
155
        return count($this->packageFiles) > 0;
156
    }
157
158
    /**
159
     * Adds install info to the cache file.
160
     *
161
     * @param InstallInfo $installInfo The package install info.
162
     */
163
    public function addInstallInfo(InstallInfo $installInfo)
164
    {
165
        $this->installInfos[$installInfo->getPackageName()] = $installInfo;
166
167
        ksort($this->installInfos);
168
    }
169
170
    /**
171
     * Removes install info file from the cache file.
172
     *
173
     * @param string $packageName The package name.
174
     */
175
    public function removeInstallInfo($packageName)
176
    {
177
        unset($this->installInfos[$packageName]);
178
    }
179
180
    /**
181
     * Removes all package install info from the cache file.
182
     */
183
    public function clearInstallInfo()
184
    {
185
        $this->installInfos = array();
186
    }
187
188
    /**
189
     * Returns the install info with the given package name.
190
     *
191
     * @param string $packageName The package name.
192
     *
193
     * @return InstallInfo The package install info with the passed name.
194
     *
195
     * @throws InvalidArgumentException If the install info was not found.
196
     */
197 View Code Duplication
    public function getInstallInfo($packageName)
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...
198
    {
199
        Assert::packageName($packageName);
200
201
        if (!isset($this->installInfos[$packageName])) {
202
            throw new InvalidArgumentException(sprintf("Could not find a package named %s.", $packageName));
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Could not find a package named %s. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
203
        }
204
205
        return $this->installInfos[$packageName];
206
    }
207
208
    /**
209
     * Returns the install info for all packages in the cache file.
210
     *
211
     * @return InstallInfo[] The install info for all packages in the cache file.
212
     */
213
    public function getInstallInfos()
214
    {
215
        return $this->installInfos;
216
    }
217
218
    /**
219
     * Returns whether install info for a package with the given name exists.
220
     *
221
     * @param string $packageName The package name.
222
     *
223
     * @return bool Whether install info for a package with this name exists.
224
     */
225
    public function hasInstallInfo($packageName)
226
    {
227
        return isset($this->installInfos[$packageName]);
228
    }
229
230
    /**
231
     * Returns whether a cache file contains any package install info.
232
     *
233
     * @return bool Whether a cache file contains any package install info.
234
     */
235
    public function hasInstallInfos()
236
    {
237
        return count($this->installInfos) > 0;
238
    }
239
}