InstagramBatchResponse::getResponses()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Maztech;
4
5
use ArrayIterator;
6
use IteratorAggregate;
7
use ArrayAccess;
8
9
/**
10
 * Class InstagramBatchResponse
11
 *
12
 * @package Instagram
13
 */
14
class InstagramBatchResponse extends InstagramResponse implements IteratorAggregate, ArrayAccess
15
{
16
    /**
17
     * @var InstagramBatchRequest The original entity that made the batch request.
18
     */
19
    protected $batchRequest;
20
21
    /**
22
     * @var array An array of InstagramResponse entities.
23
     */
24
    protected $responses = [];
25
26
    /**
27
     * Creates a new Response entity.
28
     *
29
     * @param InstagramBatchRequest $batchRequest
30
     * @param InstagramResponse     $response
31
     */
32
    public function __construct(InstagramBatchRequest $batchRequest, InstagramResponse $response)
33
    {
34
        $this->batchRequest = $batchRequest;
35
36
        $request = $response->getRequest();
37
        $body = $response->getBody();
38
        $httpStatusCode = $response->getHttpStatusCode();
39
        $headers = $response->getHeaders();
40
        parent::__construct($request, $body, $httpStatusCode, $headers);
41
42
        $responses = $response->getDecodedBody();
43
        $this->setResponses($responses);
44
    }
45
46
    /**
47
     * Returns an array of InstagramResponse entities.
48
     *
49
     * @return array
50
     */
51
    public function getResponses()
52
    {
53
        return $this->responses;
54
    }
55
56
    /**
57
     * The main batch response will be an array of requests so
58
     * we need to iterate over all the responses.
59
     *
60
     * @param array $responses
61
     */
62
    public function setResponses(array $responses)
63
    {
64
        $this->responses = [];
65
66
        foreach ($responses as $key => $graphResponse) {
67
            $this->addResponse($key, $graphResponse);
68
        }
69
    }
70
71
    /**
72
     * Add a response to the list.
73
     *
74
     * @param int        $key
75
     * @param array|null $response
76
     */
77
    public function addResponse($key, $response)
78
    {
79
        $originalRequestName = isset($this->batchRequest[$key]['name']) ? $this->batchRequest[$key]['name'] : $key;
80
        $originalRequest = isset($this->batchRequest[$key]['request']) ? $this->batchRequest[$key]['request'] : null;
81
82
        $httpResponseBody = isset($response['body']) ? $response['body'] : null;
83
        $httpResponseCode = isset($response['code']) ? $response['code'] : null;
84
        // @TODO With PHP 5.5 support, this becomes array_column($response['headers'], 'value', 'name')
85
        $httpResponseHeaders = isset($response['headers']) ? $this->normalizeBatchHeaders($response['headers']) : [];
86
87
        $this->responses[$originalRequestName] = new InstagramResponse(
88
            $originalRequest,
0 ignored issues
show
Bug introduced by
It seems like $originalRequest can also be of type null; however, parameter $request of Maztech\InstagramResponse::__construct() does only seem to accept Maztech\InstagramRequest, maybe add an additional type check? ( Ignorable by Annotation )

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

88
            /** @scrutinizer ignore-type */ $originalRequest,
Loading history...
89
            $httpResponseBody,
90
            $httpResponseCode,
91
            $httpResponseHeaders
92
        );
93
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98
    public function getIterator()
99
    {
100
        return new ArrayIterator($this->responses);
101
    }
102
103
    /**
104
     * @inheritdoc
105
     */
106
    public function offsetSet($offset, $value)
107
    {
108
        $this->addResponse($offset, $value);
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114
    public function offsetExists($offset)
115
    {
116
        return isset($this->responses[$offset]);
117
    }
118
119
    /**
120
     * @inheritdoc
121
     */
122
    public function offsetUnset($offset)
123
    {
124
        unset($this->responses[$offset]);
125
    }
126
127
    /**
128
     * @inheritdoc
129
     */
130
    public function offsetGet($offset)
131
    {
132
        return isset($this->responses[$offset]) ? $this->responses[$offset] : null;
133
    }
134
135
    /**
136
     * Converts the batch header array into a standard format.
137
     * @TODO replace with array_column() when PHP 5.5 is supported.
138
     *
139
     * @param array $batchHeaders
140
     *
141
     * @return array
142
     */
143
    private function normalizeBatchHeaders(array $batchHeaders)
144
    {
145
        $headers = [];
146
147
        foreach ($batchHeaders as $header) {
148
            $headers[$header['name']] = $header['value'];
149
        }
150
151
        return $headers;
152
    }
153
}
154