Passed
Push — feature/events ( 9d660f...96fd77 )
by Mathieu
02:50
created

DBObjectRelations::isRelationLoaded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Suricate\Traits;
6
7
use Suricate\DBObject;
8
9
trait DBObjectRelations
10
{
11
    protected $relations = [];
12
    protected $relationValues = [];
13
    protected $loadedRelations = [];
14
15
    /**
16
     * Get relation from its name
17
     *
18
     * @param string $name
19
     */
20 1
    protected function getRelation($name)
21
    {
22
        if (
23 1
            isset($this->relationValues[$name]) &&
24 1
            $this->isRelationLoaded($name)
25
        ) {
26 1
            return $this->relationValues[$name];
27
        }
28
29 1
        if (!$this->isRelationLoaded($name)) {
30 1
            $this->loadRelation($name);
31 1
            $this->markRelationAsLoaded($name);
32
        }
33
34 1
        if (isset($this->relationValues[$name])) {
35 1
            return $this->relationValues[$name];
36
        }
37
38
        return null;
39
    }
40
41
    /**
42
     * Check if variable is predefined relation
43
     * @param  string  $name variable name
44
     * @return boolean
45
     */
46 7
    protected function isRelation($name)
47
    {
48 7
        return isset($this->relations[$name]);
49
    }
50
    /**
51
     * Define object relations
52
     *
53
     * @return DBObject
54
     */
55 28
    protected function setRelations()
56
    {
57 28
        $this->relations = [];
58
59 28
        return $this;
60
    }
61
62
    /**
63
     * Mark a relation as loaded
64
     * @param  string $name varialbe name
65
     * @return void
66
     */
67 1
    protected function markRelationAsLoaded($name)
68
    {
69 1
        if ($this->isRelation($name)) {
70 1
            $this->loadedRelations[$name] = true;
71
        }
72 1
    }
73
    /**
74
     * Check if a relation already have been loaded
75
     * @param  string  $name Variable name
76
     * @return boolean
77
     */
78 1
    protected function isRelationLoaded($name)
79
    {
80 1
        return isset($this->loadedRelations[$name]);
81
    }
82
83
    /**
84
     * Load realation according to relation type
85
     *
86
     * @param string $name
87
     * @return void
88
     */
89 1
    protected function loadRelation($name)
90
    {
91 1
        if ($this->isRelation($name)) {
92 1
            switch ($this->relations[$name]['type']) {
93
                case DBObject::RELATION_ONE_ONE:
94 1
                    $this->loadRelationOneOne($name);
95 1
                    return;
96
                case DBObject::RELATION_ONE_MANY:
97
                    $this->loadRelationOneMany($name);
98
                    return;
99
                case DBObject::RELATION_MANY_MANY:
100
                    $this->loadRelationManyMany($name);
101
                    return;
102
            }
103
        }
104
    }
105
106
    /**
107
     * Load one to one relationship
108
     *
109
     * @param string $name
110
     * @return void
111
     */
112 1
    private function loadRelationOneOne($name)
113
    {
114 1
        $target = $this->relations[$name]['target'];
115 1
        $source = $this->relations[$name]['source'];
116 1
        $this->relationValues[$name] = new $target();
117 1
        $this->relationValues[$name]->load($this->$source);
118 1
    }
119
120
    /**
121
     * Load one to many relationship
122
     *
123
     * @param string $name
124
     * @return void
125
     */
126
    private function loadRelationOneMany($name)
127
    {
128
        $target = $this->relations[$name]['target'];
129
        $parentId = $this->{$this->relations[$name]['source']};
130
        $parentIdField = isset($this->relations[$name]['target_field'])
131
            ? $this->relations[$name]['target_field']
132
            : null;
133
        $validate = dataGet($this->relations[$name], 'validate', null);
134
135
        $this->relationValues[$name] = $target::loadForParentId(
136
            $parentId,
137
            $parentIdField,
138
            $validate
139
        );
140
    }
141
142
    /**
143
     * Load many to many relationship
144
     *
145
     * @param string $name
146
     * @return void
147
     */
148
    private function loadRelationManyMany($name)
149
    {
150
        $pivot = $this->relations[$name]['pivot'];
151
        $sourceType = $this->relations[$name]['source_type'];
152
        $target = dataGet($this->relations[$name], 'target');
153
        $validate = dataGet($this->relations[$name], 'validate', null);
154
155
        $this->relationValues[$name] = $pivot::loadFor(
156
            $sourceType,
157
            $this->{$this->relations[$name]['source']},
158
            $target,
159
            $validate
160
        );
161
    }
162
}
163