| Conditions | 35 |
| Paths | 1 |
| Total Lines | 242 |
| Code Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php namespace App\Modules\V1\Core\AbstractRepositories; |
||
| 173 | public function save(array $data, $saveLog = true) |
||
| 174 | { |
||
| 175 | $model = false; |
||
| 176 | $modelClass = $this->model; |
||
| 177 | $relations = []; |
||
| 178 | $with = []; |
||
| 179 | |||
| 180 | \DB::transaction(function () use (&$model, &$relations, &$with, $data, $saveLog, $modelClass) { |
||
| 181 | /** |
||
| 182 | * If the id is present in the data then select the model for updating, |
||
| 183 | * else create new model. |
||
| 184 | * @var array |
||
| 185 | */ |
||
| 186 | $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
||
| 187 | if ( ! $model) |
||
| 188 | { |
||
| 189 | \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Construct the model object with the given data, |
||
| 194 | * and if there is a relation add it to relations array, |
||
| 195 | * then save the model. |
||
| 196 | */ |
||
| 197 | foreach ($data as $key => $value) |
||
| 198 | { |
||
| 199 | /** |
||
| 200 | * If the attribute is a relation. |
||
| 201 | */ |
||
| 202 | $relation = camel_case($key); |
||
| 203 | if (method_exists($model, $relation)) |
||
| 204 | { |
||
| 205 | /** |
||
| 206 | * Add the relation to the with array to eager load the created model with the relations. |
||
| 207 | */ |
||
| 208 | $with[] = $relation; |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Check if the relation is a collection. |
||
| 212 | */ |
||
| 213 | if (class_basename($model->$relation) == 'Collection') |
||
| 214 | { |
||
| 215 | /** |
||
| 216 | * If the relation has no value then marke the relation data |
||
| 217 | * related to the model to be deleted. |
||
| 218 | */ |
||
| 219 | if ( ! $value || ! count($value)) |
||
| 220 | { |
||
| 221 | $relations[$relation] = 'delete'; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | if (is_array($value)) |
||
| 225 | { |
||
| 226 | /** |
||
| 227 | * Loop through the relation data. |
||
| 228 | */ |
||
| 229 | foreach ($value as $attr => $val) |
||
| 230 | { |
||
| 231 | /** |
||
| 232 | * Get the relation model. |
||
| 233 | */ |
||
| 234 | $relationBaseModel = \Core::$relation()->model; |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Check if the relation is a collection. |
||
| 238 | */ |
||
| 239 | if (class_basename($model->$relation) == 'Collection') |
||
| 240 | { |
||
| 241 | /** |
||
| 242 | * If the id is present in the data then select the relation model for updating, |
||
| 243 | * else create new model. |
||
| 244 | */ |
||
| 245 | $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
||
| 246 | |||
| 247 | /** |
||
| 248 | * If model doesn't exists. |
||
| 249 | */ |
||
| 250 | if ( ! $relationModel) |
||
| 251 | { |
||
| 252 | \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Loop through the relation attributes. |
||
| 257 | */ |
||
| 258 | foreach ($val as $attr => $val) |
||
| 259 | { |
||
| 260 | /** |
||
| 261 | * Prevent the sub relations or attributes not in the fillable. |
||
| 262 | */ |
||
| 263 | if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
||
| 264 | { |
||
| 265 | $relationModel->$attr = $val; |
||
| 266 | } |
||
| 267 | } |
||
| 268 | $relations[$relation][] = $relationModel; |
||
| 269 | } |
||
| 270 | /** |
||
| 271 | * If not collection. |
||
| 272 | */ |
||
| 273 | else |
||
| 274 | { |
||
| 275 | /** |
||
| 276 | * Prevent the sub relations. |
||
| 277 | */ |
||
| 278 | if (gettype($val) !== 'object' && gettype($val) !== 'array') |
||
| 279 | { |
||
| 280 | /** |
||
| 281 | * If the id is present in the data then select the relation model for updating, |
||
| 282 | * else create new model. |
||
| 283 | */ |
||
| 284 | $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
||
| 285 | |||
| 286 | /** |
||
| 287 | * If model doesn't exists. |
||
| 288 | */ |
||
| 289 | if ( ! $relationModel) |
||
| 290 | { |
||
| 291 | \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Prevent attributes not in the fillable. |
||
| 296 | */ |
||
| 297 | if (array_search($attr, $relationModel->getFillable(), true) !== false) |
||
| 298 | { |
||
| 299 | $relationModel->$attr = $val; |
||
| 300 | $relations[$relation] = $relationModel; |
||
| 301 | } |
||
| 302 | } |
||
| 303 | } |
||
| 304 | } |
||
| 305 | } |
||
| 306 | } |
||
| 307 | /** |
||
| 308 | * If the attribute isn't a relation and prevent attributes not in the fillable. |
||
| 309 | */ |
||
| 310 | else if (array_search($key, $model->getFillable(), true) !== false) |
||
| 311 | { |
||
| 312 | $model->$key = $value; |
||
| 313 | } |
||
| 314 | } |
||
| 315 | /** |
||
| 316 | * Save the model. |
||
| 317 | */ |
||
| 318 | $model->save(); |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Loop through the relations array. |
||
| 322 | */ |
||
| 323 | foreach ($relations as $key => $value) |
||
| 324 | { |
||
| 325 | /** |
||
| 326 | * If the relation is marked for delete then delete it. |
||
| 327 | */ |
||
| 328 | if ($value == 'delete' && $model->$key()->count()) |
||
| 329 | { |
||
| 330 | $model->$key()->delete(); |
||
| 331 | } |
||
| 332 | /** |
||
| 333 | * If the relation is an array. |
||
| 334 | */ |
||
| 335 | else if (gettype($value) == 'array') |
||
| 336 | { |
||
| 337 | $ids = []; |
||
| 338 | /** |
||
| 339 | * Loop through the relations. |
||
| 340 | */ |
||
| 341 | foreach ($value as $val) |
||
| 342 | { |
||
| 343 | switch (class_basename($model->$key())) |
||
| 344 | { |
||
| 345 | /** |
||
| 346 | * If the relation is one to many then update it's foreign key with |
||
| 347 | * the model id and save it then add its id to ids array to delete all |
||
| 348 | * relations who's id isn't in the ids array. |
||
| 349 | */ |
||
| 350 | case 'HasMany': |
||
| 351 | $foreignKeyName = explode('.', $model->$key()->getForeignKey())[1]; |
||
| 352 | $val->$foreignKeyName = $model->id; |
||
| 353 | $val->save(); |
||
| 354 | $ids[] = $val->id; |
||
| 355 | break; |
||
| 356 | |||
| 357 | /** |
||
| 358 | * If the relation is many to many then add it's id to the ids array to |
||
| 359 | * attache these ids to the model. |
||
| 360 | */ |
||
| 361 | case 'BelongsToMany': |
||
| 362 | $val->save(); |
||
| 363 | $ids[] = $val->id; |
||
| 364 | break; |
||
| 365 | } |
||
| 366 | } |
||
| 367 | switch (class_basename($model->$key())) |
||
| 368 | { |
||
| 369 | /** |
||
| 370 | * If the relation is one to many then delete all |
||
| 371 | * relations who's id isn't in the ids array. |
||
| 372 | */ |
||
| 373 | case 'HasMany': |
||
| 374 | $model->$key()->whereNotIn('id', $ids)->delete(); |
||
| 375 | break; |
||
| 376 | |||
| 377 | /** |
||
| 378 | * If the relation is many to many then |
||
| 379 | * detach the previous data and attach |
||
| 380 | * the ids array to the model. |
||
| 381 | */ |
||
| 382 | case 'BelongsToMany': |
||
| 383 | $model->$key()->detach(); |
||
| 384 | $model->$key()->attach($ids); |
||
| 385 | break; |
||
| 386 | } |
||
| 387 | } |
||
| 388 | /** |
||
| 389 | * If the relation isn't array. |
||
| 390 | */ |
||
| 391 | else |
||
| 392 | { |
||
| 393 | switch (class_basename($model->$key())) |
||
| 394 | { |
||
| 395 | /** |
||
| 396 | * If the relation is one to many or one to one. |
||
| 397 | */ |
||
| 398 | case 'BelongsTo': |
||
| 399 | $value->save(); |
||
| 400 | $model->$key()->associate($value); |
||
| 401 | $model->save(); |
||
| 402 | break; |
||
| 403 | } |
||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | $saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false; |
||
| 408 | }); |
||
| 409 | |||
| 410 | /** |
||
| 411 | * return the saved mdel with the given relations. |
||
| 412 | */ |
||
| 413 | return $this->find($model->id, $with); |
||
| 414 | } |
||
| 415 | |||
| 536 | } |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.