Passed
Push — develop ( 95162b...7c0612 )
by Mathieu
01:39
created

DBObjectRelations   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Test Coverage

Coverage 59.61%

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 138
ccs 31
cts 52
cp 0.5961
rs 10
c 0
b 0
f 0
wmc 19

9 Methods

Rating   Name   Duplication   Size   Complexity  
A isRelationLoaded() 0 3 1
A loadRelationOneMany() 0 8 2
A isRelation() 0 3 1
A setRelations() 0 5 1
A loadRelationOneOne() 0 6 1
A loadRelation() 0 13 5
A loadRelationManyMany() 0 8 1
A getRelation() 0 16 5
A markRelationAsLoaded() 0 4 2
1
<?php
2
namespace Suricate\Traits;
3
4
trait DBObjectRelations
5
{
6
    protected $relations                    = [];
7
    protected $relationValues               = [];
8
    protected $loadedRelations              = [];
9
10
    /**
11
     * Get relation from its name
12
     * 
13
     * @param string $name
14
     */
15 1
    protected function getRelation($name)
16
    {
17 1
        if (isset($this->relationValues[$name]) && $this->isRelationLoaded($name)) {
18 1
            return $this->relationValues[$name];
19
        }
20
21 1
        if (!$this->isRelationLoaded($name)) {
22 1
            $this->loadRelation($name);
23 1
            $this->markRelationAsLoaded($name);
24
        }
25
26 1
        if (isset($this->relationValues[$name])) {
27 1
            return $this->relationValues[$name];
28
        }
29
30
        return null;
31
    }
32
33
    /**
34
     * Check if variable is predefined relation
35
     * @param  string  $name variable name
36
     * @return boolean
37
     */
38 5
    protected function isRelation($name)
39
    {
40 5
        return isset($this->relations[$name]);
41
    }
42
    /**
43
     * Define object relations
44
     *
45
     * @return DBObject
46
     */
47 13
    protected function setRelations()
48
    {
49 13
        $this->relations = [];
50
51 13
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Suricate\Traits\DBObjectRelations which is incompatible with the documented return type Suricate\Traits\DBObject.
Loading history...
52
    }
53
54
    /**
55
     * Mark a relation as loaded
56
     * @param  string $name varialbe name
57
     * @return void
58
     */
59 1
    protected function markRelationAsLoaded($name)
60
    {
61 1
        if ($this->isRelation($name)) {
62 1
            $this->loadedRelations[$name] = true;
63
        }
64 1
    }
65
     /**
66
     * Check if a relation already have been loaded
67
     * @param  string  $name Variable name
68
     * @return boolean
69
     */
70 1
    protected function isRelationLoaded($name)
71
    {
72 1
        return isset($this->loadedRelations[$name]);
73
    }
74
75
    /**
76
     * Load realation according to relation type
77
     *
78
     * @param string $name
79
     * @return void
80
     */
81 1
    protected function loadRelation($name)
82
    {
83 1
        if ($this->isRelation($name)) {
84 1
            switch ($this->relations[$name]['type']) {
85 1
                case self::RELATION_ONE_ONE:
0 ignored issues
show
Bug introduced by
The constant Suricate\Traits\DBObject...tions::RELATION_ONE_ONE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
86 1
                    $this->loadRelationOneOne($name);
87 1
                    return;
88
                case self::RELATION_ONE_MANY:
0 ignored issues
show
Bug introduced by
The constant Suricate\Traits\DBObject...ions::RELATION_ONE_MANY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
89
                    $this->loadRelationOneMany($name);
90
                    return;
91
                case self::RELATION_MANY_MANY:
0 ignored issues
show
Bug introduced by
The constant Suricate\Traits\DBObject...ons::RELATION_MANY_MANY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
92
                    $this->loadRelationManyMany($name);
93
                    return;
94
            }
95
        }
96
    }
97
98
    /**
99
     * Load one to one relationship
100
     *
101
     * @param string $name
102
     * @return void
103
     */
104 1
    private function loadRelationOneOne($name)
105
    {
106 1
        $target = $this->relations[$name]['target'];
107 1
        $source = $this->relations[$name]['source'];
108 1
        $this->relationValues[$name] = new $target();
109 1
        $this->relationValues[$name]->load($this->$source);
110 1
    }
111
112
    /**
113
     * Load one to many relationship
114
     *
115
     * @param string $name
116
     * @return void
117
     */
118
    private function loadRelationOneMany($name)
119
    {
120
        $target         = $this->relations[$name]['target'];
121
        $parentId       = $this->{$this->relations[$name]['source']};
122
        $parentIdField  = isset($this->relations[$name]['target_field']) ? $this->relations[$name]['target_field'] : null;
123
        $validate       = dataGet($this->relations[$name], 'validate', null);
124
        
125
        $this->relationValues[$name] = $target::loadForParentId($parentId, $parentIdField, $validate);
126
    }
127
128
    /**
129
     * Load many to many relationship
130
     *
131
     * @param string $name
132
     * @return void
133
     */
134
    private function loadRelationManyMany($name)
135
    {
136
        $pivot      = $this->relations[$name]['pivot'];
137
        $sourceType = $this->relations[$name]['source_type'];
138
        $target     = dataGet($this->relations[$name], 'target');
139
        $validate   = dataGet($this->relations[$name], 'validate', null);
140
141
        $this->relationValues[$name] = $pivot::loadFor($sourceType, $this->{$this->relations[$name]['source']}, $target, $validate);
142
    }
143
}