Completed
Push — master ( efc7f4...497618 )
by Filipe
02:47
created

AbstractRelation::getParentEntityDescriptor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php
2
3
/**
4
 * This file is part of slick/orm package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Orm\Mapper\Relation;
11
12
use Slick\Common\Base;
13
use Slick\Common\Utils\Text;
14
use Slick\Database\Adapter\AdapterInterface;
15
use Slick\Orm\Descriptor\EntityDescriptorInterface;
16
use Slick\Orm\Descriptor\EntityDescriptorRegistry;
17
use Slick\Orm\Entity\EntityCollection;
18
use Slick\Orm\EntityInterface;
19
use Slick\Orm\Orm;
20
21
/**
22
 * AbstractRelation
23
 *
24
 * @package Slick\Orm\Mapper\Relation
25
 * @author  Filipe Silva <[email protected]>
26
 *
27
 * @property AdapterInterface $adapter
28
 */
29
abstract class AbstractRelation extends Base
30
{
31
    /**
32
     * @readwrite
33
     * @var string
34
     */
35
    protected $propertyName;
36
37
    /**
38
     * @readwrite
39
     * @var EntityDescriptorInterface
40
     */
41
    protected $entityDescriptor;
42
43
    /**
44
     * Parent or related entity class name
45
     * @readwrite
46
     * @var string
47
     */
48
    protected $parentEntity;
49
50
    /**
51
     * @readwrite
52
     * @var EntityDescriptorInterface
53
     */
54
    protected $parentEntityDescriptor;
55
56
    /**
57
     * @readwrite
58
     * @var string
59
     */
60
    protected $foreignKey;
61
62
    /**
63
     * @readwrite
64
     * @var AdapterInterface
65
     */
66
    protected $adapter;
67
68
    /**
69
     * Returns the property holding the relation
70
     *
71
     * @return string
72
     */
73 2
    public function getPropertyName()
74
    {
75 2
        return $this->propertyName;
76
    }
77
78
79
    /**
80
     * Gets the entity descriptor
81
     *
82
     * @return EntityDescriptorInterface
83
     */
84 2
    public function getEntityDescriptor()
85
    {
86 2
        return $this->entityDescriptor;
87
    }
88
89
    /**
90
     * Sets entity descriptor
91
     *
92
     * @param EntityDescriptorInterface $descriptor
93
     * @return $this
94
     */
95 18
    public function setEntityDescriptor(EntityDescriptorInterface $descriptor)
96
    {
97 18
        $this->entityDescriptor = $descriptor;
98 18
        return $this;
99
    }
100
101
    /**
102
     * Gets parent entity class name
103
     *
104
     * @return string
105
     */
106 2
    public function getParentEntity()
107
    {
108 2
        return $this->parentEntity;
109
    }
110
111
    /**
112
     * Gets the parent or related entity descriptor
113
     *
114
     * @return EntityDescriptorInterface
115
     */
116 4
    public function getParentEntityDescriptor()
117
    {
118 4
        if (is_null($this->parentEntityDescriptor)) {
119 4
            $this->setParentEntityDescriptor(
120 4
                EntityDescriptorRegistry::getInstance()
121 4
                    ->getDescriptorFor($this->parentEntity)
122 2
            );
123 2
        }
124 4
        return $this->parentEntityDescriptor;
125
    }
126
127
    /**
128
     * Sets parent entity descriptor
129
     *
130
     * @param EntityDescriptorInterface $parentEntityDescriptor
131
     * @return BelongsTo
132
     */
133 4
    public function setParentEntityDescriptor(
134
        EntityDescriptorInterface $parentEntityDescriptor
135
    ) {
136 4
        $this->parentEntityDescriptor = $parentEntityDescriptor;
137 4
        return $this;
138
    }
139
140
    /**
141
     * Gets the foreign key field name
142
     *
143
     * @return string
144
     */
145 4
    public function getForeignKey()
146
    {
147 4
        if (is_null($this->foreignKey)) {
148 4
            $name = $this->getParentEntityDescriptor()->getTableName();
149 4
            $name = Text::singular(strtolower($name));
150 4
            $this->foreignKey = "{$name}_id";
151 2
        }
152 4
        return $this->foreignKey;
153
    }
154
155
    /**
156
     * Register the retrieved entities in the repository identity map
157
     *
158
     * @param EntityInterface|EntityCollection $entity
159
     *
160
     * @return EntityInterface|EntityCollection
161
     */
162
    protected function registerEntity($entity)
163
    {
164
        if ($entity instanceof EntityCollection) {
165
            foreach ($entity as $item) {
166
                $this->registerEntity($item);
167
            }
168
        }
169
170
        Orm::getRepository($this->getParentEntity())
171
            ->getIdentityMap()
172
            ->set($entity);
0 ignored issues
show
Bug introduced by
It seems like $entity defined by parameter $entity on line 162 can also be of type object<Slick\Orm\Entity\EntityCollection>; however, Slick\Orm\Repository\IdentityMapInterface::set() does only seem to accept object<Slick\Orm\EntityInterface>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
173
174
        return $entity;
175
    }
176
177
    /**
178
     * Set the database adapter for this relation
179
     *
180
     * @param AdapterInterface $adapter
181
     * @return $this|self|AbstractRelation
182
     */
183 2
    public function setAdapter(AdapterInterface $adapter)
184
    {
185 2
        $this->adapter = $adapter;
186 2
        return $this;
187
    }
188
189
    /**
190
     * Gets relation adapter
191
     *
192
     * @return AdapterInterface
193
     */
194 2
    public function getAdapter()
195
    {
196 2
        if (null == $this->adapter) {
197
            $className = $this->getEntityDescriptor()->className();
198
            $repository = Orm::getRepository($className);
199
            $this->setAdapter($repository->getAdapter());
200
        }
201 2
        return $this->adapter;
202
    }
203
}