Request::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
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
 */
24
25
namespace FacebookAds\Http;
26
27
class Request implements RequestInterface {
28
29
  /**
30
   * @var string
31
   */
32
  const PROTOCOL_HTTP = 'http://';
33
34
  /**
35
   * @var string
36
   */
37
  const PROTOCOL_HTTPS = 'https://';
38
39
  /**
40
   * @var Client
41
   */
42
  protected $client;
43
44
  /**
45
   * @var Headers
46
   */
47
  protected $headers;
48
49
  /**
50
   * @var string
51
   */
52
  protected $method = self::METHOD_GET;
53
54
  /**
55
   * @var string
56
   */
57
  protected $protocol = self::PROTOCOL_HTTPS;
58
59
  /**
60
   * @var string
61
   */
62
  protected $domain;
63
64
  /**
65
   * @var string
66
   */
67
  protected $path;
68
69
  /**
70
   * @var string
71
   */
72
  protected $graphVersion;
73
74
  /**
75
   * @var Parameters
76
   */
77
  protected $queryParams;
78
79
  /**
80
   * @var Parameters
81
   */
82
  protected $bodyParams;
83
84
  /**
85
   * @var Parameters
86
   */
87
  protected $fileParams;
88
89
  /**
90
   * @param Client $client
91
   */
92 14
  public function __construct(Client $client) {
93 14
    $this->client = $client;
94 14
  }
95
96 1
  public function __clone() {
97 1
    $this->queryParams && $this->queryParams = clone $this->queryParams;
98 1
    $this->bodyParams && $this->bodyParams = clone $this->bodyParams;
99 1
    $this->fileParams && $this->fileParams = clone $this->fileParams;
100 1
  }
101
102
  /**
103
   * @return Client
104
   */
105 3
  public function getClient() {
106 3
    return $this->client;
107
  }
108
109
  /**
110
   * @return string
111
   */
112 2
  public function getProtocol() {
113 2
    return $this->protocol;
114
  }
115
116
  /**
117
   * @param string $protocol
118
   */
119 1
  public function setProtocol($protocol) {
120 1
    $this->protocol = $protocol;
121 1
  }
122
123
  /**
124
   * @return string
125
   */
126 2
  public function getDomain() {
127 2
    if ($this->domain === null) {
128 2
      $this->domain = sprintf(
129 2
        "%s.%s",
130 2
        Client::DEFAULT_LAST_LEVEL_DOMAIN,
131 2
        $this->client->getDefaultGraphBaseDomain());
132 2
    }
133
134 2
    return $this->domain;
135
  }
136
137
  /**
138
   * @param string $domain
139
   */
140 1
  public function setDomain($domain) {
141 1
    $this->domain = $domain;
142 1
  }
143
144
  /**
145
   * @param string $last_level_domain
146
   */
147 1
  public function setLastLevelDomain($last_level_domain) {
148 1
    $this->domain = sprintf(
149 1
      "%s.%s",
150 1
      $last_level_domain,
151 1
      $this->client->getDefaultGraphBaseDomain());
152 1
  }
153
154
  /**
155
   * @return Headers
156
   */
157 1
  public function getHeaders() {
158 1
    if ($this->headers === null) {
159 1
      $this->headers = clone $this->getClient()->getDefaultRequestHeaderds();
160 1
    }
161
162 1
    return $this->headers;
163
  }
164
165
  /**
166
   * @param Headers $headers
167
   */
168 1
  public function setHeaders(Headers $headers) {
169 1
    $this->headers = $headers;
170 1
  }
171
172
  /**
173
   * @return string
174
   */
175 1
  public function getMethod() {
176 1
    return $this->method;
177
  }
178
179
  /**
180
   * @param string $method
181
   */
182 1
  public function setMethod($method) {
183 1
    $this->method = $method;
184 1
  }
185
186
  /**
187
   * @return string
188
   */
189 2
  public function getPath() {
190 2
    return $this->path;
191
  }
192
193
  /**
194
   * @param string $path
195
   */
196 2
  public function setPath($path) {
197 2
    $this->path = $path;
198 2
  }
199
200
  /**
201
   * @return string
202
   */
203 2
  public function getGraphVersion() {
204 2
    return $this->graphVersion;
205
  }
206
207
  /**
208
   * @param string $version
209
   */
210 2
  public function setGraphVersion($version) {
211 2
    $this->graphVersion = $version;
212 2
  }
213
214
  /**
215
   * @return Parameters
216
   */
217 3
  public function getQueryParams() {
218 3
    if ($this->queryParams === null) {
219 3
      $this->queryParams = new Parameters();
220 3
    }
221
222 3
    return $this->queryParams;
223
  }
224
225
  /**
226
   * @param Parameters $params
227
   */
228 2
  public function setQueryParams(Parameters $params) {
229 2
    $this->queryParams = $params;
230 2
  }
231
232
  /**
233
   * @return string
234
   */
235 1
  public function getUrl() {
236 1
    return $this->getProtocol().$this->getDomain()
237 1
      .'/v'.$this->getGraphVersion().$this->getPath()
238 1
      .($this->getQueryParams()->count() ? '?' : null)
239 1
      .http_build_query($this->getQueryParams()->export());
240
  }
241
242
  /**
243
   * @return Parameters
244
   */
245 2
  public function getBodyParams() {
246 2
    if ($this->bodyParams === null) {
247 1
      $this->bodyParams = new Parameters();
248 1
    }
249
250 2
    return $this->bodyParams;
251
  }
252
253
  /**
254
   * @param Parameters $params
255
   */
256 2
  public function setBodyParams(Parameters $params) {
257 2
    $this->bodyParams = $params;
258 2
  }
259
260
  /**
261
   * @return Parameters
262
   */
263 2
  public function getFileParams() {
264 2
    if ($this->fileParams === null) {
265 1
      $this->fileParams = new Parameters();
266 1
    }
267
268 2
    return $this->fileParams;
269
  }
270
271
  /**
272
   * @param Parameters $params
273
   */
274 2
  public function setFileParams(Parameters $params) {
275 2
    $this->fileParams = $params;
276 2
  }
277
278
  /**
279
   * @return ResponseInterface
280
   */
281 1
  public function execute() {
282 1
    return $this->getClient()->sendRequest($this);
283
  }
284
285
  /**
286
   * @return Request
287
   * @see RequestInterface::createClone()
288
   */
289 1
  public function createClone() {
290 1
    return clone $this;
291
  }
292
}
293