Completed
Push — master ( 9cefff...efc7f4 )
by Filipe
07:40
created

AbstractRelation::getParentEntityDescriptor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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