Notification Setup Error

We have detected an error in your notification set-up (Event-ID dab39dc24f564ec7bd4628d1305fd03c). Currently, we cannot inform you about inspection progress. Please check that the user 557058:bca11929-8c2d-43f2-8a82-c5416880d395 still has access to your repository or update the API account.

Pager::getContent()   A
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 0
cp 0
rs 9.0777
c 0
b 0
f 0
cc 6
nc 3
nop 0
crap 42
1
<?php
2
/**
3
 * This file is part of the bitbucket-api package.
4
 *
5
 * (c) Alexandru G. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Bitbucket\API\Http\Response;
11
12
use Bitbucket\API\Http\HttpPluginClientBuilder;
13
use Http\Discovery\MessageFactoryDiscovery;
14
use Http\Message\MessageFactory;
15
use Psr\Http\Message\ResponseInterface;
16
17
/**
18
 * @author Alexandru Guzinschi <[email protected]>
19
 */
20
class Pager implements PagerInterface
21
{
22
    /** @var HttpPluginClientBuilder */
23
    private $httpPluginClientBuilder;
24
    /** @var MessageFactory */
25
    private $messageFactory;
26
    /** @var ResponseInterface */
27
    private $response;
28
29
    /**
30
     * @param HttpPluginClientBuilder $httpPluginClientBuilder
31
     * @param ResponseInterface $response
32
     * @param MessageFactory $messageFactory
33 10
     *
34
     * @throws \UnexpectedValueException
35
     */
36 10
    public function __construct(
37 2
        HttpPluginClientBuilder $httpPluginClientBuilder,
38
        ResponseInterface $response,
39
        MessageFactory $messageFactory = null
40 8
    ) {
41 8
        /** @var ResponseInterface $response */
42 8
        if ($response->getStatusCode() >= 400) {
43
            throw new \UnexpectedValueException("Can't paginate an unsuccessful response.");
44
        }
45
46
        $this->httpPluginClientBuilder = $httpPluginClientBuilder;
47 3
        $this->response = $response;
48
        $this->messageFactory = $messageFactory ? : MessageFactoryDiscovery::find();
49 3
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function hasNext()
55 2
    {
56
        return array_key_exists('next', $this->getContent());
57 2
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    public function hasPrevious()
63 3
    {
64
        return array_key_exists('previous', $this->getContent());
65 3
    }
66 2
67 2
    /**
68
     * {@inheritDoc}
69
     */
70 2
    public function fetchNext()
71
    {
72
        if ($this->hasNext()) {
73
            $content = $this->getContent();
74
75
            return $this->response = $this->httpPluginClientBuilder->getHttpClient()->get($content['next']);
76 2
        }
77
78 2
        return null;
79 1
    }
80 1
81
    /**
82
     * {@inheritDoc}
83 1
     */
84
    public function fetchPrevious()
85
    {
86
        if ($this->hasPrevious()) {
87
            $content = $this->getContent();
88
89 3
            return $this->response = $this->httpPluginClientBuilder->getHttpClient()->get($content['previous']);
90
        }
91 3
92 3
        return null;
93
    }
94
95 3
    /**
96 3
     * {@inheritDoc}
97 2
     */
98
    public function fetchAll()
99
    {
100 1
        $content = $this->getContent();
101
        $values = [];
102 1
103 1
        // merge all `values` and replace it inside the most recent response.
104 1
        while (true) {
105
            if (!array_key_exists('values', $content)) {
106
                break;
107 1
            }
108
109
            $values = (0 === count($values)) ? $content['values'] : array_merge($values, $content['values']);
110 3
111 3
            if (null !== $this->fetchNext()) {
112
                $content = $this->getContent();
113 3
                continue;
114
            }
115
116
            break;
117
        }
118
119 1
        $content['values'] = $values;
120
        $this->response = $this->messageFactory->createResponse(
121 1
            $this->response->getStatusCode(),
122
            $this->response->getReasonPhrase(),
123
            $this->response->getHeaders(),
124
            json_encode($content),
125
            $this->response->getProtocolVersion()
126
        );
127
128 7
        return $this->response;
129
    }
130 7
131
    /**
132 7
     * {@inheritDoc}
133
     */
134 6
    public function getCurrent()
135 6
    {
136 6
        return $this->response;
137
    }
138
139 6
    /**
140
     * @access private
141
     * @return array
142 1
     */
143
    private function getContent()
144
    {
145
        $content = json_decode($this->response->getBody()->getContents(), true);
146
        $this->response->getBody()->rewind();
147
148
        if (is_array($content) && JSON_ERROR_NONE === json_last_error()) {
149
            // replace reference inserted by `LegacyCollectionListener` with actual data.
150
            if (array_key_exists('values', $content) &&
151
                is_string($content['values']) &&
152
                strpos($content['values'], '.') !== false) {
153
                $content['values'] = $content[str_replace('.', '', $content['values'])];
154
            }
155
            return $content;
156
        }
157
158
        return [];
159
    }
160
}
161