Pager::key()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Rs\VersionEye\Http;
4
5
/**
6
 * ResultPager.
7
 *
8
 * @author Robert Schönthal <[email protected]>
9
 */
10
class Pager implements \Iterator
11
{
12
    private $offset  = 0;
13
    private $current = 1;
14
    private $max     = 0;
15
    private $result  = [];
16
17
    /**
18
     * @var HttpClient
19
     */
20
    private $client;
21
    private $key;
22
    private $method;
23
    private $url;
24
    private $params = [];
25
26
    /**
27
     * @param array      $result
28
     * @param string     $key
29
     * @param HttpClient $client
30
     * @param string     $method
31
     * @param string     $url
32
     * @param array      $params
33
     */
34 3
    public function __construct(array $result, $key, HttpClient $client, $method, $url, array $params = [])
35
    {
36 3
        $this->current = $result['paging']['current_page'];
37 3
        $this->max     = $result['paging']['total_entries'];
38 3
        $this->result  = $result[$key];
39
40 3
        $this->key    = $key;
41 3
        $this->client = $client;
42 3
        $this->method = $method;
43 3
        $this->url    = $url;
44 3
        $this->params = $params;
45 3
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 1
    public function current()
51
    {
52 1
        return $this->result[$this->offset];
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 1
    public function next()
59
    {
60 1
        ++$this->offset;
61 1
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 1
    public function key()
67
    {
68 1
        return $this->offset;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 1
    public function valid()
75
    {
76 1
        if (!isset($this->result[$this->offset]) && $this->offset < $this->max) {
77 1
            ++$this->current;
78 1
            $url    = preg_replace('/page=[0-9]+/', 'page=' . $this->current, $this->url);
79 1
            $result = $this->client->request($this->method, $url, $this->params);
80
81 1
            $this->result = array_merge($this->result, $result[$this->key]);
82
83 1
            return true;
84
        }
85
86 1
        return $this->offset < $this->max;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function rewind()
93
    {
94
    }
95
}
96