Completed
Pull Request — master (#2)
by Randall
04:53
created

FileActivator   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
eloc 44
c 1
b 0
f 0
dl 0
loc 132
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getModulesStatuses() 0 8 2
A readJson() 0 7 2
A getStatusesFilePath() 0 3 1
A reset() 0 9 2
A hasStatus() 0 7 2
A setActiveByName() 0 7 1
A delete() 0 11 2
A disable() 0 3 1
A __construct() 0 9 1
A writeJson() 0 3 1
A enable() 0 3 1
A flushCache() 0 3 1
A config() 0 3 1
A setActive() 0 3 1
1
<?php
2
3
namespace Rawilk\LaravelModules\Activators;
4
5
use Illuminate\Contracts\Container\Container;
6
use Rawilk\LaravelModules\Contracts\Activator;
7
use Rawilk\LaravelModules\Module;
8
9
class FileActivator implements Activator
10
{
11
    /** @var \Illuminate\Cache\CacheManager */
12
    private $cache;
13
14
    /** @var string */
15
    private $cacheKey;
16
17
    /** @var int */
18
    private $cacheLifetime;
19
20
    /** @var \Illuminate\Contracts\Config\Repository */
21
    private $config;
22
23
    /** @var \Illuminate\Filesystem\Filesystem */
24
    private $files;
25
26
    /** @var array */
27
    private $moduleStatuses;
28
29
    /** @var string */
30
    private $statusesFile;
31
32
    /**
33
     * @param \Illuminate\Contracts\Container\Container $app
34
     */
35
    public function __construct(Container $app)
36
    {
37
        $this->cache = $app['cache'];
38
        $this->files = $app['files'];
39
        $this->config = $app['config'];
40
        $this->statusesFile = $this->config('statuses-file');
41
        $this->cacheKey = $this->config('cache-key');
42
        $this->cacheLifetime = $this->config('cache-lifetime');
43
        $this->moduleStatuses = $this->getModulesStatuses();
44
    }
45
46
    public function delete(Module $module): void
47
    {
48
        if (! isset($this->moduleStatuses[$module->getName()])) {
49
            return;
50
        }
51
52
        unset($this->moduleStatuses[$module->getName()]);
53
54
        $this->writeJson();
55
56
        $this->flushCache();
57
    }
58
59
    public function disable(Module $module): void
60
    {
61
        $this->setActiveByName($module->getName(), false);
62
    }
63
64
    public function enable(Module $module): void
65
    {
66
        $this->setActiveByName($module->getName(), true);
67
    }
68
69
    public function getStatusesFilePath(): string
70
    {
71
        return $this->statusesFile;
72
    }
73
74
    public function hasStatus(Module $module, bool $status): bool
75
    {
76
        if (! isset($this->moduleStatuses[$module->getName()])) {
77
            return $status === false;
78
        }
79
80
        return $this->moduleStatuses[$module->getName()] === $status;
81
    }
82
83
    public function reset(): void
84
    {
85
        if ($this->files->exists($this->statusesFile)) {
86
            $this->files->delete($this->statusesFile);
87
        }
88
89
        $this->moduleStatuses = [];
90
91
        $this->flushCache();
92
    }
93
94
    public function setActive(Module $module, bool $active): void
95
    {
96
        $this->setActiveByName($module->getName(), $active);
97
    }
98
99
    public function setActiveByName(string $name, bool $active): void
100
    {
101
        $this->moduleStatuses[$name] = $active;
102
103
        $this->writeJson();
104
105
        $this->flushCache();
106
    }
107
108
    private function config(string $key, $default = null)
109
    {
110
        return $this->config->get("modules.activators.file.{$key}", $default);
111
    }
112
113
    private function flushCache(): void
114
    {
115
        $this->cache->forget($this->cacheKey);
116
    }
117
118
    private function getModulesStatuses(): array
119
    {
120
        if (! $this->config->get('modules.cache.enabled')) {
121
            return $this->readJson();
122
        }
123
124
        return $this->cache->remember($this->cacheKey, $this->cacheLifetime, function () {
125
            return $this->readJson();
126
        });
127
    }
128
129
    private function readJson(): array
130
    {
131
        if (! $this->files->exists($this->statusesFile)) {
132
            return [];
133
        }
134
135
        return json_decode($this->files->get($this->statusesFile), true);
136
    }
137
138
    private function writeJson(): void
139
    {
140
        $this->files->put($this->statusesFile, json_encode($this->moduleStatuses, JSON_PRETTY_PRINT));
141
    }
142
}
143