ModelQueries   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 3
eloc 10
c 3
b 0
f 0
dl 0
loc 55
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A modelQuery() 0 3 1
A resolveModel() 0 11 2
1
<?php
2
3
namespace Sfneal\CrudModelActions\Utils;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model as EloquentModel;
7
use Sfneal\Builders\QueryBuilder;
8
use Sfneal\Models\Model;
9
10
trait ModelQueries
11
{
12
    /**
13
     * @var Model|EloquentModel
14
     */
15
    protected $model;
16
17
    /**
18
     * The Eloquent Model class.
19
     *
20
     * @var Model|EloquentModel
21
     */
22
    protected $modelClass;
23
24
    /**
25
     * @var int|null
26
     */
27
    protected $related_model_key;
28
29
    /**
30
     * @var array Array of relationships that should be eager loaded with the model
31
     */
32
    protected $eagerLoadRelationships = [];
33
34
    /**
35
     * Set the $model property value.
36
     *
37
     *  - if an integer model_key value is passed, find the model
38
     *  - if a model instance is passed, declare the model
39
     *
40
     * @param  int|EloquentModel|null  $model
41
     * @return EloquentModel|null
42
     */
43
    private function resolveModel($model)
44
    {
45
        // todo: should there be a relatedModelClass or modelRelation attr?
46
        // $model is a Model class
47
        if ($model instanceof $this->modelClass) {
48
            return $model;
49
        }
50
51
        // Find the model using a model key
52
        else {
53
            return $this->modelQuery()->with($this->eagerLoadRelationships)->find($model);
54
        }
55
    }
56
57
    /**
58
     * Retrieve the Model's query builder.
59
     *
60
     * @return QueryBuilder|Builder
61
     */
62
    protected function modelQuery()
63
    {
64
        return $this->modelClass::query();
65
    }
66
}
67