Completed
Push — master ( b05d3c...2a9f41 )
by Filipe
02:53
created

AbstractRelation::setAdapter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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
 * @property bool $lazyLoaded
29
 *
30
 * @method bool isLazyLoaded()
31
 */
32
abstract class AbstractRelation extends Base
33
{
34
    /**
35
     * @readwrite
36
     * @var string
37
     */
38
    protected $propertyName;
39
40
    /**
41
     * @readwrite
42
     * @var EntityDescriptorInterface
43
     */
44
    protected $entityDescriptor;
45
46
    /**
47
     * Parent or related entity class name
48
     * @readwrite
49
     * @var string
50
     */
51
    protected $parentEntity;
52
53
    /**
54
     * @readwrite
55
     * @var EntityDescriptorInterface
56
     */
57
    protected $parentEntityDescriptor;
58
59
    /**
60
     * @readwrite
61
     * @var string
62
     */
63
    protected $foreignKey;
64
65
    /**
66
     * @readwrite
67
     * @var AdapterInterface
68
     */
69
    protected $adapter;
70
71
    /**
72
     * @readwrite
73
     * @var bool
74
     */
75
    protected $lazyLoaded = true;
76
77
    /**
78
     * Returns the property holding the relation
79
     *
80
     * @return string
81
     */
82 6
    public function getPropertyName()
83
    {
84 6
        return $this->propertyName;
85
    }
86
87
88
    /**
89
     * Gets the entity descriptor
90
     *
91
     * @return EntityDescriptorInterface
92
     */
93 22
    public function getEntityDescriptor()
94
    {
95 22
        return $this->entityDescriptor;
96
    }
97
98
    /**
99
     * Sets entity descriptor
100
     *
101
     * @param EntityDescriptorInterface $descriptor
102
     * @return $this
103
     */
104 56
    public function setEntityDescriptor(EntityDescriptorInterface $descriptor)
105
    {
106 56
        $this->entityDescriptor = $descriptor;
107 56
        return $this;
108
    }
109
110
    /**
111
     * Gets parent entity class name
112
     *
113
     * @return string
114
     */
115 10
    public function getParentEntity()
116
    {
117 10
        return $this->parentEntity;
118
    }
119
120
    /**
121
     * Gets the parent or related entity descriptor
122
     *
123
     * @return EntityDescriptorInterface
124
     */
125 32
    public function getParentEntityDescriptor()
126
    {
127 32
        if (is_null($this->parentEntityDescriptor)) {
128 26
            $this->setParentEntityDescriptor(
129 26
                EntityDescriptorRegistry::getInstance()
130 26
                    ->getDescriptorFor($this->parentEntity)
131 26
            );
132 26
        }
133 32
        return $this->parentEntityDescriptor;
134
    }
135
136
    /**
137
     * Sets parent entity descriptor
138
     *
139
     * @param EntityDescriptorInterface $parentEntityDescriptor
140
     * @return BelongsTo
141
     */
142 26
    public function setParentEntityDescriptor(
143
        EntityDescriptorInterface $parentEntityDescriptor
144
    ) {
145 26
        $this->parentEntityDescriptor = $parentEntityDescriptor;
146 26
        return $this;
147
    }
148
149
    /**
150
     * Gets the foreign key field name
151
     *
152
     * @return string
153
     */
154 8
    public function getForeignKey()
155
    {
156 8
        if (is_null($this->foreignKey)) {
157 8
            $name = $this->getParentEntityDescriptor()->getTableName();
158 8
            $this->foreignKey = "{$this->normalizeFieldName($name)}_id";
159 8
        }
160 8
        return $this->foreignKey;
161
    }
162
163
    /**
164
     * Normalizes the key field by convention
165
     * 
166
     * @param string $tableName
167
     * @return string
168
     */
169 28
    protected function normalizeFieldName($tableName)
170
    {
171 28
        $tableName = Text::camelCaseToSeparator($tableName, '#');
172 28
        $parts = explode('#', $tableName);
173 28
        $lastName = array_pop($parts);
174 28
        $lastName = Text::singular(strtolower($lastName));
175 28
        array_push($parts, ucfirst($lastName));
176 28
        return lcfirst(implode('', $parts));
177
    }
178
179
    /**
180
     * Register the retrieved entities in the repository identity map
181
     *
182
     * @param EntityInterface|EntityCollection $entity
183
     *
184
     * @return EntityInterface|EntityCollection
185
     */
186 6
    protected function registerEntity($entity)
187
    {
188 6
        Orm::getRepository($this->getParentEntity())
189 6
            ->getIdentityMap()
190 6
            ->set($entity);
0 ignored issues
show
Bug introduced by
It seems like $entity defined by parameter $entity on line 186 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...
191
192 6
        return $entity;
193
    }
194
195
    /**
196
     * Set the database adapter for this relation
197
     *
198
     * @param AdapterInterface $adapter
199
     * @return $this|self|AbstractRelation
200
     */
201 8
    public function setAdapter(AdapterInterface $adapter)
202
    {
203 8
        $this->adapter = $adapter;
204 8
        return $this;
205
    }
206
207
    /**
208
     * Gets relation adapter
209
     *
210
     * @return AdapterInterface
211
     */
212 8
    public function getAdapter()
213
    {
214 8
        if (null == $this->adapter) {
215 2
            $className = $this->getEntityDescriptor()->className();
216 2
            $repository = Orm::getRepository($className);
217 2
            $this->setAdapter($repository->getAdapter());
218 2
        }
219 8
        return $this->adapter;
220
    }
221
}