Completed
Pull Request — 2.0 (#97)
by Micheal
03:01
created

EloquentBaseRepository::paginate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 8
loc 8
cc 2
eloc 4
nc 2
nop 1
rs 9.4285
1
<?php
2
3
namespace Modules\Core\Repositories\Eloquent;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Modules\Core\Repositories\BaseRepository;
7
8
/**
9
 * Class EloquentCoreRepository
10
 *
11
 * @package Modules\Core\Repositories\Eloquent
12
 */
13
abstract class EloquentBaseRepository implements BaseRepository
14
{
15
    /**
16
     * @var \Illuminate\Database\Eloquent\Model An instance of the Eloquent Model
17
     */
18
    protected $model;
19
20
    /**
21
     * @param Model $model
22
     */
23
    public function __construct($model)
24
    {
25
        $this->model = $model;
0 ignored issues
show
Documentation Bug introduced by
It seems like $model of type object<Modules\Core\Repositories\Eloquent\Model> is incompatible with the declared type object<Illuminate\Database\Eloquent\Model> of property $model.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
26
    }
27
28
    /**
29
     * @param  int    $id
30
     * @return object
31
     */
32
    public function find($id)
33
    {
34
        if (method_exists($this->model, 'translations')) {
35
            return $this->model->with('translations')->find($id);
0 ignored issues
show
Bug introduced by
The method find does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

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...
36
        }
37
38
        return $this->model->find($id);
39
    }
40
41
    /**
42
     * @return \Illuminate\Database\Eloquent\Collection
43
     */
44 View Code Duplication
    public function all()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        if (method_exists($this->model, 'translations')) {
47
            return $this->model->with('translations')->orderBy('created_at', 'DESC')->get();
48
        }
49
50
        return $this->model->orderBy('created_at', 'DESC')->get();
51
    }
52
53
    /**
54
     * @return \Illuminate\Pagination\LengthAwarePaginator
55
     */
56 View Code Duplication
    public function paginate($perPage = 15)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58
        if (method_exists($this->model, 'translations')) {
59
            return $this->model->with('translations')->orderBy('created_at', 'DESC')->paginate($perPage);
60
        }
61
62
        return $this->model->orderBy('created_at', 'DESC')->paginate($perPage);
63
    }
64
65
    /**
66
     * @param  mixed  $data
67
     * @return object
68
     */
69
    public function create($data)
70
    {
71
        return $this->model->create($data);
72
    }
73
74
    /**
75
     * @param $model
76
     * @param  array  $data
77
     * @return object
78
     */
79
    public function update($model, $data)
80
    {
81
        $model->update($data);
82
83
        return $model;
84
    }
85
86
    /**
87
     * @param  Model $model
88
     * @return bool
89
     */
90
    public function destroy($model)
91
    {
92
        return $model->delete();
93
    }
94
95
    /**
96
     * Return all resources in the given language
97
     *
98
     * @param  string                                   $lang
99
     * @return \Illuminate\Database\Eloquent\Collection
100
     */
101
    public function allTranslatedIn($lang)
102
    {
103
        return $this->model->whereHas('translations', function (Builder $q) use ($lang) {
104
            $q->where('locale', "$lang");
105
        })->with('translations')->orderBy('created_at', 'DESC')->get();
106
    }
107
108
    /**
109
     * Find a resource by the given slug
110
     *
111
     * @param  string $slug
112
     * @return object
113
     */
114
    public function findBySlug($slug)
115
    {
116
        if (method_exists($this->model, 'translations')) {
117
            return $this->model->whereHas('translations', function (Builder $q) use ($slug) {
118
                $q->where('slug', $slug);
119
            })->with('translations')->first();
120
        }
121
122
        return $this->model->where('slug', $slug)->first();
123
    }
124
125
    /**
126
     * Find a resource by an array of attributes
127
     * @param  array  $attributes
128
     * @return object
129
     */
130
    public function findByAttributes(array $attributes)
131
    {
132
        $query = $this->buildQueryByAttributes($attributes);
133
134
        return $query->first();
0 ignored issues
show
Bug Compatibility introduced by
The expression $query->first(); of type stdClass|array|null adds the type array to the return on line 134 which is incompatible with the return type declared by the interface Modules\Core\Repositorie...itory::findByAttributes of type object.
Loading history...
135
    }
136
137
    /**
138
     * Get resources by an array of attributes
139
     * @param array $attributes
140
     * @param null|string $orderBy
141
     * @param string $sortOrder
142
     * @return \Illuminate\Database\Eloquent\Collection
143
     */
144
    public function getByAttributes(array $attributes, $orderBy = null, $sortOrder = 'asc')
145
    {
146
        $query = $this->buildQueryByAttributes($attributes, $orderBy, $sortOrder);
147
148
        return $query->get();
149
    }
150
151
    /**
152
     * Build Query to catch resources by an array of attributes and params
153
     * @param array $attributes
154
     * @param null|string $orderBy
155
     * @param string $sortOrder
156
     * @return \Illuminate\Database\Query\Builder object
157
     */
158
    private function buildQueryByAttributes(array $attributes, $orderBy = null, $sortOrder = 'asc')
159
    {
160
        $query = $this->model->query();
161
162
        if (method_exists($this->model, 'translations')) {
163
            $query = $query->with('translations');
164
        }
165
166
        foreach ($attributes as $field => $value) {
167
            $query = $query->where($field, $value);
168
        }
169
170
        if (null !== $orderBy) {
171
            $query->orderBy($orderBy, $sortOrder);
172
        }
173
174
        return $query;
175
    }
176
177
    /**
178
     * Return a collection of elements who's ids match
179
     * @param array $ids
180
     * @return mixed
181
     */
182
    public function findByMany(array $ids)
183
    {
184
        $query = $this->model->query();
185
186
        if (method_exists($this->model, 'translations')) {
187
            $query = $query->with('translations');
188
        }
189
190
        return $query->whereIn("id", $ids)->get();
191
    }
192
193
    /**
194
     * Clear the cache for this Repositories' Entity
195
     * @return bool
196
     */
197
    public function clearCache()
198
    {
199
        return true;
200
    }
201
}
202