Completed
Push — master ( 4fafa1...e65f27 )
by Nicolas
12:12
created

src/Activators/FileActivator.php (1 issue)

Labels
Severity

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
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 193
    public function __construct(Container $app)
61
    {
62 193
        $this->cache = $app['cache'];
63 193
        $this->files = $app['files'];
64 193
        $this->config = $app['config'];
65 193
        $this->statusesFile = $this->config('statuses-file');
66 193
        $this->cacheKey = $this->config('cache-key');
67 193
        $this->cacheLifetime = $this->config('cache-lifetime');
68 193
        $this->modulesStatuses = $this->getModulesStatuses();
69 193
    }
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 98
    public function reset(): void
85
    {
86 98
        if ($this->files->exists($this->statusesFile)) {
87 40
            $this->files->delete($this->statusesFile);
88
        }
89 98
        $this->modulesStatuses = [];
90 98
        $this->flushCache();
91 98
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96 7
    public function enable(Module $module): void
97
    {
98 7
        $this->setActiveByName($module->getName(), true);
99 7
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104 6
    public function disable(Module $module): void
105
    {
106 6
        $this->setActiveByName($module->getName(), false);
107 6
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112 18
    public function hasStatus(Module $module, bool $status): bool
113
    {
114 18
        if (!isset($this->modulesStatuses[$module->getName()])) {
115 9
            return $status === false;
116
        }
117
118 9
        return $this->modulesStatuses[$module->getName()] === $status;
119
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124 4
    public function setActive(Module $module, bool $active): void
125
    {
126 4
        $this->setActiveByName($module->getName(), $active);
127 4
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132 131
    public function setActiveByName(string $name, bool $status): void
133
    {
134 131
        $this->modulesStatuses[$name] = $status;
135 131
        $this->writeJson();
136 131
        $this->flushCache();
137 131
    }
138
139
    /**
140
     * @inheritDoc
141
     */
142 100
    public function delete(Module $module): void
143
    {
144 100
        if (!isset($this->modulesStatuses[$module->getName()])) {
145
            return;
146
        }
147 100
        unset($this->modulesStatuses[$module->getName()]);
148 100
        $this->writeJson();
149 100
        $this->flushCache();
150 100
    }
151
152
    /**
153
     * Writes the activation statuses in a file, as json
154
     */
155 131
    private function writeJson(): void
156
    {
157 131
        $this->files->put($this->statusesFile, json_encode($this->modulesStatuses, JSON_PRETTY_PRINT));
158 131
    }
159
160
    /**
161
     * Reads the json file that contains the activation statuses.
162
     * @return array
163
     * @throws FileNotFoundException
164
     */
165 193
    private function readJson(): array
166
    {
167 193
        if (!$this->files->exists($this->statusesFile)) {
168 101
            return [];
169
        }
170
171 92
        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 193
    private function getModulesStatuses(): array
181
    {
182 193
        if (!$this->config->get('modules.cache.enabled')) {
183 193
            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 193
    private function config(string $key, $default = null)
199
    {
200 193
        return $this->config->get('modules.activators.file.' . $key, $default);
201
    }
202
203
    /**
204
     * Flushes the modules activation statuses cache
205
     */
206 190
    private function flushCache(): void
207
    {
208 190
        $this->cache->forget($this->cacheKey);
0 ignored issues
show
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 190
    }
210
}
211