Completed
Push — develop ( b07017...36f41e )
by Nicolas
02:56
created

EloquentBaseRepository::getByAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 3
1
<?php namespace Modules\Core\Repositories\Eloquent;
2
3
use Illuminate\Database\Eloquent\Builder;
4
use Modules\Core\Repositories\BaseRepository;
5
6
/**
7
 * Class EloquentCoreRepository
8
 *
9
 * @package Modules\Core\Repositories\Eloquent
10
 */
11
abstract class EloquentBaseRepository implements BaseRepository
12
{
13
    /**
14
     * @var Model An instance of the Eloquent Model
15
     */
16
    protected $model;
17
18
    /**
19
     * @param Model $model
20
     */
21
    public function __construct($model)
22
    {
23
        $this->model = $model;
24
    }
25
26
    /**
27
     * @param  int    $id
28
     * @return object
29
     */
30
    public function find($id)
31
    {
32
        if (method_exists($this->model, 'translations')) {
33
            return $this->model->with('translations')->find($id);
34
        }
35
36
        return $this->model->find($id);
37
    }
38
39
    /**
40
     * @return \Illuminate\Database\Eloquent\Collection
41
     */
42
    public function all()
43
    {
44
        if (method_exists($this->model, 'translations')) {
45
            return $this->model->with('translations')->orderBy('created_at', 'DESC')->get();
46
        }
47
48
        return $this->model->orderBy('created_at', 'DESC')->get();
49
    }
50
51
    /**
52
     * @param  mixed  $data
53
     * @return object
54
     */
55
    public function create($data)
56
    {
57
        return $this->model->create($data);
58
    }
59
60
    /**
61
     * @param $model
62
     * @param  array  $data
63
     * @return object
64
     */
65
    public function update($model, $data)
66
    {
67
        $model->update($data);
68
69
        return $model;
70
    }
71
72
    /**
73
     * @param  Model $model
74
     * @return bool
75
     */
76
    public function destroy($model)
77
    {
78
        return $model->delete();
79
    }
80
81
    /**
82
     * Return all resources in the given language
83
     *
84
     * @param  string                                   $lang
85
     * @return \Illuminate\Database\Eloquent\Collection
86
     */
87
    public function allTranslatedIn($lang)
88
    {
89
        return $this->model->whereHas('translations', function (Builder $q) use ($lang) {
90
            $q->where('locale', "$lang");
91
        })->with('translations')->orderBy('created_at', 'DESC')->get();
92
    }
93
94
    /**
95
     * Find a resource by the given slug
96
     *
97
     * @param  string $slug
98
     * @return object
99
     */
100
    public function findBySlug($slug)
101
    {
102
        if (method_exists($this->model, 'translations')) {
103
            return $this->model->whereHas('translations', function (Builder $q) use ($slug) {
104
                $q->where('slug', $slug);
105
            })->with('translations')->first();
106
        }
107
108
        return $this->model->where('slug', $slug)->first();
109
    }
110
    
111
    /**
112
     * Find a resource by an array of attributes
113
     * @param  array  $attributes
114
     * @return object
115
     */
116
    public function findByAttributes(array $attributes)
117
    {
118
        $query = $this->catchByAttributes($attributes);
119
120
        return $query->first();
121
    }
122
123
    /**
124
     * Get resources by an array of attributes
125
     * @param  array  $attributes
126
     * @return objects
127
     */
128
    public function getByAttributes(array $attributes, $orderBy = '', $sort = 'asc')
129
    {
130
        $query = $this->catchByAttributes($attributes, $orderBy, $sort);
131
132
        return $query->get();
133
    }
134
135
    /**
136
     * Build Query to catch resources by an array of attributes and params
137
     * @param  array  $attributes
138
     * @return query object
139
     */
140
    private function catchByAttributes(array $attributes, $orderBy = '', $sort = 'asc')
141
    {
142
        $query = $this->model->query();
143
144
        if (method_exists($this->model, 'translations')) {
145
            $query = $query->with('translations');
146
        }
147
148
        foreach ($attributes as $field => $value) {
149
            $query = $query->where($field, $value);
150
        }
151
152
        if (! empty($orderBy)) {
153
            $query->orderBy($orderBy, $sort);
154
        }
155
156
        return $query;
157
    }
158
159
    /**
160
     * Return a collection of elements who's ids match
161
     * @param array $ids
162
     * @return mixed
163
     */
164
    public function findByMany(array $ids)
165
    {
166
        $query = $this->model->query();
167
168
        if (method_exists($this->model, 'translations')) {
169
            $query = $query->with('translations');
170
        }
171
172
        return $query->whereIn("id", $ids)->get();
173
    }
174
175
    /**
176
     * Clear the cache for this Repositories' Entity
177
     * @return bool
178
     */
179
    public function clearCache()
180
    {
181
        return true;
182
    }
183
}
184