| Total Complexity | 40 |
| Total Lines | 263 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ContentService 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 ContentService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class ContentService extends BaseService |
||
| 11 | { |
||
| 12 | public function __construct(ContentRepository $taxRate) |
||
| 13 | { |
||
| 14 | $this->repo = $taxRate; |
||
|
|
|||
| 15 | } |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The validation rules for the model. |
||
| 19 | * |
||
| 20 | * @param integer $id id attribute model |
||
| 21 | * @return array |
||
| 22 | */ |
||
| 23 | public function rules($contentId = false, $attributes = false) |
||
| 24 | { |
||
| 25 | if (isset($attributes['seo'])) { |
||
| 26 | $rules = array( |
||
| 27 | 'meta_title' => 'required|between:4,65|unique_with:'.$this->repo->getModel()->getTable().', shop_id' |
||
| 28 | ); |
||
| 29 | } else { |
||
| 30 | $rules = array( |
||
| 31 | 'title' => 'required|between:4,65|unique_with:'.$this->repo->getModel()->getTable().', shop_id' |
||
| 32 | ); |
||
| 33 | |||
| 34 | if ($contentId) { |
||
| 35 | $rules['title'] = 'required|between:4,65|unique_with:'.$this->repo->getModel()->getTable().', shop_id, '.$contentId.' = id'; |
||
| 36 | } |
||
| 37 | } |
||
| 38 | |||
| 39 | return $rules; |
||
| 40 | } |
||
| 41 | |||
| 42 | public function create(array $attributes) |
||
| 43 | { |
||
| 44 | $attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
||
| 45 | $validator = Validator::make($attributes, $this->rules()); |
||
| 46 | |||
| 47 | if ($validator->fails()) { |
||
| 48 | return $validator; |
||
| 49 | } |
||
| 50 | |||
| 51 | $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
||
| 52 | $this->repo->getModel()->fill($attributes); |
||
| 53 | $this->repo->getModel()->save(); |
||
| 54 | return $this->repo->getModel(); |
||
| 55 | } |
||
| 56 | |||
| 57 | public function updateById(array $attributes, $id) |
||
| 58 | { |
||
| 59 | $attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
||
| 60 | $validator = Validator::make($attributes, $this->rules($id)); |
||
| 61 | if ($validator->fails()) { |
||
| 62 | return $validator; |
||
| 63 | } |
||
| 64 | |||
| 65 | $model = $this->find($id); |
||
| 66 | $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
||
| 67 | |||
| 68 | if (count($attributes) > 0) { |
||
| 69 | $model->fill($attributes); |
||
| 70 | $model->save(); |
||
| 71 | } |
||
| 72 | return $model; |
||
| 73 | } |
||
| 74 | |||
| 75 | public function destroy($newsId) |
||
| 76 | { |
||
| 77 | $this->model = $this->find($newsId); |
||
| 78 | $this->model->save(); |
||
| 79 | |||
| 80 | if ($this->model->contentImages()->count()) { |
||
| 81 | foreach ($this->model->contentImages()->get() as $image) { |
||
| 82 | $this->contentImage->destroy($image->id); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | $directory = app_path() . "/storage/files/".$this->model->shop_id."/content/".$this->model->id; |
||
| 87 | File::deleteDirectory($directory); |
||
| 88 | |||
| 89 | return $this->model->delete(); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * The validation rules for the modelGroup. |
||
| 94 | * |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | public function rulesGroup($contentGroupId = false, $attributes = false) |
||
| 98 | { |
||
| 99 | $rules = array( |
||
| 100 | 'title' => 'required|between:4,65|unique:'.$this->repo->getGroupModel()->getTable().'' |
||
| 101 | ); |
||
| 102 | |||
| 103 | if ($contentGroupId) { |
||
| 104 | $rules['title'] = 'required|between:4,65|unique:'.$this->repo->getGroupModel()->getTable().',title,'.$contentGroupId; |
||
| 105 | } |
||
| 106 | |||
| 107 | |||
| 108 | return $rules; |
||
| 109 | } |
||
| 110 | |||
| 111 | |||
| 112 | public function createGroup(array $attributes) |
||
| 113 | { |
||
| 114 | $attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
||
| 115 | $validator = Validator::make($attributes, $this->rulesGroup()); |
||
| 116 | |||
| 117 | if ($validator->fails()) { |
||
| 118 | return $validator; |
||
| 119 | } |
||
| 120 | |||
| 121 | $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
||
| 122 | |||
| 123 | $this->repo->getGroupModel()->fill($attributes); |
||
| 124 | $this->repo->getGroupModel()->save(); |
||
| 125 | |||
| 126 | return $this->repo->getGroupModel(); |
||
| 127 | } |
||
| 128 | |||
| 129 | public function createImage(array $attributes, $contentId) |
||
| 130 | { |
||
| 131 | $userId = auth('hideyobackend')->user()->id; |
||
| 132 | $shopId = auth('hideyobackend')->user()->selected_shop_id; |
||
| 133 | $shop = ShopService::find($shopId); |
||
| 134 | |||
| 135 | $rules = array( |
||
| 136 | 'file'=>'required|image|max:1000', |
||
| 137 | 'rank' => 'required' |
||
| 138 | ); |
||
| 139 | |||
| 140 | $validator = Validator::make($attributes, $rules); |
||
| 141 | |||
| 142 | if ($validator->fails()) { |
||
| 143 | return $validator; |
||
| 144 | } |
||
| 145 | |||
| 146 | $attributes['modified_by_user_id'] = $userId; |
||
| 147 | |||
| 148 | $destinationPath = storage_path() . "/app/files/content/".$contentId; |
||
| 149 | $attributes['user_id'] = $userId; |
||
| 150 | $attributes['content_id'] = $contentId; |
||
| 151 | $attributes['extension'] = $attributes['file']->getClientOriginalExtension(); |
||
| 152 | $attributes['size'] = $attributes['file']->getSize(); |
||
| 153 | |||
| 154 | $filename = str_replace(" ", "_", strtolower($attributes['file']->getClientOriginalName())); |
||
| 155 | $uploadSuccess = $attributes['file']->move($destinationPath, $filename); |
||
| 156 | |||
| 157 | if ($uploadSuccess) { |
||
| 158 | $attributes['file'] = $filename; |
||
| 159 | $attributes['path'] = $uploadSuccess->getRealPath(); |
||
| 160 | |||
| 161 | $this->modelImage->fill($attributes); |
||
| 162 | $this->modelImage->save(); |
||
| 163 | |||
| 164 | if ($shop->square_thumbnail_sizes) { |
||
| 165 | $sizes = explode(',', $shop->square_thumbnail_sizes); |
||
| 166 | if ($sizes) { |
||
| 167 | foreach ($sizes as $valueSize) { |
||
| 168 | $image = Image::make($uploadSuccess->getRealPath()); |
||
| 169 | $explode = explode('x', $valueSize); |
||
| 170 | $image->resize($explode[0], $explode[1]); |
||
| 171 | $image->interlace(); |
||
| 172 | |||
| 173 | if (!File::exists(public_path() . "/files/content/".$valueSize."/".$contentId."/")) { |
||
| 174 | File::makeDirectory(public_path() . "/files/content/".$valueSize."/".$contentId."/", 0777, true); |
||
| 175 | } |
||
| 176 | $image->save(public_path() . "/files/content/".$valueSize."/".$contentId."/".$filename); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | return $this->modelImage; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | public function updateGroupById(array $attributes, $newsGroupId) |
||
| 186 | { |
||
| 187 | $validator = Validator::make($attributes, $this->rulesGroup($newsGroupId, $attributes)); |
||
| 188 | |||
| 189 | if ($validator->fails()) { |
||
| 190 | return $validator; |
||
| 191 | } |
||
| 192 | |||
| 193 | $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
||
| 194 | $this->modelGroup = $this->findGroup($newsGroupId); |
||
| 195 | return $this->updateGroupEntity($attributes); |
||
| 196 | } |
||
| 197 | |||
| 198 | private function updateGroupEntity(array $attributes = array()) |
||
| 199 | { |
||
| 200 | if (count($attributes) > 0) { |
||
| 201 | $this->modelGroup->fill($attributes); |
||
| 202 | $this->modelGroup->save(); |
||
| 203 | } |
||
| 204 | |||
| 205 | return $this->modelGroup; |
||
| 206 | } |
||
| 207 | |||
| 208 | public function updateImageById(array $attributes, $contentId, $newsImageId) |
||
| 209 | { |
||
| 210 | $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
||
| 211 | $this->modelImage = $this->find($newsImageId); |
||
| 212 | return $this->updateImageEntity($attributes); |
||
| 213 | } |
||
| 214 | |||
| 215 | private function updateImageEntity(array $attributes = array()) |
||
| 216 | { |
||
| 217 | if (count($attributes) > 0) { |
||
| 218 | $this->modelImage->fill($attributes); |
||
| 219 | $this->modelImage->save(); |
||
| 220 | } |
||
| 221 | |||
| 222 | return $this->modelImage; |
||
| 223 | } |
||
| 224 | |||
| 225 | public function destroyImage($newsImageId) |
||
| 226 | { |
||
| 227 | $this->modelImage = $this->findImage($newsImageId); |
||
| 228 | $filename = storage_path() ."/app/files/content/".$this->modelImage->content_id."/".$this->modelImage->file; |
||
| 229 | $shopId = auth('hideyobackend')->user()->selected_shop_id; |
||
| 230 | $shop = ShopService::find($shopId); |
||
| 231 | |||
| 232 | if (File::exists($filename)) { |
||
| 233 | File::delete($filename); |
||
| 234 | if ($shop->square_thumbnail_sizes) { |
||
| 235 | $sizes = explode(',', $shop->square_thumbnail_sizes); |
||
| 236 | if ($sizes) { |
||
| 237 | foreach ($sizes as $valueSize) { |
||
| 238 | File::delete(public_path() . "/files/content/".$valueSize."/".$this->modelImage->content_id."/".$this->modelImage->file); |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | return $this->modelImage->delete(); |
||
| 245 | } |
||
| 246 | |||
| 247 | public function destroyGroup($newsGroupId) |
||
| 248 | { |
||
| 249 | $this->modelGroup = $this->findGroup($newsGroupId); |
||
| 250 | $this->modelGroup->save(); |
||
| 251 | |||
| 252 | return $this->modelGroup->delete(); |
||
| 253 | } |
||
| 254 | |||
| 255 | public function getGroupModel() |
||
| 256 | { |
||
| 257 | return $this->repo->getGroupModel(); |
||
| 258 | } |
||
| 259 | |||
| 260 | public function findGroup($id) |
||
| 261 | { |
||
| 262 | return $this->repo->findGroup($id); |
||
| 263 | } |
||
| 264 | |||
| 265 | public function selectAllGroups() |
||
| 266 | { |
||
| 267 | return $this->repo->selectAllGroups(); |
||
| 268 | } |
||
| 269 | |||
| 270 | public function getImageModel() |
||
| 273 | } |
||
| 274 | } |