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   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 1
dl 0
loc 91
ccs 46
cts 46
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 20 6
A fetchResources() 0 16 4
A assembleResource() 0 16 3
A totalReached() 0 4 2
A shouldNotSendAnotherRequest() 0 4 3
A __invoke() 0 14 4
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