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