Completed
Pull Request — master (#1163)
by
unknown
03:47 queued 10s
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\Filesystem\Filesystem;
7
use Nwidart\Modules\Entities\ModuleEntity;
8
use Nwidart\Modules\Json;
9
use Nwidart\Modules\Process\Updater;
10
11
class DatabaseModule extends Module
12
{
13
    /**
14
     * @var array<string, mixed>
15
     */
16
    public $attributes;
17
18
    /**
19
     * @return ModuleEntity
20
     */
21
    public function getModel()
22
    {
23
        return new ModuleEntity();
24
    }
25
26
    /**
27
     * Get attributes.
28
     *
29
     * @return array
30
     */
31
    public function getAttributes()
32
    {
33
        return $this->attributes;
34
    }
35
36
    /**
37
     * Set attributes.
38
     *
39
     * @param array $attributes
40
     */
41
    public function setAttributes($attributes)
42
    {
43
        $this->attributes = $attributes;
44
    }
45
46
    /**
47
     * Get a specific data from json file by given the key.
48
     *
49
     * @param string $key
50
     * @param null   $default
51
     *
52
     * @return mixed
53
     */
54
    public function get(string $key, $default = null)
55
    {
56
        return isset($this->attributes[$key]) ? $this->attributes[$key] : $default;
57
    }
58
59
    /**
60
     * Determine whether the given status same with the current module status.
61
     *
62
     * @param bool $status
63
     *
64
     * @return bool
65
     */
66
    public function isStatus(bool $status): bool
67
    {
68
        return $this->isEnabled();
69
    }
70
71
    /**
72
     * Determine whether the current module activated.
73
     *
74
     * @return bool
75
     */
76
    public function isEnabled(): bool
77
    {
78
        return $this->attributes['is_active'];
79
    }
80
81
    /**
82
     *  Determine whether the current module not disabled.
83
     *
84
     * @return bool
85
     */
86
    public function isDisabled(): bool
87
    {
88
        return !$this->isEnabled();
89
    }
90
91
    /**
92
     * Set active state for current module.
93
     *
94
     * @param bool $active
95
     *
96
     * @return void
97
     */
98
    public function setActive(bool $active): void
99
    {
100
        $this->getModel()->where(['name' => $this->getName()])->update(['is_active' => $active]);
101
        $this->flushCache();
102
    }
103
104
    /**
105
     * Disable the current module.
106
     */
107
    public function disable(): void
108
    {
109
        $this->fireEvent('disabling');
110
111
        $this->setActive(false);
112
113
        $this->fireEvent('disabled');
114
    }
115
116
    /**
117
     * Enable the current module.
118
     */
119
    public function enable(): void
120
    {
121
        $this->fireEvent('enabling');
122
123
        $this->setActive(true);
124
125
        $this->fireEvent('enabled');
126
    }
127
128
    /**
129
     * Delete the current module.
130
     *
131
     * @return bool
132
     * @throws Exception
133
     */
134
    public function delete(): bool
135
    {
136
        $module = $this->getModel()->where(['name' => $this->getName()])->first();
137
        if ($module) {
138
            $module->delete();
139
        }
140
        $this->flushCache();
141
142
        return (new Filesystem())->deleteDirectory($this->getPath());
143
    }
144
145
    /**
146
     * Get version.
147
     *
148
     * @return mixed|null
149
     */
150
    public function getVersion()
151
    {
152
        return $this->get('version', '1.0.0');
153
    }
154
155
    public function update(Updater $updater)
156
    {
157
158
        if (config('modules.database_management.update_file_to_database_when_updating')) {
159
            $json = Json::make($this->getPath() . '/' . 'module.json');
160
            $data = $json->getAttributes();
161
162
            if (!isset($data['version'])) {
163
                $data['version'] = '1.0.0';
164
            }
165
166
            // Check version, if version is higher then update module.json into database.
167
            if (version_compare($this->getVersion(), $data['version'], '<=')) {
168
                $data = $updater->getModule()->validateAttributes($data);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Nwidart\Modules\Contracts\RepositoryInterface as the method validateAttributes() does only exist in the following implementations of said interface: Nwidart\Modules\Laravel\LaravelDatabaseRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
169
                $this->getModel()->where(['name' => $data['name']])->update($data);
170
            }
171
        }
172
173
        $response = with($updater)->update($this->getName());
174
        $this->flushCache();
175
176
        return $response;
177
    }
178
}
179