Completed
Pull Request — develop (#76)
by
unknown
12:11
created

EloquentBaseRepository   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 5
Bugs 0 Features 3
Metric Value
wmc 21
c 5
b 0
f 3
lcom 1
cbo 1
dl 0
loc 169
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A find() 0 8 2
A all() 0 8 2
A create() 0 4 1
A update() 0 6 1
A destroy() 0 4 1
A allTranslatedIn() 0 6 1
A findBySlug() 0 10 2
A findByAttributes() 0 14 3
A getByAttributes() 0 18 4
A findByMany() 0 10 2
A clearCache() 0 4 1
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->model->query();
119
120
        if (method_exists($this->model, 'translations')) {
121
            $query = $query->with('translations');
122
        }
123
124
        foreach ($attributes as $field => $value) {
125
            $query = $query->where($field, $value);
126
        }
127
128
        return $query->first();
129
    }
130
    
131
    /**
132
     * Get resources by an array of attributes
133
     * @param  array  $attributes
134
     * @return objects
135
     */
136
    public function getByAttributes(array $attributes, $orderBy='', $sort='asc')
137
    {
138
        $query = $this->model->query();
139
140
        if (method_exists($this->model, 'translations')) {
141
            $query = $query->with('translations');
142
        }
143
144
        foreach ($attributes as $field => $value) {
145
            $query = $query->where($field, $value);
146
        }
147
148
        if (!empty($orderBy)) {
149
            $query->orderBy($orderBy, $sort);
150
        }
151
152
        return $query->get();
153
    }
154
155
    /**
156
     * Return a collection of elements who's ids match
157
     * @param array $ids
158
     * @return mixed
159
     */
160
    public function findByMany(array $ids)
161
    {
162
        $query = $this->model->query();
163
164
        if (method_exists($this->model, 'translations')) {
165
            $query = $query->with('translations');
166
        }
167
168
        return $query->whereIn("id", $ids)->get();
169
    }
170
171
    /**
172
     * Clear the cache for this Repositories' Entity
173
     * @return bool
174
     */
175
    public function clearCache()
176
    {
177
        return true;
178
    }
179
}
180