Completed
Push — master ( 51a9f1...e46252 )
by Filipe
02:26
created

AbstractRelation::getEntityDescriptor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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 14
    public function getEntityDescriptor()
94
    {
95 14
        return $this->entityDescriptor;
96
    }
97
98
    /**
99
     * Sets entity descriptor
100
     *
101
     * @param EntityDescriptorInterface $descriptor
102
     * @return $this
103
     */
104 48
    public function setEntityDescriptor(EntityDescriptorInterface $descriptor)
105
    {
106 48
        $this->entityDescriptor = $descriptor;
107 48
        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 18
    public function getParentEntityDescriptor()
126
    {
127 18
        if (is_null($this->parentEntityDescriptor)) {
128 18
            $this->setParentEntityDescriptor(
129 18
                EntityDescriptorRegistry::getInstance()
130 18
                    ->getDescriptorFor($this->parentEntity)
131 9
            );
132 9
        }
133 18
        return $this->parentEntityDescriptor;
134
    }
135
136
    /**
137
     * Sets parent entity descriptor
138
     *
139
     * @param EntityDescriptorInterface $parentEntityDescriptor
140
     * @return BelongsTo
141
     */
142 18
    public function setParentEntityDescriptor(
143
        EntityDescriptorInterface $parentEntityDescriptor
144
    ) {
145 18
        $this->parentEntityDescriptor = $parentEntityDescriptor;
146 18
        return $this;
147
    }
148
149
    /**
150
     * Gets the foreign key field name
151
     *
152
     * @return string
153
     */
154 8 View Code Duplication
    public function getForeignKey()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
    {
156 8
        if (is_null($this->foreignKey)) {
157 8
            $name = $this->getParentEntityDescriptor()->getTableName();
158 8
            $name = Text::singular(strtolower($name));
159 8
            $this->foreignKey = "{$name}_id";
160 4
        }
161 8
        return $this->foreignKey;
162
    }
163
164
    /**
165
     * Register the retrieved entities in the repository identity map
166
     *
167
     * @param EntityInterface|EntityCollection $entity
168
     *
169
     * @return EntityInterface|EntityCollection
170
     */
171 6
    protected function registerEntity($entity)
172
    {
173 6
        Orm::getRepository($this->getParentEntity())
174 6
            ->getIdentityMap()
175 6
            ->set($entity);
0 ignored issues
show
Bug introduced by
It seems like $entity defined by parameter $entity on line 171 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...
176
177 6
        return $entity;
178
    }
179
180
    /**
181
     * Set the database adapter for this relation
182
     *
183
     * @param AdapterInterface $adapter
184
     * @return $this|self|AbstractRelation
185
     */
186 8
    public function setAdapter(AdapterInterface $adapter)
187
    {
188 8
        $this->adapter = $adapter;
189 8
        return $this;
190
    }
191
192
    /**
193
     * Gets relation adapter
194
     *
195
     * @return AdapterInterface
196
     */
197 8
    public function getAdapter()
198
    {
199 8
        if (null == $this->adapter) {
200 2
            $className = $this->getEntityDescriptor()->className();
201 2
            $repository = Orm::getRepository($className);
202 2
            $this->setAdapter($repository->getAdapter());
203 1
        }
204 8
        return $this->adapter;
205
    }
206
}