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

DatabaseModule::enable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules\Laravel;
4
5
use Illuminate\Container\Container;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Filesystem\Filesystem;
8
use Nwidart\Modules\Entities\ModuleEntity;
9
10
class DatabaseModule extends Module
11
{
12
    public $attributes;
13
14
    public function __construct(Container $app, string $name, $path)
15
    {
16
        parent::__construct($app, $name, $path);
17
        $this->loadAttributes();
18
    }
19
20
    /**
21
     * @return ModuleEntity|Builder
22
     */
23
    public function getModel()
24
    {
25
        return new ModuleEntity();
26
    }
27
28
    private function loadAttributes()
29
    {
30
        if (!$this->attributes) {
31
32
            // Try to get from cache first.
33
            $attributes = [];
34
            if (config('modules.cache.enabled')) {
35
                if ($modules = cache()->get(config('modules.cache.key'))) {
36
                    foreach ($modules as $module) {
37
                        if ($this->getName() == $module['name']) {
38
                            $attributes = $module;
39
                        }
40
                    }
41
                }
42
            }
43
44
            // Find from database. Throw error if still not found.
45
            if (!isset($attributes['is_active'])) {
46
                $attributes = $this->getModel()->where('name', $this->getName())->firstOrFail()->toArray();
0 ignored issues
show
Bug introduced by
The method where does only exist in Illuminate\Database\Eloquent\Builder, but not in Nwidart\Modules\Entities\ModuleEntity.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
47
            }
48
49
            $this->attributes = $attributes;
50
        }
51
52
        return $this->attributes;
53
    }
54
55
    public function getAttributes()
56
    {
57
        return $this->attributes;
58
    }
59
60
    public function setAttributes($attributes)
61
    {
62
        $this->attributes = $attributes;
63
    }
64
65
    /**
66
     * Get a specific data from json file by given the key.
67
     *
68
     * @param string $key
69
     * @param null   $default
70
     *
71
     * @return mixed
72
     */
73
    public function get(string $key, $default = null)
74
    {
75
        return isset($this->attributes[$key]) ? $this->attributes[$key] : $default;
76
    }
77
78
    /**
79
     * Determine whether the given status same with the current module status.
80
     *
81
     * @param bool $status
82
     *
83
     * @return bool
84
     */
85
    public function isStatus(bool $status): bool
86
    {
87
        return $this->isEnabled();
88
    }
89
90
    /**
91
     * Determine whether the current module activated.
92
     *
93
     * @return bool
94
     */
95
    public function isEnabled(): bool
96
    {
97
        return $this->attributes['is_active'];
98
    }
99
100
    /**
101
     *  Determine whether the current module not disabled.
102
     *
103
     * @return bool
104
     */
105
    public function isDisabled(): bool
106
    {
107
        return !$this->isEnabled();
108
    }
109
110
    /**
111
     * Set active state for current module.
112
     *
113
     * @param bool $active
114
     *
115
     * @return void
116
     */
117
    public function setActive(bool $active): void
118
    {
119
        $this->getModel()->where(['name' => $this->getName()])->update(['is_active' => $active]);
0 ignored issues
show
Bug introduced by
The method where does only exist in Illuminate\Database\Eloquent\Builder, but not in Nwidart\Modules\Entities\ModuleEntity.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
120
        $this->flushCache();
121
    }
122
123
    /**
124
     * Disable the current module.
125
     */
126 View Code Duplication
    public function disable(): void
127
    {
128
        $this->fireEvent('disabling');
129
130
        $this->getModel()->where(['name' => $this->getName()])->update(['is_active' => 0]);
0 ignored issues
show
Bug introduced by
The method where does only exist in Illuminate\Database\Eloquent\Builder, but not in Nwidart\Modules\Entities\ModuleEntity.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
131
        $this->flushCache();
132
133
        $this->fireEvent('disabled');
134
    }
135
136
    /**
137
     * Enable the current module.
138
     */
139 View Code Duplication
    public function enable(): void
140
    {
141
        $this->fireEvent('enabling');
142
143
        $this->getModel()->where(['name' => $this->getName()])->update(['is_active' => 1]);
0 ignored issues
show
Bug introduced by
The method where does only exist in Illuminate\Database\Eloquent\Builder, but not in Nwidart\Modules\Entities\ModuleEntity.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
144
        $this->flushCache();
145
146
        $this->fireEvent('enabled');
147
    }
148
149
    /**
150
     * Delete the current module.
151
     *
152
     * @return bool
153
     */
154
    public function delete(): bool
155
    {
156
        $module = $this->getModel()->where(['name' => $this->getName()])->first();
0 ignored issues
show
Bug introduced by
The method where does only exist in Illuminate\Database\Eloquent\Builder, but not in Nwidart\Modules\Entities\ModuleEntity.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
157
        if ($module) {
158
            $module->delete();
159
        }
160
        $this->flushCache();
161
162
        return (new Filesystem())->deleteDirectory($this->getPath());
163
    }
164
}
165