Module::getPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Rawilk\LaravelModules;
4
5
use Illuminate\Contracts\Container\Container;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Str;
8
use Illuminate\Support\Traits\Macroable;
9
use Rawilk\LaravelModules\Contracts\Activator;
10
11
abstract class Module
12
{
13
    use Macroable;
14
15
    /** @var \Rawilk\LaravelModules\Contracts\Activator */
16
    private $activator;
17
18
    /** @var \Illuminate\Contracts\Foundation\Application */
19
    protected $app;
20
21
    /** @var \Illuminate\Cache\CacheManager */
22
    private $cache;
23
24
    /** @var \Illuminate\Filesystem\Filesystem */
25
    private $files;
26
27
    /** @var array */
28
    protected $moduleJson = [];
29
30
    /** @var string */
31
    protected $name;
32
33
    /** @var string */
34
    protected $path;
35
36
    /** @var \Illuminate\Translation\Translator */
37
    private $translator;
38
39
    /**
40
     * @param \Illuminate\Contracts\Container\Container $app
41
     * @param string $name
42
     * @param string|null $path
43
     */
44
    public function __construct(Container $app, string $name, ?string $path)
45
    {
46
        $this->name = $name;
47
        $this->path = $path;
48
        $this->cache = $app['cache'];
49
        $this->files = $app['files'];
50
        $this->translator = $app['translator'];
51
        $this->activator = $app[Activator::class];
52
        $this->app = $app;
0 ignored issues
show
Documentation Bug introduced by
$app is of type Illuminate\Contracts\Container\Container, but the property $app was declared to be of type Illuminate\Contracts\Foundation\Application. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
53
    }
54
55
    /**
56
     * Get the path to the cached *_module.php file.
57
     *
58
     * @return string
59
     */
60
    abstract public function getCachedServicesPath(): string;
61
62
    abstract public function registerAliases(): void;
63
64
    abstract public function registerProviders(): void;
65
66
    public function boot(): void
67
    {
68
        if (config('modules.register.translations', true)) {
69
            $this->registerTranslation();
70
        }
71
72
        if ($this->isLoadFilesOnBoot()) {
73
            $this->registerFiles();
74
        }
75
76
        $this->fireEvent('boot');
77
    }
78
79
    public function delete(): bool
80
    {
81
        $this->activator->delete($this);
82
83
        return $this->json()->getFilesystem()->deleteDirectory($this->getPath());
84
    }
85
86
    public function disable(): void
87
    {
88
        $this->fireEvent('disabling');
89
90
        $this->activator->disable($this);
91
        $this->flushCache();
92
93
        $this->fireEvent('disabled');
94
    }
95
96
    public function enable(): void
97
    {
98
        $this->fireEvent('enabling');
99
100
        $this->activator->enable($this);
101
        $this->flushCache();
102
103
        $this->fireEvent('enabled');
104
    }
105
106
    public function get(string $key, $default = null)
107
    {
108
        return $this->json()->get($key, $default);
109
    }
110
111
    public function getAlias(): string
112
    {
113
        return $this->get('alias');
114
    }
115
116
    public function getAssets(): array
117
    {
118
        try {
119
            return $this->json('assets.json')->toArray();
0 ignored issues
show
Bug introduced by
The method toArray() does not exist on Rawilk\LaravelModules\Json. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
            return $this->json('assets.json')->/** @scrutinizer ignore-call */ toArray();
Loading history...
120
        } catch (\Exception $e) {
121
            return [];
122
        }
123
    }
124
125
    public function getAssetAttr(string $key, $default = null)
126
    {
127
        try {
128
            return $this->json('assets.json')->get($key, $default);
129
        } catch (\Exception $e) {
130
            return null;
131
        }
132
    }
133
134
    public function getComposerAttr(string $key, $default = null)
135
    {
136
        return $this->json('composer.json')->get($key, $default);
137
    }
138
139
    public function getDescription(): string
140
    {
141
        return $this->get('description');
142
    }
143
144
    public function getExtraPath(string $path): string
145
    {
146
        return $this->getPath() . '/' . $path;
147
    }
148
149
    public function getName(): string
150
    {
151
        return $this->name;
152
    }
153
154
    public function getLowerName(): string
155
    {
156
        return strtolower($this->name);
157
    }
158
159
    public function getPriority(): string
160
    {
161
        return $this->get('priority');
162
    }
163
164
    public function getRequires(): array
165
    {
166
        return $this->get('requires');
167
    }
168
169
    public function getPath(): string
170
    {
171
        return $this->path;
172
    }
173
174
    public function getSnakeName(): string
175
    {
176
        return Str::snake($this->name);
177
    }
178
179
    public function getStudlyName(): string
180
    {
181
        return Str::studly($this->name);
182
    }
183
184
    public function isDisabled(): bool
185
    {
186
        return $this->activator->hasStatus($this, false);
187
    }
188
189
    public function isEnabled(): bool
190
    {
191
        return $this->activator->hasStatus($this, true);
192
    }
193
194
    public function isStatus(bool $status): bool
195
    {
196
        return $this->activator->hasStatus($this, $status);
197
    }
198
199
    public function json(?string $file = null): Json
200
    {
201
        if ($file === null) {
202
            $file = 'module.json';
203
        }
204
205
        return Arr::get($this->moduleJson, $file, function () use ($file) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return Illuminate\Suppor...ion(...) { /* ... */ }) could return the type callable which is incompatible with the type-hinted return Rawilk\LaravelModules\Json. Consider adding an additional type-check to rule them out.
Loading history...
206
            return $this->moduleJson[$file] = new Json($this->getPath() . '/' . $file, $this->files);
207
        });
