Completed
Pull Request — master (#1151)
by
unknown
07:51
created

LaravelEloquentRepository::getCached()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
cc 1
nc 1
nop 0
rs 10
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
12
class LaravelEloquentRepository implements RepositoryInterface
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: assetPath, boot, delete, findByAlias, findRequirements, getPath, isDisabled, isEnabled, register
Loading history...
13
{
14
    /**
15
     * @var ModuleEntity
16
     */
17
    private $moduleEntity;
18
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 \Illuminate\Database\Eloquent\Collection
33
     */
34
    public function all(): array
35
    {
36
        return $this->moduleEntity->get();
37
    }
38
39
    /**
40
     * Get cached modules.
41
     * @return array
42
     */
43
    public function getCached(): array
44
    {
45
        return $this->app['cache']->remember($this->config('cache.key'), $this->config('cache.lifetime'), function () {
46
            return $this->toCollection()->toArray();
47
        });
48
    }
49
50
    /**
51
     * Scan & get all available modules.
52
     * @return array
53
     */
54
    public function scan(): array
55
    {
56
        return $this->toCollection()->toArray();
57
    }
58
59
    /**
60
     * Get modules as modules collection instance.
61
     * @return EloquentCollection
62
     */
63
    public function toCollection(): Collection
64
    {
65
        return $this->convertToCollection($this->all());
0 ignored issues
show
Documentation introduced by
$this->all() is of type array, but the function expects a object<Illuminate\Database\Eloquent\Collection>.

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...
66
    }
67
68
    protected function createModule(...$args)
69
    {
70
        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...
71
    }
72
73
    /**
74
     * Get scanned paths.
75
     * @return array
76
     */
77
    public function getScanPaths(): array
78
    {
79
        return [];
80
    }
81
82
    /**
83
     * Get list of enabled modules.
84
     * @return array
85
     */
86
    public function allEnabled(): array
87
    {
88
        return $this->convertToCollection($results)->toArray();
0 ignored issues
show
Bug introduced by
The variable $results does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
89
    }
90
91
    /**
92
     * Get list of disabled modules.
93
     * @return mixed
94
     */
95
    public function allDisabled()
96
    {
97
        $results = $this->moduleEntity->newQuery()->where('is_active', 0)->get();
98
99
        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 97 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...
100
    }
101
102
    /**
103
     * Get count from all modules.
104
     * @return int
105
     */
106
    public function count(): int
107
    {
108
        return $this->moduleEntity->count();
109
    }
110
111
    /**
112
     * Get all ordered modules.
113
     * @param string $direction
114
     * @return mixed
115
     */
116
    public function getOrdered(string $direction = 'asc'): array
117
    {
118
        $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...
119
            ->newQuery()
120
            ->where('is_active', 1)
121
            ->orderBy('order', $direction)
122
            ->get();
123
124
        return $this->convertToCollection($results)->toArray();
125
    }
126
127
    /**
128
     * Get modules by the given status.
129
     * @param int $status
130
     * @return array
131
     */
132
    public function getByStatus($status): array
133
    {
134
        $results = $this->moduleEntity
135
            ->newQuery()
136
            ->where('is_active', $status)
137
            ->get();
138
        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 134 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...
139
    }
140
141
    /**
142
     * Find a specific module.
143
     * @param $name
144
     * @return \Nwidart\Modules\Module
145
     */
146
    public function find($name): ?\Nwidart\Modules\Module
147
    {
148
        $module = $this->moduleEntity
149
            ->newQuery()
150
            ->where('name', $name)
151
            ->first();
152
153
        if ($module === null) {
154
            return null;
155
        }
156
157
        return $this->createModule($this->app, $module->name, $module->path);
158
    }
159
160
    /**
161
     * Find a specific module. If there return that, otherwise throw exception.
162
     * @param $name
163
     * @return \Nwidart\Modules\Module
164
     * @throws ModuleNotFoundException
165
     */
166
    public function findOrFail($name): \Nwidart\Modules\Module
167
    {
168
        $module = $this->find($name);
169
170
        if ($module === null) {
171
            throw new ModuleNotFoundException();
172
        }
173
174
        return $module;
175
    }
176
177
    public function getModulePath($moduleName)
178
    {
179
        $module = $this->findOrFail($moduleName);
180
181
        return $module->getPath();
182
    }
183
184
    /**
185
     * @return \Illuminate\Filesystem\Filesystem
186
     */
187
    public function getFiles()
188
    {
189
        return $this->app['files'];
190
    }
191
192
    public function config($key, $default = null)
193
    {
194
        return $this->app['config']->get('modules.' . $key, $default);
195
    }
196
197
    private function convertToCollection(EloquentCollection $eloquentCollection): Collection
198
    {
199
        $collection = new Collection();
200
        $eloquentCollection->map(function ($module) use ($collection) {
201
            $collection->push($this->createModule($this->app, $module->name, $module->path));
202
        });
203
        return $collection;
204
    }
205
}
206