Completed
Push — master ( 7113c4...8875d3 )
by Yassine
14s
created

src/BatchResponse.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Copyright 2017 Facebook, Inc.
4
 *
5
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
6
 * use, copy, modify, and distribute this software in source code or binary
7
 * form for use in connection with the web services and APIs provided by
8
 * Facebook.
9
 *
10
 * As with any software that integrates with the Facebook platform, your use
11
 * of this software is subject to the Facebook Developer Principles and
12
 * Policies [http://developers.facebook.com/policy/]. This copyright notice
13
 * shall be included in all copies or substantial portions of the software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * DEALINGS IN THE SOFTWARE.
22
 */
23
namespace Facebook;
24
25
use ArrayIterator;
26
use IteratorAggregate;
27
use ArrayAccess;
28
29
/**
30
 * @package Facebook
31
 */
32
class BatchResponse extends Response implements IteratorAggregate, ArrayAccess
33
{
34
    /**
35
     * @var BatchRequest the original entity that made the batch request
36
     */
37
    protected $batchRequest;
38
39
    /**
40
     * @var Response[] an array of Response entities
41
     */
42
    protected $responses = [];
43
44
    /**
45
     * Creates a new Response entity.
46
     *
47
     * @param BatchRequest $batchRequest
48
     * @param Response     $response
49
     */
50 5
    public function __construct(BatchRequest $batchRequest, Response $response)
51
    {
52 5
        $this->batchRequest = $batchRequest;
53
54 5
        $request = $response->getRequest();
55 5
        $body = $response->getBody();
56 5
        $httpStatusCode = $response->getHttpStatusCode();
57 5
        $headers = $response->getHeaders();
58 5
        parent::__construct($request, $body, $httpStatusCode, $headers);
59
60 5
        $responses = $response->getDecodedBody();
61 5
        $this->setResponses($responses);
62 5
    }
63
64
    /**
65
     * Returns an array of Response entities.
66
     *
67
     * @return Response[]
68
     */
69 1
    public function getResponses()
70
    {
71 1
        return $this->responses;
72
    }
73
74
    /**
75
     * The main batch response will be an array of requests so
76
     * we need to iterate over all the responses.
77
     *
78
     * @param array $responses
79
     */
80 5
    public function setResponses(array $responses)
81
    {
82 5
        $this->responses = [];
83
84 5
        foreach ($responses as $key => $graphResponse) {
85 5
            $this->addResponse($key, $graphResponse);
86
        }
87 5
    }
88
89
    /**
90
     * Add a response to the list.
91
     *
92
     * @param int        $key
93
     * @param null|array $response
94
     */
95 5
    public function addResponse($key, $response)
96
    {
97 5
        $originalRequestName = isset($this->batchRequest[$key]['name']) ? $this->batchRequest[$key]['name'] : $key;
98 5
        $originalRequest = isset($this->batchRequest[$key]['request']) ? $this->batchRequest[$key]['request'] : null;
99
100 5
        $httpResponseBody = isset($response['body']) ? $response['body'] : null;
101 5
        $httpResponseCode = isset($response['code']) ? $response['code'] : null;
102
        // @TODO With PHP 5.5 support, this becomes array_column($response['headers'], 'value', 'name')
103 5
        $httpResponseHeaders = isset($response['headers']) ? $this->normalizeBatchHeaders($response['headers']) : [];
104
105 5
        $this->responses[$originalRequestName] = new Response(
106 5
            $originalRequest,
0 ignored issues
show
It seems like $originalRequest can also be of type null; however, parameter $request of Facebook\Response::__construct() does only seem to accept Facebook\Request, 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

106
            /** @scrutinizer ignore-type */ $originalRequest,
Loading history...
107 5
            $httpResponseBody,
108 5
            $httpResponseCode,
109 5
            $httpResponseHeaders
110
        );
111 5
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 1
    public function getIterator()
117
    {
118 1
        return new ArrayIterator($this->responses);
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function offsetSet($offset, $value)
125
    {
126
        $this->addResponse($offset, $value);
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function offsetExists($offset)
133
    {
134
        return isset($this->responses[$offset]);
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function offsetUnset($offset)
141
    {
142
        unset($this->responses[$offset]);
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 3
    public function offsetGet($offset)
149
    {
150 3
        return isset($this->responses[$offset]) ? $this->responses[$offset] : null;
151
    }
152
153
    /**
154
     * Converts the batch header array into a standard format.
155
     *
156
     * @TODO replace with array_column() when PHP 5.5 is supported.
157
     *
158
     * @param array $batchHeaders
159
     *
160
     * @return array
161
     */
162 4
    private function normalizeBatchHeaders(array $batchHeaders)
163
    {
164 4
        $headers = [];
165
166 4
        foreach ($batchHeaders as $header) {
167 2
            $headers[$header['name']] = $header['value'];
168
        }
169
170 4
        return $headers;
171
    }
172
}
173