Completed
Pull Request — master (#47)
by Mateusz
05:43
created

CacheFile::removeModuleFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the puli/manager Module.
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\Module\InstallInfo;
16
use Puli\Manager\Api\Module\ModuleFile;
17
use Puli\Manager\Assert\Assert;
18
19
/**
20
 * Stores combined Modules 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 ModuleFile[]
35
     */
36
    private $moduleFiles = 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 41
    public function __construct($path = null)
49
    {
50 41
        Assert::nullOrAbsoluteSystemPath($path);
51
52 39
        $this->path = $path;
53 39
    }
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 8
    public function getPath()
62
    {
63 8
        return $this->path;
64
    }
65
66
    /**
67
     * Sets the Module files.
68
     *
69
     * @param ModuleFile[] $moduleFiles The Module files
70
     */
71 2
    public function setModuleFiles(array $moduleFiles)
72
    {
73 2
        $this->moduleFiles = array();
74
75 2
        foreach ($moduleFiles as $moduleFile) {
76 1
            $this->addModuleFile($moduleFile);
77
        }
78 2
    }
79
80
    /**
81
     * Adds a Module to the cache file.
82
     *
83
     * @param ModuleFile $moduleFile The added Module file
84
     */
85 23
    public function addModuleFile(ModuleFile $moduleFile)
86
    {
87 23
        $this->moduleFiles[$moduleFile->getModuleName()] = $moduleFile;
88
89 23
        ksort($this->moduleFiles);
90 23
    }
91
92
    /**
93
     * Removes a Module file from the cache file.
94
     *
95
     * @param string $moduleName The Module name
96
     */
97 1
    public function removeModuleFile($moduleName)
98
    {
99 1
        unset($this->moduleFiles[$moduleName]);
100 1
    }
101
102
    /**
103
     * Removes all Module files from the cache file.
104
     */
105 1
    public function clearModuleFiles()
106
    {
107 1
        $this->moduleFiles = array();
108 1
    }
109
110
    /**
111
     * Returns the Module file with the given name.
112
     *
113
     * @param string $moduleName The Module name
114
     *
115
     * @return ModuleFile The Module with the passed name
116
     *
117
     * @throws InvalidArgumentException If the Module was not found
118
     */
119 2 View Code Duplication
    public function getModuleFile($moduleName)
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...
120
    {
121 2
        Assert::ModuleName($moduleName);
122
123 2
        if (!isset($this->moduleFiles[$moduleName])) {
124
            throw new InvalidArgumentException(sprintf('Could not find a Module named %s.', $moduleName));
125
        }
126
127 2
        return $this->moduleFiles[$moduleName];
128
    }
129
130
    /**
131
     * Returns the Module files in the cache file.
132
     *
133
     * @return ModuleFile[] The Module files in the cache file
134
     */
135 12
    public function getModuleFiles()
136
    {
137 12
        return $this->moduleFiles;
138
    }
139
140
    /**
141
     * Returns whether a Module with the given name exists.
142
     *
143
     * @param string $moduleName The Module name
144
     *
145
     * @return bool Whether a Module with this name exists
146
     */
147 5
    public function hasModuleFile($moduleName)
148
    {
149 5
        return isset($this->moduleFiles[$moduleName]);
150
    }
151
152
    /**
153
     * Returns whether a cache file contains any Module files.
154
     *
155
     * @return bool Whether a cache file contains any Module files
156
     */
157 4
    public function hasModuleFiles()
158
    {
159 4
        return count($this->moduleFiles) > 0;
160
    }
161
162
    /**
163
     * Adds install info to the cache file.
164
     *
165
     * @param InstallInfo $installInfo The Module install info
166
     */
167 22
    public function addInstallInfo(InstallInfo $installInfo)
168
    {
169 22
        $this->installInfos[$installInfo->getModuleName()] = $installInfo;
170
171 22
        ksort($this->installInfos);
172 22
    }
173
174
    /**
175
     * Removes install info file from the cache file.
176
     *
177
     * @param string $moduleName The Module name
178
     */
179 1
    public function removeInstallInfo($moduleName)
180
    {
181 1
        unset($this->installInfos[$moduleName]);
182 1
    }
183
184
    /**
185
     * Removes all Module install info from the cache file.
186
     */
187 3
    public function clearInstallInfo()
188
    {
189 3
        $this->installInfos = array();
190 3
    }
191
192
    /**
193
     * Returns the install info with the given Module name.
194
     *
195
     * @param string $moduleName The Module name
196
     *
197
     * @return InstallInfo The Module install info with the passed name
198
     *
199
     * @throws InvalidArgumentException If the install info was not found
200
     */
201 5 View Code Duplication
    public function getInstallInfo($moduleName)
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...
202
    {
203 5
        Assert::ModuleName($moduleName);
204
205 5
        if (!isset($this->installInfos[$moduleName])) {
206
            throw new InvalidArgumentException(sprintf('Could not find a Module named %s.', $moduleName));
207
        }
208
209 5
        return $this->installInfos[$moduleName];
210
    }
211
212
    /**
213
     * Returns the install info for all Modules in the cache file.
214
     *
215
     * @return InstallInfo[] The install info for all Modules in the cache file
216
     */
217 5
    public function getInstallInfos()
218
    {
219 5
        return $this->installInfos;
220
    }
221
222
    /**
223
     * Returns whether install info for a Module with the given name exists.
224
     *
225
     * @param string $moduleName The Module name
226
     *
227
     * @return bool Whether install info for a Module with this name exists
228
     */
229 4
    public function hasInstallInfo($moduleName)
230
    {
231 4
        return isset($this->installInfos[$moduleName]);
232
    }
233
234
    /**
235
     * Returns whether a cache file contains any Module install info.
236
     *
237
     * @return bool Whether a cache file contains any Module install info
238
     */
239 3
    public function hasInstallInfos()
240
    {
241 3
        return count($this->installInfos) > 0;
242
    }
243
}
244