Completed
Push — feature/middleware ( e85030...d864e0 )
by Derek Stephen
05:41
created

OrcCollection   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 1
dl 0
loc 101
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 9 2
A append() 0 4 1
A current() 0 4 1
A findKey() 0 12 3
A findById() 0 12 3
A toArray() 0 14 2
A jsonSerialize() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BoneMvc\Module\Orc\Collection;
6
7
use BoneMvc\Module\Orc\Entity\Orc;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use JsonSerializable;
10
use LogicException;
11
12
class OrcCollection extends ArrayCollection implements JsonSerializable
13
{
14
    /**
15
     * @param Orc $orc
16
     * @return $this
17
     * @throws LogicException
18
     */
19
    public function update(Orc $orc): OrcCollection
20
    {
21
        $key = $this->findKey($orc);
22
        if($key) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $key of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
23
            $this->offsetSet($key,$orc);
24
            return $this;
25
        }
26
        throw new LogicException('Orc was not in the collection.');
27
    }
28
29
    /**
30
     * @param Orc $orc
31
     */
32
    public function append(Orc $orc): void
33
    {
34
        $this->add($orc);
35
    }
36
37
    /**
38
     * @return Orc|null
39
     */
40
    public function current(): ?Orc
41
    {
42
        return parent::current();
43
    }
44
45
    /**
46
     * @param Orc $orc
47
     * @return int|null
48
     */
49
    public function findKey(Orc $orc): ?int
50
    {
51
        $it = $this->getIterator();
52
        $it->rewind();
53
        while($it->valid()) {
54
            if($it->current()->getId() == $orc->getId()) {
55
                return $it->key();
56
            }
57
            $it->next();
58
        }
59
        return null;
60
    }
61
62
    /**
63
     * @param int $id
64
     * @return Orc|null
65
     */
66
    public function findById(int $id): ?Orc
67
    {
68
        $it = $this->getIterator();
69
        $it->rewind();
70
        while($it->valid()) {
71
            if($it->current()->getId() == $id) {
72
                return $it->current();
73
            }
74
            $it->next();
75
        }
76
        return null;
77
    }
78
79
    /**
80
     * @return array
81
     */
82
    public function toArray(): array
83
    {
84
        $collection = [];
85
        $it = $this->getIterator();
86
        $it->rewind();
87
        while($it->valid()) {
88
            /** @var Orc $row */
89
            $row = $it->current();
90
            $collection[] = $row->toArray();
91
            $it->next();
92
        }
93
94
        return $collection;
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function jsonSerialize(): string
101
    {
102
        return \json_encode($this->toArray());
103
    }
104
105
    /**
106
     * @return string
107
     */
108
    public function __toString(): string
109
    {
110
        return $this->jsonSerialize();
111
    }
112
}
113