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

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