Failed Conditions
Pull Request — master (#47)
by Mateusz
09:48
created

CacheManagerImpl::buildModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

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 7
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 2
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\Cache;
13
14
use Puli\Manager\Api\Cache\CacheFile;
15
use Puli\Manager\Api\Cache\CacheManager;
16
use Puli\Manager\Api\Config\Config;
17
use Puli\Manager\Api\Context\ProjectContext;
18
use Puli\Manager\Api\Module\Module;
19
use Puli\Manager\Api\Module\ModuleList;
20
use Puli\Manager\Api\Module\ModuleFile;
21
use Puli\Manager\Api\Module\ModuleManager;
22
use Puli\Manager\Api\Module\RootModule;
23
use Puli\Manager\Assert\Assert;
24
use Puli\Manager\Json\JsonStorage;
25
use Webmozart\Expression\Expr;
26
use Webmozart\Expression\Expression;
27
use Webmozart\PathUtil\Path;
28
29
/**
30
 * Manages cached Modules information.
31
 *
32
 * @since  1.0
33
 *
34
 * @author Mateusz Sojda <[email protected]>
35
 */
36
class CacheManagerImpl implements CacheManager
37
{
38
    /**
39
     * @var ModuleManager
40
     */
41
    private $moduleManager;
42
43
    /**
44
     * @var JsonStorage
45
     */
46
    private $storage;
47
48
    /**
49
     * @var ProjectContext
50
     */
51
    private $context;
52
53
    /**
54
     * @var string
55
     */
56
    private $rootDir;
57
58
    /**
59
     * Creates new cache manager.
60
     *
61
     * @param ModuleManager     $moduleManager   The Module manager.
62
     * @param JsonStorage       $storage         The file storage.
63
     * @param ProjectContext    $context         Project context.
64
     */
65 1
    public function __construct(ModuleManager $moduleManager, JsonStorage $storage, ProjectContext $context)
66
    {
67 1
        $this->moduleManager = $moduleManager;
68 1
        $this->storage = $storage;
69 1
        $this->context = $context;
70 1
        $this->rootDir = $context->getRootDirectory();
71 1
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 1
    public function getContext()
77
    {
78 1
        return $this->context;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function getCacheFile()
85
    {
86
        $path = $this->getContext()->getConfig()->get(Config::CACHE_FILE);
87
        $path = Path::makeAbsolute($path, $this->rootDir);
88
89
        if (false == $this->storage->fileExists($path)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
90
            $this->refreshCacheFile();
91
        }
92
93
        $cacheFile = $this->storage->loadCacheFile($path);
94
95
        return $cacheFile;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function refreshCacheFile()
102
    {
103
        $path = $this->getContext()->getConfig()->get(Config::CACHE_FILE);
104
        $path = Path::makeAbsolute($path, $this->rootDir);
105
106
        if ($this->storage->fileExists($path) && false === $this->isRootModuleFileModified()) {
107
            return;
108
        }
109
110
        $cacheFile = new CacheFile(Path::makeAbsolute($path, $this->rootDir));
111
112
        foreach ($this->getInstalledModules() as $module) {
113
            $moduleFile = $module->getModuleFile();
114
            if (false === $moduleFile instanceof ModuleFile) {
115
                continue;
116
            }
117
118
            $cacheFile->addModuleFile($moduleFile);
119
120
            $installInfo = $module->getInstallInfo();
121
            $cacheFile->addInstallInfo($installInfo);
122
        }
123
124
        $this->storage->saveCacheFile($cacheFile);
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function getModule($name)
131
    {
132
        Assert::string($name, 'The Module name must be a string. Got: %s');
133
134
        $cacheFile = $this->getCacheFile();
135
        $moduleFile = $cacheFile->getModuleFile($name);
136
137
        return $this->buildModule($moduleFile, $cacheFile);
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function getRootModule()
144
    {
145
        return new RootModule(
146
            $this->getContext()->getRootModuleFile(),
147
            $this->rootDir
148
        );
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function getModules()
155
    {
156
        $cacheFile = $this->getCacheFile();
157
        $modules = new ModuleList();
158
159
        foreach ($cacheFile->getModuleFiles() as $moduleFile) {
160
            $module = $this->buildModule($moduleFile, $cacheFile);
161
            $modules->add($module);
162
        }
163
164
        return $modules;
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    public function findModules(Expression $expr)
171
    {
172
        $cacheFile = $this->getCacheFile();
173
        $modules = new ModuleList();
174
175
        foreach ($cacheFile->getModuleFiles() as $moduleFile) {
176
            $module = $this->buildModule($moduleFile, $cacheFile);
177
178
            if ($expr->evaluate($module)) {
179
                $modules->add($module);
180
            }
181
        }
182
183
        return $modules;
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function hasModule($name)
190
    {
191
        Assert::string($name, 'The Module name must be a string. Got: %s');
192
193
        $cacheFile = $this->getCacheFile();
194
195
        return $cacheFile->hasModuleFile($name);
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201
    public function hasModules(Expression $expr = null)
202
    {
203
        $cacheFile = $this->getCacheFile();
204
205
        if (!$expr) {
206
            return $cacheFile->hasModuleFiles();
207
        }
208
209
        foreach ($this->getModules() as $module) {
210
            if ($expr->evaluate($module)) {
211
                return true;
212
            }
213
        }
214
215
        return false;
216
    }
217
218
    private function getInstalledModules()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
219
    {
220
        $modules = $this->moduleManager->findModules(Expr::true());
221
222
        return $modules->getInstalledModules();
223
    }
224
225
    private function buildModule(ModuleFile $moduleFile, CacheFile $cacheFile)
226
    {
227
        $installInfo = $cacheFile->getInstallInfo($moduleFile->getModuleName());
228
        $installPath = Path::makeAbsolute($installInfo->getInstallPath(), $this->rootDir);
229
230
        return new Module($moduleFile, $installPath, $installInfo);
231
    }
232
233
    private function isRootModuleFileModified()
234
    {
235
        $cacheFilePath = $this->getContext()->getConfig()->get(Config::CACHE_FILE);
236
        $cacheFilePath = Path::makeAbsolute($cacheFilePath, $this->rootDir);
237
238
        clearstatcache(true, $cacheFilePath);
239
        $cacheFileMtime = filemtime($cacheFilePath);
240
241
        $rootModuleFilePath = $this->getRootModule()->getModuleFile()->getPath();
242
243
        if (false === $this->storage->fileExists($rootModuleFilePath)) {
244
            return true;
245
        }
246
247
        clearstatcache(true, $rootModuleFilePath);
248
        $rootModuleFileMtime = filemtime($rootModuleFilePath);
249
250
        if ($rootModuleFileMtime > $cacheFileMtime) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $rootModuleFileMtime > $cacheFileMtime;.
Loading history...
251
            return true;
252
        }
253
254
        return false;
255
    }
256
}