208
    }
209
210
    public function register(): void
211
    {
212
        $this->registerAliases();
213
214
        $this->registerProviders();
215
216
        if (! $this->isLoadFilesOnBoot()) {
217
            $this->registerFiles();
218
        }
219
220
        $this->fireEvent('register');
221
    }
222
223
    public function setActive(bool $active): bool
224
    {
225
        return $this->activator->setActive($this, $active);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->activator->setActive($this, $active) targeting Rawilk\LaravelModules\Co...\Activator::setActive() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug Best Practice introduced by
The expression return $this->activator-...tActive($this, $active) returns the type void which is incompatible with the type-hinted return boolean.
Loading history...
226
    }
227
228
    public function setPath(string $path): self
229
    {
230
        $this->path = $path;
231
232
        return $this;
233
    }
234
235
    protected function fireEvent(string $event): void
236
    {
237
        $this->app['events']->dispatch(sprintf('modules.%s.' . $event, $this->getLowerName()), [$this]);
238
    }
239
240
    protected function isLoadFilesOnBoot(): bool
241
    {
242
        return config('modules.register.files', 'register') === 'boot';
243
    }
244
245
    protected function registerFiles(): void
246
    {
247
        foreach ($this->get('files', []) as $file) {
248
            include $this->path . '/' . $file;
249
        }
250
    }
251
252
    protected function registerTranslation(): void
253
    {
254
        $lowerName = $this->getLowerName();
255
256
        $langPath = $this->getPath() . '/resources/lang';
257
258
        if (is_dir($langPath)) {
259
            $this->loadTranslationsFrom($langPath, $lowerName);
260
        }
261
    }
262
263
    private function flushCache(): void
264
    {
265
        if (config('modules.cache.enabled')) {
266
            $this->cache->store()->flush();
267
        }
268
    }
269
270
    private function loadTranslationsFrom(string $path, string $namespace): void
271
    {
272
        $this->translator->addNamespace($namespace, $path);
273
    }
274
275
    public function __toString()
276
    {
277
        return $this->getStudlyName();
278
    }
279
}
280