Completed
Pull Request — master (#1085)
by
unknown
54:16 queued 45:25
created

FileActivator::readJson()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules\Activators;
4
5
use Illuminate\Cache\CacheManager;
6
use Illuminate\Config\Repository as Config;
7
use Illuminate\Container\Container;
8
use Illuminate\Contracts\Filesystem\FileNotFoundException;
9
use Illuminate\Filesystem\Filesystem;
10
use Nwidart\Modules\Contracts\ActivatorInterface;
11
use Nwidart\Modules\Module;
12
13
class FileActivator implements ActivatorInterface
14
{
15
    /**
16
     * Laravel cache instance
17
     *
18
     * @var CacheManager
19
     */
20
    private $cache;
21
22
    /**
23
     * Laravel Filesystem instance
24
     *
25
     * @var Filesystem
26
     */
27
    private $files;
28
29
    /**
30
     * Laravel config instance
31
     *
32
     * @var Config
33
     */
34
    private $config;
35
36
    /**
37
     * @var string
38
     */
39
    private $cacheKey;
40
41
    /**
42
     * @var string
43
     */
44
    private $cacheLifetime;
45
46
    /**
47
     * Array of modules activation statuses
48
     *
49
     * @var array
50
     */
51
    private $modulesStatuses;
52
53
    /**
54
     * File used to store activation statuses
55
     *
56
     * @var string
57
     */
58
    private $statusesFile;
59
60 74
    public function __construct(Container $app)
61
    {
62 74
        $this->cache = $app['cache'];
63 74
        $this->files = $app['files'];
64 74
        $this->config = $app['config'];
65 74
        $this->statusesFile = $this->config('statuses-file');
66 74
        $this->cacheKey = $this->config('cache-key');
67 74
        $this->cacheLifetime = $this->config('cache-lifetime');
68 74
        $this->modulesStatuses = $this->getModulesStatuses();
69 74
    }
70
71
    /**
72
     * Get the path of the file where statuses are stored
73
     *
74
     * @return string
75
     */
76 2
    public function getStatusesFilePath(): string
77
    {
78 2
        return $this->statusesFile;
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84 71
    public function reset(): void
85
    {
86 71
        if ($this->files->exists($this->statusesFile)) {
87 12
            $this->files->delete($this->statusesFile);
88
        }
89 71
        $this->modulesStatuses = [];
90 71
        $this->flushCache();
91 71
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96 6
    public function enable(Module $module): void
97
    {
98 6
        $this->setActiveByName($module->getName(), true);
99 6
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104 5
    public function disable(Module $module): void
105
    {
106 5
        $this->setActiveByName($module->getName(), false);
107 5
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112 15
    public function hasStatus(Module $module, bool $status): bool
113
    {
114 15
        if (!isset($this->modulesStatuses[$module->getName()])) {
115 9
            return $status === false;
116
        }
117
118 6
        return $this->modulesStatuses[$module->getName()] === $status;
119
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124 5
    public function setActive(Module $module, bool $active): void
125
    {
126 5
        $this->setActiveByName($module->getName(), $active);
127 5
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132 12
    public function setActiveByName(string $name, bool $status): void
133
    {
134 12
        $this->modulesStatuses[$name] = $status;
135 12
        $this->writeJson();
136 12
        $this->flushCache();
137 12
    }
138
139
    /**
140
     * @inheritDoc
141
     */
142
    public function delete(Module $module): void
143
    {
144
        if (!isset($this->modulesStatuses[$module->getName()])) {
145
            return;
146
        }
147
        unset($this->modulesStatuses[$module->getName()]);
148
        $this->writeJson();
149
        $this->flushCache();
150
    }
151
152
    /**
153
     * Writes the activation statuses in a file, as json
154
     */
155 12
    private function writeJson(): void
156
    {
157 12
        $this->files->put($this->statusesFile, json_encode($this->modulesStatuses, JSON_PRETTY_PRINT));
158 12
    }
159
160
    /**
161
     * Reads the json file that contains the activation statuses.
162
     * @return array
163
     * @throws FileNotFoundException
164
     */
165 74
    private function readJson(): array
166
    {
167 74
        if (!$this->files->exists($this->statusesFile)) {
168 74
            return [];
169
        }
170
171
        return json_decode($this->files->get($this->statusesFile), true);
172
    }
173
174
    /**
175
     * Get modules statuses, either from the cache or from
176
     * the json statuses file if the cache is disabled.
177
     * @return array
178
     * @throws FileNotFoundException
179
     */
180 74
    private function getModulesStatuses(): array
181
    {
182 74
        if (!$this->config->get('modules.cache.enabled')) {
183 74
            return $this->readJson();
184
        }
185
186
        return $this->cache->remember($this->cacheKey, $this->cacheLifetime, function () {
187
            return $this->readJson();
188
        });
189
    }
190
191
    /**
192
     * Reads a config parameter under the 'activators.file' key
193
     *
194
     * @param  string $key
195
     * @param  $default
196
     * @return mixed
197
     */
198 74
    private function config(string $key, $default = null)
199
    {
200 74
        return $this->config->get('modules.activators.file.' . $key, $default);
201
    }
202
203
    /**
204
     * Flushes the modules activation statuses cache
205
     */
206 71
    private function flushCache(): void
207
    {
208 71
        $this->cache->forget($this->cacheKey);
0 ignored issues
show
Bug introduced by
The method forget() does not exist on Illuminate\Cache\CacheManager. Did you maybe mean forgetDriver()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
209 71
    }
210
}
211