Total Complexity | 58 |
Total Lines | 393 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like NewsRepository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use NewsRepository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class NewsRepository extends BaseRepository |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * Note: please keep logic in this repository. Put logic not in models, |
||
19 | * Information about models in Laravel: http://laravel.com/docs/5.1/eloquent |
||
20 | * @author Matthijs Neijenhuijs <[email protected]> |
||
21 | * @copyright DutchBridge - dont share/steel! |
||
22 | */ |
||
23 | |||
24 | protected $model; |
||
25 | |||
26 | public function __construct(News $model, NewsImage $modelImage, NewsGroup $modelGroup) |
||
27 | { |
||
28 | $this->model = $model; |
||
29 | $this->modelImage = $modelImage; |
||
|
|||
30 | $this->modelGroup = $modelGroup; |
||
31 | $this->storageImagePath = storage_path() .config('hideyo.storage_path'). "/news/"; |
||
32 | $this->publicImagePath = public_path() .config('hideyo.public_path'). "/news/"; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * The validation rules for the model. |
||
37 | * |
||
38 | * @param integer $id id attribute model |
||
39 | * @return array |
||
40 | */ |
||
41 | public function rules($newsId = false, $attributes = false) |
||
42 | { |
||
43 | if (isset($attributes['seo'])) { |
||
44 | $rules = array( |
||
45 | 'meta_title' => 'required|between:4,65|unique_with:'.$this->model->getTable().', shop_id' |
||
46 | ); |
||
47 | } else { |
||
48 | $rules = array( |
||
49 | 'title' => 'required|between:4,65|unique:'.$this->model->getTable().'' |
||
50 | ); |
||
51 | |||
52 | if ($newsId) { |
||
53 | $rules['title'] = 'required|between:4,65|unique:'.$this->model->getTable().',title,'.$newsId; |
||
54 | } |
||
55 | } |
||
56 | |||
57 | return $rules; |
||
58 | } |
||
59 | |||
60 | public function rulesGroup($newsGroupId = false, $attributes = false) |
||
61 | { |
||
62 | if (isset($attributes['seo'])) { |
||
63 | $rules = array( |
||
64 | 'meta_title' => 'required|between:4,65|unique_with:'.$this->modelGroup->getTable().', shop_id' |
||
65 | ); |
||
66 | } else { |
||
67 | $rules = array( |
||
68 | 'title' => 'required|between:4,65|unique:'.$this->modelGroup->getTable() |
||
69 | ); |
||
70 | |||
71 | if ($newsGroupId) { |
||
72 | $rules['title'] = 'required|between:4,65|unique:'.$this->modelGroup->getTable().',title,'.$newsGroupId; |
||
73 | } |
||
74 | } |
||
75 | |||
76 | return $rules; |
||
77 | } |
||
78 | |||
79 | public function create(array $attributes) |
||
80 | { |
||
81 | $attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
||
82 | $validator = Validator::make($attributes, $this->rules()); |
||
83 | |||
84 | if ($validator->fails()) { |
||
85 | return $validator; |
||
86 | } |
||
87 | |||
88 | $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
||
89 | $this->model->fill($attributes); |
||
90 | $this->model->save(); |
||
91 | return $this->model; |
||
92 | } |
||
93 | |||
94 | public function updateById(array $attributes, $id) |
||
95 | { |
||
96 | $attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
||
97 | $validator = Validator::make($attributes, $this->rules($id)); |
||
98 | if ($validator->fails()) { |
||
99 | return $validator; |
||
100 | } |
||
101 | |||
102 | $this->model = $this->find($id); |
||
103 | $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
||
104 | return $this->updateEntity($attributes); |
||
105 | } |
||
106 | |||
107 | public function createImage(array $attributes, $newsId) |
||
108 | { |
||
109 | $userId = auth('hideyobackend')->user()->id; |
||
110 | $shopId = auth('hideyobackend')->user()->selected_shop_id; |
||
111 | $shop = ShopService::find($shopId); |
||
112 | $attributes['modified_by_user_id'] = $userId; |
||
113 | $destinationPath = $this->storageImagePath.$newsId; |
||
114 | $attributes['user_id'] = $userId; |
||
115 | $attributes['news_id'] = $newsId; |
||
116 | $attributes['extension'] = $attributes['file']->getClientOriginalExtension(); |
||
117 | $attributes['size'] = $attributes['file']->getSize(); |
||
118 | |||
119 | $rules = array( |
||
120 | 'file'=>'required|image|max:1000', |
||
121 | 'rank' => 'required' |
||
122 | ); |
||
123 | |||
124 | $validator = Validator::make($attributes, $rules); |
||
125 | |||
126 | if ($validator->fails()) { |
||
127 | return $validator; |
||
128 | } |
||
129 | |||
130 | $filename = str_replace(" ", "_", strtolower($attributes['file']->getClientOriginalName())); |
||
131 | $uploadSuccess = $attributes['file']->move($destinationPath, $filename); |
||
132 | |||
133 | if ($uploadSuccess) { |
||
134 | $attributes['file'] = $filename; |
||
135 | $attributes['path'] = $uploadSuccess->getRealPath(); |
||
136 | |||
137 | $this->modelImage->fill($attributes); |
||
138 | $this->modelImage->save(); |
||
139 | |||
140 | if ($shop->thumbnail_square_sizes) { |
||
141 | $sizes = explode(',', $shop->thumbnail_square_sizes); |
||
142 | if ($sizes) { |
||
143 | foreach ($sizes as $valueImage) { |
||
144 | $image = Image::make($uploadSuccess->getRealPath()); |
||
145 | $explode = explode('x', $valueImage); |
||
146 | $image->resize($explode[0], $explode[1]); |
||
147 | $image->interlace(); |
||
148 | |||
149 | if (!File::exists($this->publicImagePath.$valueImage."/".$newsId."/")) { |
||
150 | File::makeDirectory($this->publicImagePath.$valueImage."/".$newsId."/", 0777, true); |
||
151 | } |
||
152 | $image->save($this->publicImagePath.$valueImage."/".$newsId."/".$filename); |
||
153 | } |
||
154 | } |
||
155 | } |
||
156 | |||
157 | return $this->modelImage; |
||
158 | } |
||
159 | |||
160 | } |
||
161 | |||
162 | |||
163 | public function createGroup(array $attributes) |
||
164 | { |
||
165 | $attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
||
166 | $validator = Validator::make($attributes, $this->rulesGroup()); |
||
167 | |||
168 | if ($validator->fails()) { |
||
169 | return $validator; |
||
170 | } |
||
171 | |||
172 | $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
||
173 | |||
174 | $this->modelGroup->fill($attributes); |
||
175 | $this->modelGroup->save(); |
||
176 | |||
177 | return $this->modelGroup; |
||
178 | } |
||
179 | |||
180 | public function refactorAllImagesByShopId($shopId) |
||
181 | { |
||
182 | $result = $this->modelImage->get(); |
||
183 | $shop = ShopService::find($shopId); |
||
184 | foreach ($result as $productImage) { |
||
185 | if ($shop->thumbnail_square_sizes) { |
||
186 | $sizes = explode(',', $shop->thumbnail_square_sizes); |
||
187 | if ($sizes) { |
||
188 | foreach ($sizes as $valueImage) { |
||
189 | if (!File::exists($this->publicImagePath.$valueImage."/".$productImage->news_id."/")) { |
||
190 | File::makeDirectory($this->publicImagePath.$valueImage."/".$productImage->news_id."/", 0777, true); |
||
191 | } |
||
192 | |||
193 | if (!File::exists($this->publicImagePath.$valueImage."/".$productImage->news_id."/".$productImage->file)) { |
||
194 | if (File::exists($this->storageImagePath.$productImage->news_id."/".$productImage->file)) { |
||
195 | $image = Image::make(storage_path() .config('hideyo.storage_path'). "//news/".$productImage->news_id."/".$productImage->file); |
||
196 | $explode = explode('x', $valueImage); |
||
197 | $image->fit($explode[0], $explode[1]); |
||
198 | |||
199 | $image->interlace(); |
||
200 | |||
201 | $image->save(public_path() .config('hideyo.storage_path'). "/news/".$valueImage."/".$productImage->news_id."/".$productImage->file); |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | } |
||
206 | } |
||
207 | } |
||
208 | } |
||
209 | |||
210 | public function updateImageById(array $attributes, $newsId, $newsImageId) |
||
211 | { |
||
212 | $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
||
213 | $this->modelImage = $this->findImage($newsImageId); |
||
214 | return $this->updateImageEntity($attributes); |
||
215 | } |
||
216 | |||
217 | public function updateImageEntity(array $attributes = array()) |
||
218 | { |
||
219 | if (count($attributes) > 0) { |
||
220 | $this->modelImage->fill($attributes); |
||
221 | $this->modelImage->save(); |
||
222 | } |
||
223 | |||
224 | return $this->modelImage; |
||
225 | } |
||
226 | |||
227 | public function updateGroupById(array $attributes, $newsGroupId) |
||
228 | { |
||
229 | $validator = Validator::make($attributes, $this->rulesGroup($newsGroupId, $attributes)); |
||
230 | |||
231 | if ($validator->fails()) { |
||
232 | return $validator; |
||
233 | } |
||
234 | |||
235 | $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
||
236 | $this->modelGroup = $this->findGroup($newsGroupId); |
||
237 | return $this->updateGroupEntity($attributes); |
||
238 | } |
||
239 | |||
240 | public function updateGroupEntity(array $attributes = array()) |
||
241 | { |
||
242 | if (count($attributes) > 0) { |
||
243 | $this->modelGroup->fill($attributes); |
||
244 | $this->modelGroup->save(); |
||
245 | } |
||
246 | |||
247 | return $this->modelGroup; |
||
248 | } |
||
249 | |||
250 | public function destroy($newsId) |
||
251 | { |
||
252 | $this->model = $this->find($newsId); |
||
253 | |||
254 | if ($this->model->newsImages->count()) { |
||
255 | foreach ($this->model->newsImages as $image) { |
||
256 | $this->newsImage->destroy($image->id); |
||
257 | } |
||
258 | } |
||
259 | |||
260 | $directory = app_path() . "/storage/files/news/".$this->model->id; |
||
261 | File::deleteDirectory($directory); |
||
262 | |||
263 | return $this->model->delete(); |
||
264 | } |
||
265 | |||
266 | public function destroyImage($newsImageId) |
||
267 | { |
||
268 | $this->modelImage = $this->findImage($newsImageId); |
||
269 | $filename = $this->storageImagePath.$this->modelImage->news_id."/".$this->modelImage->file; |
||
270 | $shopId = auth('hideyobackend')->user()->selected_shop_id; |
||
271 | $shop = ShopService::find($shopId); |
||
272 | |||
273 | if (File::exists($filename)) { |
||
274 | File::delete($filename); |
||
275 | if ($shop->thumbnail_square_sizes) { |
||
276 | $sizes = explode(',', $shop->thumbnail_square_sizes); |
||
277 | if ($sizes) { |
||
278 | foreach ($sizes as $valueImage) { |
||
279 | File::delete($this->publicImagePath.$valueImage."/".$this->modelImage->news_id."/".$this->modelImage->file); |
||
280 | } |
||
281 | } |
||
282 | } |
||
283 | } |
||
284 | |||
285 | return $this->modelImage->delete(); |
||
286 | } |
||
287 | |||
288 | public function destroyGroup($newsGroupId) |
||
289 | { |
||
290 | $this->modelGroup = $this->findGroup($newsGroupId); |
||
291 | $this->modelGroup->save(); |
||
292 | return $this->modelGroup->delete(); |
||
293 | } |
||
294 | |||
295 | public function selectAllGroups() |
||
296 | { |
||
297 | return $this->model->where('shop_id', auth('hideyobackend')->user()->selected_shop_id)->get(); |
||
298 | } |
||
299 | |||
300 | public function findGroup($groupId) |
||
301 | { |
||
302 | return $this->modelGroup->find($groupId); |
||
303 | } |
||
304 | |||
305 | public function getGroupModel() |
||
306 | { |
||
307 | return $this->modelGroup; |
||
308 | } |
||
309 | |||
310 | public function findImage($imageId) |
||
311 | { |
||
312 | return $this->modelImage->find($imageId); |
||
313 | } |
||
314 | |||
315 | public function getImageModel() |
||
318 | } |
||
319 | |||
320 | function selectOneBySlug($shopId, $slug) |
||
321 | { |
||
322 | $dt = Carbon::now('Europe/Amsterdam'); |
||
323 | return $this->model->where('slug', $slug)->where('published_at', '<=', $dt->toDateString('Y-m-d'))->get()->first(); |
||
324 | } |
||
325 | |||
326 | function selectAllByBlogCategoryId($newsCategoryId) |
||
327 | { |
||
328 | return $this->model->with(array('extraFields' => function ($query) { |
||
329 | }, 'taxRate', 'newsCategory', 'relatedBlogs' => function ($query) { |
||
330 | $query->with('newsImages')->orderBy('rank', 'asc'); |
||
331 | }, 'newsImages' => function ($query) { |
||
332 | $query->orderBy('rank', 'asc'); |
||
333 | }))->where('active', 1)->where('news_category_id', '=', $newsCategoryId)->get(); |
||
334 | } |
||
335 | |||
336 | function selectOneById($shopId, $slug) |
||
337 | { |
||
338 | $dt = Carbon::now('Europe/Amsterdam'); |
||
339 | $result = $this->model->with(array('newsCategory', 'relatedBlogs', 'newsImages' => function ($query) { |
||
340 | $query->orderBy('rank', 'asc'); |
||
341 | }))->where('published_at', '<=', $dt->toDateString('Y-m-d'))->where('active', 1)->where('id', $id)->get()->first(); |
||
342 | return $result; |
||
343 | } |
||
344 | |||
345 | function selectAllActiveGroupsByShopId($shopId) |
||
346 | { |
||
347 | return $this->modelGroup->where('shop_id', $shopId)->where('active', 1)->get(); |
||
348 | } |
||
349 | |||
350 | function selectOneGroupByShopIdAndSlug($shopId, $slug) |
||
351 | { |
||
352 | $result = $this->modelGroup->where('shop_id', $shopId)->where('slug', $slug)->get(); |
||
353 | |||
354 | if ($result->isEmpty()) { |
||
355 | return false; |
||
356 | } |
||
357 | return $result->first(); |
||
358 | } |
||
359 | |||
360 | public function selectByLimitAndOrderBy($shopId, $limit, $orderBy) |
||
361 | { |
||
362 | $dt = Carbon::now('Europe/Amsterdam'); |
||
363 | |||
364 | return $this->model->with( |
||
365 | array('newsImages' => function ($query) { |
||
366 | $query->orderBy('rank', 'asc'); |
||
367 | }) |
||
368 | ) |
||
369 | ->limit($limit) |
||
370 | ->where('shop_id', $shopId) |
||
371 | ->where('published_at', '<=', $dt->toDateString('Y-m-d')) |
||
372 | ->orderBy('created_at', $orderBy)->get(); |
||
373 | } |
||
374 | |||
375 | function selectAllByShopIdAndPaginate($shopId, $totalPage, $filters = false) |
||
376 | { |
||
377 | $dt = Carbon::now('Europe/Amsterdam'); |
||
378 | |||
379 | $result = $this->model |
||
380 | ->where('shop_id', $shopId) |
||
381 | ->where('published_at', '<=', $dt->toDateString('Y-m-d')); |
||
382 | |||
383 | return array( |
||
384 | 'totals' => $result->get()->count(), |
||
385 | 'totalPages' => ceil($result->get()->count() / $totalPage), |
||
386 | 'result' => $result->paginate($totalPage), |
||
387 | 'totalOnPage' => $totalPage |
||
388 | ); |
||
389 | } |
||
390 | |||
391 | function selectByGroupAndByShopIdAndPaginate($shopId, $newsGroupSlug, $totalPage, $filters = false) |
||
407 | ); |
||
408 | } |
||
409 | } |