LinkCollection::current()   A
last analyzed

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 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
namespace Aeq\Hal\Explorer;
3
4
use Aeq\Hal\Exception\AlreadyEmbeddedException;
5
use Aeq\Hal\Explorer;
6
7
class LinkCollection implements \Iterator, LinkInterface, EmbeddableInterface
8
{
9
    /**
10
     * @var Explorer
11
     */
12
    private $explorer;
13
14
    /**
15
     * @var string
16
     */
17
    private $name;
18
19
    /**
20
     * @var Link[]
21
     */
22
    private $links = [];
23
24
    /**
25
     * @var EmbeddableInterface
26
     */
27
    private $parent;
28
29
    /**
30
     * @var ResourceCollection
31
     */
32
    private $resources;
33
34
    /**
35
     * @param Explorer $explorer
36
     * @param string $name
37
     * @param EmbeddableInterface $parent
38
     */
39 1
    public function __construct(Explorer $explorer, $name, EmbeddableInterface $parent)
40
    {
41 1
        $this->explorer = $explorer;
42 1
        $this->name = $name;
43 1
        $this->parent = $parent;
44 1
        $this->resources = new ResourceCollection($explorer);
45 1
    }
46
47
    /**
48
     * @param Link $link
49
     */
50 1
    public function addLink(Link $link)
51
    {
52 1
        $this->links[] = $link;
53 1
    }
54
55
    /**
56
     * @return Link
57
     */
58
    public function current()
59
    {
60
        return current($this->links);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression current($this->links); of type Aeq\Hal\Explorer\Link|false adds false to the return on line 60 which is incompatible with the return type documented by Aeq\Hal\Explorer\LinkCollection::current of type Aeq\Hal\Explorer\Link. It seems like you forgot to handle an error condition.
Loading history...
61
    }
62
63
    /**
64
     * @return void
65
     */
66
    public function next()
67
    {
68
        next($this->links);
69
    }
70
71
    /**
72
     * @return mixed
73
     */
74
    public function key()
75
    {
76
        return key($this->links);
77
    }
78
79
    /**
80
     * @return bool
81
     */
82
    public function valid()
83
    {
84
        return key($this->links) !== null;
85
    }
86
87
    /**
88
     * @return void
89
     */
90
    public function rewind()
91
    {
92
        reset($this->links);
93
    }
94
95
    /**
96
     * @return array
97
     */
98 2
    public function getData()
99
    {
100 2
        $data = [];
101 2
        foreach ($this->links as $link) {
102 2
            $data[] = $link->getData();
103 2
        }
104 2
        return $data;
105
    }
106
107
    /**
108
     * @param string $name
109
     * @param \Aeq\Hal\Explorer\ResourceInterface $resource
110
     */
111 1
    public function addEmbedded($name, ResourceInterface $resource)
112
    {
113 1
        $this->resources->addResource($resource);
114 1
    }
115
116
    /**
117
     * @param string $name
118
     * @return bool
119
     */
120 1
    public function hasEmbedded($name)
121
    {
122 1
        return $this->parent->hasEmbedded($name);
123
    }
124
125
    /**
126
     * @param array $variables
127
     * @param array $options
128
     * @return ResourceCollection
129
     * @throws AlreadyEmbeddedException
130
     */
131 1
    public function follow(array $variables = [], array $options = [])
132
    {
133 1
        foreach ($this->links as $link) {
134 1
            $link->follow($variables, $options);
135 1
        }
136
137 1
        $this->parent->addEmbedded($this->name, $this->resources);
138
139 1
        return $this->resources;
140
    }
141
}
142