FileActivator   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 19
eloc 44
dl 0
loc 196
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A hasStatus() 0 7 2
A enable() 0 3 1
A config() 0 3 1
A setActive() 0 3 1
A flushCache() 0 3 1
A setActiveByName() 0 5 1
A getDomainsStatuses() 0 8 2
A writeJson() 0 3 1
A disable() 0 3 1
A getStatusesFilePath() 0 3 1
A reset() 0 7 2
A readJson() 0 7 2
A delete() 0 8 2
A __construct() 0 9 1
1
<?php
2
3
namespace Salah3id\Domains\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 Salah3id\Domains\Contracts\ActivatorInterface;
11
use Salah3id\Domains\Domain;
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 domains activation statuses
48
     *
49
     * @var array
50
     */
51
    private $domainsStatuses;
52
53
    /**
54
     * File used to store activation statuses
55
     *
56
     * @var string
57
     */
58
    private $statusesFile;
59
60
    public function __construct(Container $app)
61
    {
62
        $this->cache = $app['cache'];
63
        $this->files = $app['files'];
64
        $this->config = $app['config'];
65
        $this->statusesFile = $this->config('statuses-file');
66
        $this->cacheKey = $this->config('cache-key');
67
        $this->cacheLifetime = $this->config('cache-lifetime');
68
        $this->domainsStatuses = $this->getDomainsStatuses();
69
    }
70
71
    /**
72
     * Get the path of the file where statuses are stored
73
     *
74
     * @return string
75
     */
76
    public function getStatusesFilePath(): string
77
    {
78
        return $this->statusesFile;
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84
    public function reset(): void
85
    {
86
        if ($this->files->exists($this->statusesFile)) {
87
            $this->files->delete($this->statusesFile);
88
        }
89
        $this->domainsStatuses = [];
90
        $this->flushCache();
91
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96
    public function enable(Domain $domain): void
97
    {
98
        $this->setActiveByName($domain->getName(), true);
99
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104
    public function disable(Domain $domain): void
105
    {
106
        $this->setActiveByName($domain->getName(), false);
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112
    public function hasStatus(Domain $domain, bool $status): bool
113
    {
114
        if (!isset($this->domainsStatuses[$domain->getName()])) {
115
            return $status === false;
116
        }
117
118
        return $this->domainsStatuses[$domain->getName()] === $status;
119
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124
    public function setActive(Domain $domain, bool $active): void
125
    {
126
        $this->setActiveByName($domain->getName(), $active);
127
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132
    public function setActiveByName(string $name, bool $status): void
133
    {
134
        $this->domainsStatuses[$name] = $status;
135
        $this->writeJson();
136
        $this->flushCache();
137
    }
138
139
    /**
140
     * @inheritDoc
141
     */
142
    public function delete(Domain $domain): void
143
    {
144
        if (!isset($this->domainsStatuses[$domain->getName()])) {
145
            return;
146
        }
147
        unset($this->domainsStatuses[$domain->getName()]);
148
        $this->writeJson();
149
        $this->flushCache();
150
    }
151
152
    /**
153
     * Writes the activation statuses in a file, as json
154
     */
155
    private function writeJson(): void
156
    {
157
        $this->files->put($this->statusesFile, json_encode($this->domainsStatuses, JSON_PRETTY_PRINT));
158
    }
159
160
    /**
161
     * Reads the json file that contains the activation statuses.
162
     * @return array
163
     * @throws FileNotFoundException
164
     */
165
    private function readJson(): array
166
    {
167
        if (!$this->files->exists($this->statusesFile)) {
168
            return [];
169
        }
170
171
        return json_decode($this->files->get($this->statusesFile), true);
172
    }
173
174
    /**
175
     * Get domains 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
    private function getDomainsStatuses(): array
181
    {
182
        if (!$this->config->get('domains.cache.enabled')) {
183
            return $this->readJson();
184
        }
185
186
        return $this->cache->store($this->config->get('domains.cache.driver'))->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
    private function config(string $key, $default = null)
199
    {
200
        return $this->config->get('domains.activators.file.' . $key, $default);
201
    }
202
203
    /**
204
     * Flushes the domains activation statuses cache
205
     */
206
    private function flushCache(): void
207
    {
208
        $this->cache->store($this->config->get('domains.cache.driver'))->forget($this->cacheKey);
209
    }
210
}
211