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