Relation   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 111
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
addConstraints() 0 1 ?
A getQuery() 0 4 1
A getBaseQuery() 0 4 1
A getParent() 0 4 1
A getRelated() 0 4 1
A __call() 0 10 2
1
<?php
2
3
namespace Magister\Services\Database\Elegant\Relations;
4
5
use Magister\Services\Database\Elegant\Builder;
6
use Magister\Services\Database\Elegant\Model;
7
8
/**
9
 * Class Relation.
10
 */
11
abstract class Relation
12
{
13
    /**
14
     * The Elegant query builder instance.
15
     *
16
     * @var \Magister\Services\Database\Elegant\Builder
17
     */
18
    protected $query;
19
20
    /**
21
     * The parent model instance.
22
     *
23
     * @var \Magister\Services\Database\Elegant\Model
24
     */
25
    protected $parent;
26
27
    /**
28
     * The related model instance.
29
     *
30
     * @var \Magister\Services\Database\Elegant\Model
31
     */
32
    protected $related;
33
34
    /**
35
     * Indicates if the relation is adding constraints.
36
     *
37
     * @var bool
38
     */
39
    protected static $constraints = true;
40
41
    /**
42
     * Create a new relation instance.
43
     *
44
     * @param \Magister\Services\Database\Elegant\Builder $query
45
     * @param \Magister\Services\Database\Elegant\Model   $parent
46
     */
47
    public function __construct(Builder $query, Model $parent)
48
    {
49
        $this->query = $query;
50
        $this->parent = $parent;
51
        $this->related = $query->getModel();
52
53
        $this->addConstraints();
54
    }
55
56
    /**
57
     * Set the base constraints on the relation query.
58
     *
59
     * @return void
60
     */
61
    abstract public function addConstraints();
62
63
    /**
64
     * Get the underlying query for the relation.
65
     *
66
     * @return \Magister\Services\Database\Elegant\Builder
67
     */
68
    public function getQuery()
69
    {
70
        return $this->query;
71
    }
72
73
    /**
74
     * Get the base query builder driving the Elegant builder.
75
     *
76
     * @return \Magister\Services\Database\Query\Builder
77
     */
78
    public function getBaseQuery()
79
    {
80
        return $this->query->getQuery();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->query->getQuery(); of type Magister\Services\Databa...atabase\Elegant\Builder adds the type Magister\Services\Database\Elegant\Builder to the return on line 80 which is incompatible with the return type documented by Magister\Services\Databa...\Relation::getBaseQuery of type Magister\Services\Database\Query\Builder.
Loading history...
81
    }
82
83
    /**
84
     * Get the parent model of the relation.
85
     *
86
     * @return \Magister\Services\Database\Elegant\Model
87
     */
88
    public function getParent()
89
    {
90
        return $this->parent;
91
    }
92
93
    /**
94
     * Get the related model of the relation.
95
     *
96
     * @return \Magister\Services\Database\Elegant\Model
97
     */
98
    public function getRelated()
99
    {
100
        return $this->related;
101
    }
102
103
    /**
104
     * Handle dynamic method calls to the relationship.
105
     *
106
     * @param string $method
107
     * @param array  $parameters
108
     *
109
     * @return mixed
110
     */
111
    public function __call($method, $parameters)
112
    {
113
        $result = call_user_func_array([$this->query, $method], $parameters);
114
115
        if ($result === $this->query) {
116
            return $this;
117
        }
118
119
        return $result;
120
    }
121
}
122