Total Complexity | 49 |
Total Lines | 393 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ClientService 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 ClientService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class ClientService extends BaseService |
||
14 | { |
||
15 | public function __construct(ClientRepository $client, ClientAddressRepository $clientAddress) |
||
16 | { |
||
17 | $this->repo = $client; |
||
18 | $this->repoAddress = $clientAddress; |
||
19 | } |
||
20 | |||
21 | /** |
||
22 | * The validation rules for the model. |
||
23 | * |
||
24 | * @param integer $clientId id attribute model |
||
25 | * @return array |
||
26 | */ |
||
27 | private function rules($clientId = false) |
||
28 | { |
||
29 | if ($clientId) { |
||
30 | $rules = array( |
||
31 | 'email' => 'required|email|unique_with:client, shop_id' |
||
32 | ); |
||
33 | } else { |
||
34 | $rules = array( |
||
35 | 'email' => 'required|email|unique_with:'.$this->repo->getModel()->getTable().', shop_id', |
||
36 | 'gender' => 'required', |
||
37 | 'firstname' => 'required', |
||
38 | 'lastname' => 'required', |
||
39 | 'street' => 'required', |
||
40 | 'housenumber' => 'required|integer', |
||
41 | 'zipcode' => 'required', |
||
42 | 'city' => 'required', |
||
43 | 'country' => 'required' |
||
44 | ); |
||
45 | } |
||
46 | |||
47 | |||
48 | if ($clientId) { |
||
49 | $rules['email'] = 'required|email|unique_with:'.$this->repo->getModel()->getTable().', shop_id, '.$clientId.' = id'; |
||
50 | } |
||
51 | |||
52 | return $rules; |
||
53 | } |
||
54 | |||
55 | public function create(array $attributes) |
||
56 | { |
||
57 | $attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
||
58 | $validator = Validator::make($attributes, $this->rules()); |
||
59 | |||
60 | if ($validator->fails()) { |
||
61 | return $validator; |
||
62 | } |
||
63 | |||
64 | $attributes['password'] = Hash::make($attributes['password']); |
||
65 | $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
||
66 | $this->repo->getModel()->fill($attributes); |
||
67 | $this->repo->getModel()->save(); |
||
68 | $clientAddress = $this->createAddress($attributes, $this->repo->getModel()->id); |
||
69 | $new['delivery_client_address_id'] = $clientAddress->id; |
||
70 | $new['bill_client_address_id'] = $clientAddress->id; |
||
71 | $this->repo->getModel()->fill($new); |
||
72 | $this->repo->getModel()->save(); |
||
73 | return $this->repo->getModel(); |
||
74 | } |
||
75 | |||
76 | public function updateById(array $attributes, $clientId) |
||
77 | { |
||
78 | $model = $this->find($clientId); |
||
79 | $attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
||
80 | $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
||
81 | |||
82 | $validator = Validator::make($attributes, $this->rules($clientId)); |
||
83 | |||
84 | if ($validator->fails()) { |
||
85 | return $validator; |
||
86 | } |
||
87 | |||
88 | if ($attributes['password']) { |
||
89 | $attributes['password'] = Hash::make($attributes['password']); |
||
90 | } |
||
91 | |||
92 | $model->fill($attributes); |
||
93 | $model->save(); |
||
94 | |||
95 | return $model; |
||
96 | } |
||
97 | |||
98 | public function validateConfirmationCode($confirmationCode, $email, $shopId) |
||
101 | } |
||
102 | |||
103 | public function validateLogin($attributes) |
||
111 | } |
||
112 | |||
113 | public function login($request) { |
||
114 | |||
115 | $loginData = array( |
||
116 | 'email' => $request->get('email'), |
||
117 | 'password' => $request->get('password'), |
||
118 | 'confirmed' => 1, |
||
119 | 'active' => 1, |
||
120 | 'shop_id' => config()->get('app.shop_id') |
||
121 | ); |
||
122 | |||
123 | if (auth('web')->attempt($loginData)) { |
||
124 | return true; |
||
125 | } |
||
126 | |||
127 | |||
128 | } |
||
129 | |||
130 | public function confirmClient($confirmationCode, $email, $shopId) |
||
131 | { |
||
132 | $model = $this->repo->getClientByConfirmationCode($shopId, $email, $confirmationCode); |
||
133 | |||
134 | if ($model) { |
||
135 | $attributes['confirmed'] = 1; |
||
136 | $attributes['active'] = 1; |
||
137 | $attributes['confirmation_code'] = null; |
||
138 | |||
139 | $model->fill($attributes); |
||
140 | $model->save(); |
||
141 | return $model; |
||
142 | } |
||
143 | |||
144 | return false; |
||
145 | } |
||
146 | |||
147 | public function getConfirmationCodeByEmail($email, $shopId) |
||
148 | { |
||
149 | $model = $this->repo->checkEmailByShopIdAndNoAccountCreated($email, $shopId); |
||
150 | |||
151 | if ($model) { |
||
152 | $attributes['confirmation_code'] = md5(uniqid(mt_rand(), true)); |
||
153 | $model->fill($attributes); |
||
154 | $model->save(); |
||
155 | |||
156 | return $model; |
||
157 | } |
||
158 | |||
159 | return false; |
||
160 | } |
||
161 | |||
162 | public function validateRegister($attributes) { |
||
163 | |||
164 | // create the validation rules ------------------------ |
||
165 | $rules = array( |
||
166 | 'email' => 'required|email', // required and must be unique in the ducks table |
||
167 | 'password' => 'required', |
||
168 | 'firstname' => 'required', |
||
169 | 'lastname' => 'required', |
||
170 | 'zipcode' => 'required', |
||
171 | 'housenumber' => 'required|numeric', |
||
172 | 'houseletter' => 'alpha', |
||
173 | 'street' => 'required', |
||
174 | 'city' => 'required', |
||
175 | 'country' => 'required' |
||
176 | ); |
||
177 | |||
178 | return $validator = Validator::make($attributes, $rules); |
||
179 | } |
||
180 | |||
181 | public function register($attributes, $shopId, $accountConfirmed = false) |
||
182 | { |
||
183 | $client = $this->repo->checkEmailByShopId($attributes['email'], $shopId); |
||
184 | |||
185 | if($client AND $client->account_created) { |
||
186 | return false; |
||
187 | } |
||
188 | |||
189 | if($client) { |
||
190 | $model = $client; |
||
191 | } else { |
||
192 | $model = $this->repo->getModel(); |
||
193 | } |
||
194 | |||
195 | $attributes['shop_id'] = $shopId; |
||
196 | $attributes['modified_by_user_id'] = null; |
||
197 | $attributes['active'] = 0; |
||
198 | $attributes['confirmed'] = 0; |
||
199 | $attributes['confirmation_code'] = md5(uniqid(mt_rand(), true)); |
||
200 | if($accountConfirmed) { |
||
201 | $attributes['active'] = 1; |
||
202 | $attributes['confirmed'] = 1; |
||
203 | $attributes['confirmation_code'] = null; |
||
204 | } |
||
205 | |||
206 | if (isset($attributes['password'])) { |
||
207 | $attributes['password'] = Hash::make($attributes['password']); |
||
208 | $attributes['account_created'] = Carbon::now()->toDateTimeString(); |
||
209 | } |
||
210 | |||
211 | $model->fill($attributes); |
||
212 | $model->save(); |
||
213 | |||
214 | $clientAddress = $this->createAddress($attributes, $model->id); |
||
215 | $new['delivery_client_address_id'] = $clientAddress->id; |
||
216 | $new['bill_client_address_id'] = $clientAddress->id; |
||
217 | $model->fill($new); |
||
218 | $model->save(); |
||
219 | return $model; |
||
220 | } |
||
221 | |||
222 | public function createAddress($attributes, $clientId) |
||
230 | } |
||
231 | |||
232 | public function editAddress($clientId, $addressId, $attributes) |
||
233 | { |
||
234 | |||
235 | $clientAddress = $this->repoAddress->find($addressId); |
||
236 | |||
237 | if($clientAddress) { |
||
238 | $clientAddress->fill($attributes); |
||
239 | $clientAddress->save(); |
||
240 | |||
241 | return $clientAddress; |
||
242 | } |
||
243 | |||
244 | return false; |
||
245 | } |
||
246 | |||
247 | public function updateByIdAndShopId($shopId, array $attributes, $clientId, $id) |
||
248 | { |
||
249 | $this->model = $this->find($id); |
||
250 | return $this->updateEntity($attributes); |
||
251 | } |
||
252 | |||
253 | public function requestChangeAccountDetails($attributes, $shopId) { |
||
254 | |||
255 | $client = $this->repo->checkEmailByShopId($attributes['email'], $shopId); |
||
256 | |||
257 | if ($client) { |
||
258 | $newAttributes = array( |
||
259 | 'new_email' => $attributes['email'], |
||
260 | 'new_password' => Hash::make($attributes['password']), |
||
261 | 'confirmation_code' => md5(uniqid(mt_rand(), true)) |
||
262 | ); |
||
263 | |||
264 | $client->fill($newAttributes); |
||
265 | $client->save(); |
||
266 | |||
267 | return $client; |
||
268 | } |
||
269 | |||
270 | return false; |
||
271 | } |
||
272 | |||
273 | public function changeAccountDetails($confirmationCode, $newEmail, $shopId) { |
||
293 | } |
||
294 | |||
295 | public function changePassword(array $attributes, $shopId) |
||
296 | { |
||
297 | $result = array(); |
||
298 | $result['result'] = false; |
||
299 | |||
300 | $client = $this->repo->validateConfirmationCodeByConfirmationCodeAndEmail($attributes['confirmation_code'], $attributes['email'], $shopId); |
||
301 | |||
302 | if ($client) { |
||
303 | if ($attributes['password']) { |
||
304 | $newAttributes['confirmed'] = 1; |
||
305 | $newAttributes['active'] = 1; |
||
306 | $newAttributes['confirmation_code'] = null; |
||
307 | $newAttributes['password'] = Hash::make($attributes['password']); |
||
308 | $client->fill($newAttributes); |
||
309 | $client->save(); |
||
310 | return $client; |
||
311 | } |
||
312 | } |
||
313 | |||
314 | return false; |
||
315 | } |
||
316 | |||
317 | |||
318 | public function selectAllExport() { |
||
319 | return $this->repo->selectAllExport(); |
||
320 | } |
||
321 | |||
322 | |||
323 | public function activate($clientId) |
||
339 | } |
||
340 | |||
341 | public function deactivate($clientId) |
||
342 | { |
||
357 | } |
||
358 | |||
359 | public function setBillOrDeliveryAddress($shopId, $clientId, $addressId, $type) |
||
379 | } |
||
380 | |||
381 | |||
382 | public function selectOneByShopIdAndId($shopId, $clientId) |
||
383 | { |
||
384 | return $this->repo->selectOneByShopIdAndId($shopId, $clientId); |
||
385 | } |
||
386 | |||
387 | |||
388 | public function selectAddressesByClientId($clientId) { |
||
390 | } |
||
391 | |||
392 | public function getAddressModel() |
||
393 | { |
||
394 | return $this->repoAddress->getModel(); |
||
395 | } |
||
396 | |||
397 | public function findAddress($clientAddressId) |
||
400 | } |
||
401 | |||
402 | |||
403 | public function validateRegisterNoAccount(array $attributes, $shopId) |
||
406 | |||
407 | } |
||
410 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths