Completed
Push — master ( b416d7...51a9f1 )
by Filipe
02:48
created

EntityCollection::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
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\Entity;
11
12
use League\Event\EmitterAwareTrait;
13
use Slick\Common\Utils\Collection\AbstractList;
14
use Slick\Orm\EntityInterface;
15
use Slick\Orm\Event\EntityAdded;
16
use Slick\Orm\Event\EntityRemoved;
17
use Slick\Orm\Exception\EntityNotFoundException;
18
use Slick\Orm\Exception\InvalidArgumentException;
19
use Slick\Orm\Orm;
20
use Slick\Orm\RepositoryInterface;
21
22
/**
23
 * Entity Collection
24
 *
25
 * @package Slick\Orm\Entity
26
 * @author  Filipe Silva <[email protected]>
27
 */
28
class EntityCollection extends AbstractList implements
29
    EntityCollectionInterface
30
{
31
    /**
32
     * @var string
33
     */
34
    protected $type;
35
36
    /**
37
     * @var RepositoryInterface
38
     */
39
    protected $repository;
40
41
    /**
42
     * @var EntityInterface|null
43
     */
44
    protected $parentEntity;
45
46
    /**
47
     * @var string
48
     */
49
    protected $cid;
50
51
    /**
52
     * Emitter methods
53
     */
54
    use EmitterAwareTrait;
55
56
    /**
57
     * Creates the collection for entity type with provided data
58
     *
59
     * @param string $entityType
60
     * @param array|\Traversable $data
61
     */
62 34
    public function __construct($entityType, $data = [])
63
    {
64 34
        parent::__construct($data);
65 34
        $this->type = $entityType;
66 34
    }
67
68
    /**
69
     * Offset to set
70
     *
71
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
72
     *
73
     * @param mixed $offset The offset to assign the value to.
74
     * @param mixed $value  The value to set.
75
     */
76 2
    public function offsetSet($offset, $value)
77
    {
78 2
        $this->add($value);
79 2
    }
80
81
    /**
82
     * Adds an entity to the collection
83
     *
84
     * This method adds an entity to the collection by accepting the entity
85
     * itself or the entity id value
86
     *
87
     * @param EntityInterface|integer $entity
88
     *
89
     * @throws EntityNotFoundException If the provided id is does not
90
     *  references an existing entity.
91
     * @throws InvalidArgumentException If the entity is not from
92
     *  the provided entity type
93
     *
94
     * @return self
95
     */
96 12
    public function add($entity)
97
    {
98 12
        if ($entity instanceof EntityInterface) {
99 8
            return $this->addEntity($entity);
100
        }
101 4
        return $this->addEntityWithId($entity);
102
    }
103
104
    /**
105
     * Adds an entity to the collection
106
     *
107
     * @param EntityInterface $entity
108
     * @throws InvalidArgumentException If the entity is not from
109
     *  the provided entity type
110
     *
111
     * @return self
112
     */
113 10
    public function addEntity(EntityInterface $entity)
114
    {
115 10
        if (!is_a($entity, $this->type)) {
116 2
            throw new InvalidArgumentException(
117 2
                "Trying to add an entity that is not a {$this->type}."
118 1
            );
119
        }
120 8
        $this->data[] = $entity;
121 8
        $event = new EntityAdded($entity, ['collection' => $this]);
122 8
        $this->getEmitter()->emit($event);
123 8
        return $this;
124
    }
125
126
    /**
127
     * Adds an entity by entity id
128
     *
129
     * @param mixed $entityId
130
     *
131
     * @throws EntityNotFoundException If the provided id is does not
132
     *  references an existing entity.
133
     * @throws InvalidArgumentException If the entity is not from
134
     *  the provided entity type
135
     *
136
     * @return self
137
     */
138 4
    public function addEntityWithId($entityId)
139
    {
140 4
        $entity = $this->getRepository()->get($entityId);
141 4
        if (!$entity) {
142 2
            throw new EntityNotFoundException(
143 2
                "The entity with is '{$entityId}' was not found."
144 1
            );
145
        }
146 2
        return $this->addEntity($entity);
147
        
148
    }
149
150
    /**
151
     * Gets entity repository for this collection
152
     * 
153
     * @return RepositoryInterface
154
     */
155 6
    public function getRepository()
156
    {
157 6
        if (null == $this->repository) {
158 2
            $this->setRepository(Orm::getRepository($this->type));
159 1
        }
160 6
        return $this->repository;
161
    }
162
163
    /**
164
     * Set repository for this entity collection
165
     * 
166
     * @param RepositoryInterface $repository
167
     * 
168
     * @return self|$this|EntityCollection
169
     */
170 6
    public function setRepository(RepositoryInterface $repository)
171
    {
172 6
        $this->repository = $repository;
173 6
        return $this;
174
    }
175
176
    /**
177
     * Get the parent entity if this collection is a relation in other entity
178
     *
179
     * @return null|EntityInterface
180
     */
181 2
    public function parentEntity()
182
    {
183 2
        return $this->parentEntity;
184
    }
185
186
    /**
187
     * Set the parent entity if this collection is a relation in other entity
188
     *
189
     * @param EntityInterface $entity
190
     *
191
     * @return self
192
     */
193 4
    public function setParentEntity(EntityInterface $entity)
194
    {
195 4
        $this->parentEntity = $entity;
196 4
        return $this;
197
    }
198
199
    /**
200
     * Gets the cache id used for this collection
201
     *
202
     * @return string
203
     */
204
    public function getId()
205
    {
206
        return $this->cid;
207
    }
208
209
    /**
210
     * Sets the cache id used for this collection
211
     *
212
     * @param string $collectionId
213
     *
214
     * @return self
215
     */
216 4
    public function setId($collectionId)
217
    {
218 4
        $this->cid = $collectionId;
219 4
        return $this;
220
    }
221
222
    /**
223
     * Removes the element at the given index, and returns it.
224
     *
225
     * @param integer|EntityInterface $index
226
     *
227
     * @return EntityInterface|null
228
     */
229
    public function remove($index)
230
    {
231
        if (!$index instanceof EntityInterface) {
232
            $index = $this->getRepository()->get($index);
233
        }
234
        
235
        return $this->findAndRemove($index);
236
    }
237
238
    /**
239
     * Iterates over the collection to remove an entity if found
240
     * 
241
     * @param EntityInterface|null $entity
242
     * @return EntityInterface
243
     */
244
    protected function findAndRemove(EntityInterface $entity = null)
245
    {
246
        if (null == $entity) {
247
            return $entity;
248
        }
249
250
        /**
251
         * @var int $key
252
         * @var EntityInterface $existent
253
         */
254
        foreach ($this->data as $key => $existent) {
255
            if ($existent->getId() == $entity->getId()) {
256
                parent::remove($key);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (remove() instead of findAndRemove()). Are you sure this is correct? If so, you might want to change this to $this->remove().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
257
                $this->getEmitter()
258
                    ->emit(
259
                        new EntityRemoved(
260
                            $entity,
261
                            ['collection' => $this]
262
                        )
263
                    );
264
                break;
265
            }
266
        }
267
        
268
        return $entity;
269
    }
270
}