Completed
Push — master ( f1d84d...653498 )
by Derek Stephen
03:51
created

User::findKey()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Del\Collection;
4
5
use Del\Entity\User as UserEntity;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use LogicException;
8
9
class User extends ArrayCollection
10
{
11
    /**
12
     * @param UserEntity $user
13
     * @return $this
14
     */
15
    public function update(UserEntity $user)
16
    {
17
        $key = $this->findKey($user);
18
        if($key) {
19
20
            $this->offsetSet($key,$user);
21
            return $this;
22
23
        }
24
        throw new LogicException('User was not in the collection.');
25
    }
26
27
    /**
28
     * @param UserEntity $user
29
     */
30
    public function append(UserEntity $user)
31
    {
32
        $this->add($user);
33
    }
34
35
    /**
36
     * @return UserEntity|null
37
     */
38
    public function current()
39
    {
40
        return parent::current();
41
    }
42
43
    /**
44
     * @param UserEntity $user
45
     * @return bool|int
46
     */
47 View Code Duplication
    public function findKey(UserEntity $user)
48
    {
49
        $it = $this->getIterator();
50
        $it->rewind();
51
        while($it->valid()) {
52
            if($it->current()->getId() == $user->getId()) {
53
                return $it->key();
54
            }
55
            $it->next();
56
        }
57
        return false;
58
    }
59
60
61
62 View Code Duplication
    public function findById($id)
63
    {
64
        $it = $this->getIterator();
65
        $it->rewind();
66
        while($it->valid()) {
67
            if($it->current()->getId() == $id) {
68
                return $it->current();
69
            }
70
            $it->next();
71
        }
72
73
        return false;
74
    }
75
76
}