Completed
Pull Request — master (#1163)
by
unknown
09:01
created

DatabaseModule::isEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules\Laravel;
4
5
use Exception;
6
use Illuminate\Container\Container;
7
use Illuminate\Filesystem\Filesystem;
8
use Nwidart\Modules\Entities\ModuleEntity;
9
10
class DatabaseModule extends Module
11
{
12
    /**
13
     * @var array
14
     */
15
    public $attributes;
16
17
    /**
18
     * DatabaseModule constructor.
19
     *
20
     * @inheritDoc
21
     */
22
    public function __construct(Container $app, string $name, $path)
23
    {
24
        parent::__construct($app, $name, $path);
25
        $this->loadAttributes();
26
    }
27
28
    /**
29
     * @return ModuleEntity
30
     */
31
    public function getModel()
32
    {
33
        return new ModuleEntity();
34
    }
35
36
    /**
37
     * Load attributes.
38
     *
39
     * @return mixed
40
     * @throws Exception
41
     */
42
    private function loadAttributes()
43
    {
44
        if (!$this->getAttributes()) {
45
46
            // Try to get from cache first.
47
            $attributes = [];
48
            if (config('modules.cache.enabled')) {
49
                if ($modules = cache()->get(config('modules.cache.key'))) {
50
                    foreach ($modules as $module) {
51
                        if ($this->getName() == $module['name']) {
52
                            $attributes = $module;
53
                        }
54
                    }
55
                }
56
            }
57
58
            // Find from database. Throw error if still not found.
59
            if (!isset($attributes['is_active'])) {
60
                $attributes = $this->getModel()->where('name', $this->getName())->firstOrFail()->toArray();
61
            }
62
63
            $this->setAttributes($attributes);
64
        }
65
66
        return $this->attributes;
67
    }
68
69
    /**
70
     * Get attributes.
71
     *
72
     * @return array
73
     */
74
    public function getAttributes()
75
    {
76
        return $this->attributes;
77
    }
78
79
    /**
80
     * Set attributes.
81
     *
82
     * @param array $attributes
83
     */
84
    public function setAttributes($attributes)
85
    {
86
        $this->attributes = $attributes;
87
    }
88
89
    /**
90
     * Get a specific data from json file by given the key.
91
     *
92
     * @param string $key
93
     * @param null   $default
94
     *
95
     * @return mixed
96
     */
97
    public function get(string $key, $default = null)
98
    {
99
        return isset($this->attributes[$key]) ? $this->attributes[$key] : $default;
100
    }
101
102
    /**
103
     * Determine whether the given status same with the current module status.
104
     *
105
     * @param bool $status
106
     *
107
     * @return bool
108
     */
109
    public function isStatus(bool $status): bool
110
    {
111
        return $this->isEnabled();
112
    }
113
114
    /**
115
     * Determine whether the current module activated.
116
     *
117
     * @return bool
118
     */
119
    public function isEnabled(): bool
120
    {
121
        return $this->attributes['is_active'];
122
    }
123
124
    /**
125
     *  Determine whether the current module not disabled.
126
     *
127
     * @return bool
128
     */
129
    public function isDisabled(): bool
130
    {
131
        return !$this->isEnabled();
132
    }
133
134
    /**
135
     * Set active state for current module.
136
     *
137
     * @param bool $active
138
     *
139
     * @return void
140
     */
141
    public function setActive(bool $active): void
142
    {
143
        $this->getModel()->where(['name' => $this->getName()])->update(['is_active' => $active]);
144
        $this->flushCache();
145
    }
146
147
    /**
148
     * Disable the current module.
149
     */
150 View Code Duplication
    public function disable(): void
151
    {
152
        $this->fireEvent('disabling');
153
154
        $this->getModel()->where(['name' => $this->getName()])->update(['is_active' => 0]);
155
        $this->flushCache();
156
157
        $this->fireEvent('disabled');
158
    }
159
160
    /**
161
     * Enable the current module.
162
     */
163 View Code Duplication
    public function enable(): void
164
    {
165
        $this->fireEvent('enabling');
166
167
        $this->getModel()->where(['name' => $this->getName()])->update(['is_active' => 1]);
168
        $this->flushCache();
169
170
        $this->fireEvent('enabled');
171
    }
172
173
    /**
174
     * Delete the current module.
175
     *
176
     * @return bool
177
     * @throws Exception
178
     */
179
    public function delete(): bool
180
    {
181
        $module = $this->getModel()->where(['name' => $this->getName()])->first();
182
        if ($module) {
183
            $module->delete();
184
        }
185
        $this->flushCache();
186
187
        return (new Filesystem())->deleteDirectory($this->getPath());
188
    }
189
190
    /**
191
     * @return mixed|null
192
     */
193
    public function getVersion()
194
    {
195
        return $this->get('version', '1.0.0');
196
    }
197
}
198