GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Iterator::__construct()   B
last analyzed

Complexity

Conditions 6
Paths 16

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 15
cts 15
cp 1
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 11
nc 16
nop 3
crap 6
1
<?php declare(strict_types=1);
2
3
namespace OpenStack\Common\Resource;
4
5
use OpenStack\Common\Transport\Utils;
6
7
class Iterator
8
{
9
    private $requestFn;
10
    private $resourceFn;
11
    private $limit;
12
    private $count;
13
    private $resourcesKey;
14
    private $markerKey;
15
    private $mapFn;
16
    private $currentMarker;
17
18 33
    public function __construct(array $options, callable $requestFn, callable $resourceFn)
19
    {
20 33
        $this->limit = isset($options['limit']) ? $options['limit'] : false;
21 33
        $this->count = 0;
22
23 33
        if (isset($options['resourcesKey'])) {
24 33
            $this->resourcesKey = $options['resourcesKey'];
25 33
        }
26
27 33
        if (isset($options['markerKey'])) {
28 8
            $this->markerKey = $options['markerKey'];
29 8
        }
30
31 33
        if (isset($options['mapFn']) && is_callable($options['mapFn'])) {
32 1
            $this->mapFn = $options['mapFn'];
33 1
        }
34
35 33
        $this->requestFn  = $requestFn;
36 33
        $this->resourceFn = $resourceFn;
37 33
    }
38
39 33
    private function fetchResources()
40
    {
41 33
        if ($this->shouldNotSendAnotherRequest()) {
42 29
            return false;
43
        }
44
45 33
        $response = call_user_func($this->requestFn, $this->currentMarker);
46
47 33
        $json = Utils::flattenJson(Utils::jsonDecode($response), $this->resourcesKey);
48
49 33
        if ($response->getStatusCode() === 204 || empty($json)) {
50 4
            return false;
51
        }
52
53 33
        return $json;
54
    }
55
56 33
    private function assembleResource(array $data)
57
    {
58 33
        $resource = call_user_func($this->resourceFn, $data);
59
60
        // Invoke user-provided fn if provided
61 33
        if ($this->mapFn) {
62 1
            call_user_func_array($this->mapFn, [&$resource]);
63 1
        }
64
65
        // Update marker if operation supports it
66 33
        if ($this->markerKey) {
67 8
            $this->currentMarker = $resource->{$this->markerKey};
68 8
        }
69
70 33
        return $resource;
71
    }
72
73 33
    private function totalReached()
74
    {
75 33
        return $this->limit && $this->count >= $this->limit;
76
    }
77
78 33
    private function shouldNotSendAnotherRequest()
79
    {
80 33
        return $this->totalReached() || ($this->count > 0 && !$this->markerKey);
81
    }
82
83 33
    public function __invoke()
84
    {
85 33
        while ($resources = $this->fetchResources()) {
86 33
            foreach ($resources as $resourceData) {
87 33
                if ($this->totalReached()) {
88 2
                    break;
89
                }
90
91 33
                $this->count++;
92
93 33
                yield $this->assembleResource($resourceData);
94 33
            }
95 33
        }
96 33
    }
97
}
98