Passed
Push — main ( dd7998...bae415 )
by Dylan
03:01
created

ListResource::getIterator()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 16
rs 9.9332
cc 3
nc 1
nop 0
1
<?php
2
3
namespace Lifeboat\Resource;
4
5
use Lifeboat\Connector;
6
use Lifeboat\Exceptions\OAuthException;
7
use Lifeboat\Services\ObjectFactory;
8
use IteratorAggregate;
9
use Generator;
10
11
/**
12
 * Class ResourceList
13
 * @package Lifeboat\Resource
14
 *
15
 * @property string $_url
16
 * @property array $_items
17
 */
18
class ListResource extends ApiResource implements IteratorAggregate {
19
20
    const PAGE_LENGTH   = 20;
21
    const PAGE_PARAM    = 'page';
22
    const LIMIT_PARAM   = 'limit';
23
24
    private $_url           = '';
25
    private $_items         = [];
26
    private $_max_items     = 0;
27
28
    public function __construct(Connector $connector, string $url)
29
    {
30
        $this->setClient($connector);
31
        $this->setURL($url);
32
    }
33
34
    /**
35
     * @param string $url
36
     * @return $this
37
     */
38
    public function setURL(string $url): ListResource
39
    {
40
        $this->_url = $url;
41
        return $this;
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function getURL(): string
48
    {
49
        return $this->_url;
50
    }
51
52
    /**
53
     * @param int $page
54
     * @return array
55
     * @throws OAuthException
56
     */
57
    public function getItems(int $page = 1): array
58
    {
59
        if (!array_key_exists($page, $this->_items)) {
60
            $response = $this->getClient()->curl_api($this->getURL(), 'GET', [
61
                self::PAGE_PARAM    => $page,
62
                self::LIMIT_PARAM   => self::PAGE_LENGTH
63
            ]);
64
65
            $data = ($response->isValid() && $response->isJSON()) ? $response->getJSON() : [];
66
            $this->_max_items = $data['available_items'];
67
68
            foreach ($data['items'] as $item) {
69
                $obj = ObjectFactory::make($this->getClient(), $item);
70
                if (!$obj) continue;
71
72
                $this->_items[$page][] = $obj;
73
            }
74
        }
75
76
        return $this->_items[$page];
77
    }
78
79
    /**
80
     * @param mixed $offset
81
     * @return ObjectResource|null
82
     * @throws OAuthException
83
     */
84
    public function offsetGet($offset): ?ObjectResource
85
    {
86
        return $this->getItems()[$offset] ?? null;
87
    }
88
89
    /**
90
     * @param mixed $offset
91
     * @param mixed $value
92
     * @throws OAuthException
93
     */
94
    public function offsetSet($offset, $value)
95
    {
96
        $this->getItems()[$offset] = $value;
97
    }
98
99
    /**
100
     * @return int
101
     */
102
    public function count(): int
103
    {
104
        return $this->_max_items;
105
    }
106
107
    /**
108
     * @param mixed $offset
109
     * @return bool
110
     * @throws OAuthException
111
     */
112
    public function offsetExists($offset): bool
113
    {
114
        return array_key_exists($offset, $this->getItems());
115
    }
116
117
    /**
118
     * @param mixed $offset
119
     * @throws OAuthException
120
     */
121
    public function offsetUnset($offset)
122
    {
123
        unset($this->getItems()[$offset]);
124
    }
125
126
    /**
127
     * @return Generator
128
     */
129
    public function getIterator(): Generator
130
    {
131
        return (function () {
132
            $i = 0;
133
            $x = 0;
134
            $m = $this->count();
135
136
            while ($i < $m) {
137
                if ($x === 0) {
138
                    $x = self::PAGE_LENGTH;
139
                    $t = $this->getItems(ceil($i / self::PAGE_LENGTH));
0 ignored issues
show
Bug introduced by
ceil($i / self::PAGE_LENGTH) of type double is incompatible with the type integer expected by parameter $page of Lifeboat\Resource\ListResource::getItems(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

139
                    $t = $this->getItems(/** @scrutinizer ignore-type */ ceil($i / self::PAGE_LENGTH));
Loading history...
140
                }
141
142
                yield $i => $t[self::PAGE_LENGTH - $x];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $t does not seem to be defined for all execution paths leading up to this point.
Loading history...
143
144
                $x -= 1;
145
            }
146
        })();
147
    }
148
}
149