Passed
Push — master ( 5a03f9...8e29e6 )
by Stephen
03:56 queued 01:10
created

ModelQueries::resolveModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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