Completed
Pull Request — master (#1151)
by
unknown
01:57
created

LaravelEloquentRepository::assetPath()   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 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules\Laravel;
4
5
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
6
use Nwidart\Modules\Collection;
7
use Illuminate\Container\Container;
8
use Nwidart\Modules\Contracts\RepositoryInterface;
9
use Nwidart\Modules\Entities\ModuleEntity;
10
use Nwidart\Modules\Exceptions\ModuleNotFoundException;
11
use Nwidart\Modules\Laravel\Module;
12
13
class LaravelEloquentRepository implements RepositoryInterface
14
{
15
    /**
16
     * @var ModuleEntity
17
     */
18
    private $moduleEntity;
19
    /**
20
     * @var Container
21
     */
22
    private $app;
23
24
    public function __construct(Container $app, ModuleEntity $moduleEntity)
25
    {
26
        $this->app = $app;
27
        $this->moduleEntity = $moduleEntity;
28
    }
29
30
    /**
31
     * Get all modules.
32
     * @return EloquentCollection
33
     */
34
    public function all(): array
35
    {
36
        return $this->convertToCollection($this->moduleEntity->get())->toArray();
37
    }
38
39
    /**
40
     * Get cached modules.
41
     */
42
    public function getCached(): array
43
    {
44
        return $this->app['cache']->remember($this->config('cache.key'), $this->config('cache.lifetime'), function () {
45
            return $this->toCollection()->toArray();
46
        });
47
    }
48
49
    /**
50
     * Scan & get all available modules.
51
     */
52
    public function scan(): array
53
    {
54
        return $this->toCollection()->toArray();
55
    }
56
57
    /**
58
     * Get modules as modules collection instance.
59
     */
60
    public function toCollection(): Collection
61
    {
62
        return $this->convertToCollection($this->moduleEntity->get());
63
    }
64
65
    protected function createModule(...$args)
66
    {
67
        return new Module(...$args);
0 ignored issues
show
Bug introduced by
The call to Module::__construct() misses some required arguments starting with $name.
Loading history...
Documentation introduced by
$args is of type array<integer,?>, but the function expects a object<Illuminate\Container\Container>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
68
    }
69
70
    /**
71
     * Get scanned paths.
72
     * @return array
73
     */
74
    public function getScanPaths(): array
75
    {
76
        return [];
77
    }
78
79
    /**
80
     * Get list of enabled modules.
81
     * @return mixed
82
     */
83
    public function allEnabled(): array
84
    {
85
        $results = $this->moduleEntity->newQuery()->where('is_active', 1)->get();
86
87
        return $this->convertToCollection($results)->toArray();
0 ignored issues
show
Bug introduced by
It seems like $results defined by $this->moduleEntity->new...('is_active', 1)->get() on line 85 can also be of type array<integer,object<Ill...base\Eloquent\Builder>>; however, Nwidart\Modules\Laravel\...::convertToCollection() does only seem to accept object<Illuminate\Database\Eloquent\Collection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
88
    }
89
90
    /**
91
     * Get list of disabled modules.
92
     * @return mixed
93
     */
94
    public function allDisabled()
95
    {
96
        $results = $this->moduleEntity->newQuery()->where('is_active', 0)->get();
97
98
        return $this->convertToCollection($results)->toArray();
0 ignored issues
show
Bug introduced by
It seems like $results defined by $this->moduleEntity->new...('is_active', 0)->get() on line 96 can also be of type array<integer,object<Ill...base\Eloquent\Builder>>; however, Nwidart\Modules\Laravel\...::convertToCollection() does only seem to accept object<Illuminate\Database\Eloquent\Collection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
99
    }
100
101
    /**
102
     * Get count from all modules.
103
     * @return int
104
     */
105
    public function count(): int
106
    {
107
        return $this->moduleEntity->count();
108
    }
109
110
    /**
111
     * Get all ordered modules.
112
     */
113
    public function getOrdered(string $direction = 'asc'): array
114
    {
115
        $results = $this->moduleEntity
0 ignored issues
show
Bug introduced by
The method orderBy() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean enforceOrderBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
116
            ->newQuery()
117
            ->where('is_active', 1)
118
            ->orderBy('order', $direction)
119
            ->get();
120
121
        return $this->convertToCollection($results)->toArray();
122
    }
123
124
    /**
125
     * Get modules by the given status.
126
     * @param int $status
127
     * @return array
128
     */
129
    public function getByStatus($status): array
130
    {
131
        $results = $this->moduleEntity
132
            ->newQuery()
133
            ->where('is_active', $status)
134
            ->get();
135
        return $this->convertToCollection($results)->toArray();
0 ignored issues
show
Bug introduced by
It seems like $results defined by $this->moduleEntity->new...ctive', $status)->get() on line 131 can also be of type array<integer,object<Ill...base\Eloquent\Builder>>; however, Nwidart\Modules\Laravel\...::convertToCollection() does only seem to accept object<Illuminate\Database\Eloquent\Collection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
136
    }
137
138
    /**
139
     * Find a specific module.
140
     * @param $name
141
     * @return \Nwidart\Modules\Contracts\ModuleInterface
142
     */
143
    public function find($name): ?\Nwidart\Modules\Contracts\ModuleInterface
144
    {
145
        $module = $this->moduleEntity
146
            ->newQuery()
147
            ->where('name', $name)
148
            ->first();
149
150
        if ($module === null) {
151
            return null;
152
        }
153
154
        return $this->createModule($this->app, $module->name, $module->path);
155
    }
156
157
    /**
158
     * Find a specific module. If there return that, otherwise throw exception.
159
     * @param $name
160
     * @return \Nwidart\Modules\Contracts\ModuleInterface
161
     * @throws ModuleNotFoundException
162
     */
163
    public function findOrFail($name): \Nwidart\Modules\Contracts\ModuleInterface
164
    {
165
        $module = $this->find($name);
166
167
        if ($module === null) {
168
            throw new ModuleNotFoundException();
169
        }
170
171
        return $module;
172
    }
173
174
    public function getModulePath($moduleName)
175
    {
176
        $module = $this->findOrFail($moduleName);
177
178
        return $module->getPath();
179
    }
180
181
    /**
182
     * @return \Illuminate\Filesystem\Filesystem
183
     */
184
    public function getFiles()
185
    {
186
        return $this->app['files'];
187
    }
188
189
    public function config($key, $default = null)
190
    {
191
        return $this->app['config']->get('modules.' . $key, $default);
192
    }
193
194
    public function exists(string $name): bool
195
    {
196
        return (bool) $this->moduleEntity
197
            ->newQuery()
198
            ->where('name', $name)
199
            ->count();
200
    }
201
202
    /**
203
     * Delete a specific module.
204
     * @param string $name
205
     * @return bool
206
     * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException
207
     */
208
    public function delete($name): bool
209
    {
210
        return $this->findOrFail($name)->delete();
211
    }
212
213
    private function convertToCollection(EloquentCollection $eloquentCollection): Collection
214
    {
215
        $collection = new Collection();
216
        $eloquentCollection->map(function ($module) use ($collection) {
217
            $collection->push($this->createModule($this->app, $module->name, $module->path, $module->toArray()));
218
        });
219
        return $collection;
220
    }
221
222
    public function findRequirements($name): array
223
    {
224
        // TODO: Implement findRequirements() method.
225
    }
226
227
    public function getPath(): string
228
    {
229
        // TODO: Implement getPath() method.
230
    }
231
232
    public function findByAlias(string $alias)
233
    {
234
        // TODO: Implement findByAlias() method.
235
    }
236
237
    public function boot(): void
238
    {
239
        // TODO: Implement boot() method.
240
    }
241
242
    public function register(): void
243
    {
244
        // TODO: Implement register() method.
245
    }
246
247
    public function assetPath(string $module): string
248
    {
249
        // TODO: Implement assetPath() method.
250
    }
251
252
    public function isEnabled(string $name): bool
253
    {
254
        // TODO: Implement isEnabled() method.
255
    }
256
257
    public function isDisabled(string $name): bool
258
    {
259
        // TODO: Implement isDisabled() method.
260
    }
261
}
262