Completed
Push — master ( 837ea5...a962f3 )
by
unknown
04:34
created

Client::setRequestPrototype()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 3
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
use FacebookAds\Api;
28
use FacebookAds\Http\Adapter\AdapterInterface;
29
use FacebookAds\Http\Adapter\CurlAdapter;
30
use FacebookAds\Http\Exception\EmptyResponseException;
31
use FacebookAds\Http\Exception\RequestException;
32
33
class Client {
34
35
  /**
36
   * @var string
37
   */
38
  const DEFAULT_GRAPH_BASE_DOMAIN = 'facebook.com';
39
40
  /**
41
   * @var string
42
   */
43
  const DEFAULT_LAST_LEVEL_DOMAIN = 'graph';
44
45
  /**
46
   * @var RequestInterface
47
   */
48
  protected $requestPrototype;
49
50
  /**
51
   * @var ResponseInterface
52
   */
53
  protected $responsePrototype;
54
55
  /**
56
   * @var Headers
57
   */
58
  protected $defaultRequestHeaders;
59
60
  /**
61
   * @var AdapterInterface
62
   */
63
  protected $adapter;
64
65
  /**
66
   * @var string
67
   */
68
  protected $caBundlePath;
69
70
  /**
71
   * @var string
72
   */
73
  protected $defaultGraphBaseDomain = self::DEFAULT_GRAPH_BASE_DOMAIN;
74
75
  /**
76
   * @return RequestInterface
77
   */
78 1
  public function getRequestPrototype() {
79 1
    if ($this->requestPrototype === null) {
80 1
      $this->requestPrototype = new Request($this);
81 1
    }
82
83 1
    return $this->requestPrototype;
84
  }
85
86
  /**
87
   * @param RequestInterface $prototype
88
   */
89 1
  public function setRequestPrototype(RequestInterface $prototype) {
90 1
    $this->requestPrototype = $prototype;
91 1
  }
92
93
  /**
94
   * @return RequestInterface
95
   */
96 1
  public function createRequest() {
97 1
    return $this->getRequestPrototype()->createClone();
98
  }
99
100
  /**
101
   * @return ResponseInterface
102
   */
103 2
  public function getResponsePrototype() {
104 2
    if ($this->responsePrototype === null) {
105 2
      $this->responsePrototype = new Response();
106 2
    }
107
108 2
    return $this->responsePrototype;
109
  }
110
111
  /**
112
   * @param ResponseInterface $prototype
113
   */
114 1
  public function setResponsePrototype(ResponseInterface $prototype) {
115 1
    $this->responsePrototype = $prototype;
116 1
  }
117
118
  /**
119
   * @return ResponseInterface
120
   */
121 2
  public function createResponse() {
122 2
    return clone $this->getResponsePrototype();
123
  }
124
125
  /**
126
   * @return Headers
127
   */
128 1
  public function getDefaultRequestHeaderds() {
129 1
    if ($this->defaultRequestHeaders === null) {
130 1
      $this->defaultRequestHeaders = new Headers(array(
131 1
        'User-Agent' => 'fb-php-ads-'.Api::VERSION,
132 1
        'Accept-Encoding' => '*',
133 1
      ));
134 1
    }
135
136 1
    return $this->defaultRequestHeaders;
137
  }
138
139
  /**
140
   * @param Headers $headers
141
   */
142 1
  public function setDefaultRequestHeaders(Headers $headers) {
143 1
    $this->defaultRequestHeaders = $headers;
144 1
  }
145
146
  /**
147
   * @return string
148
   */
149 1
  public function getDefaultGraphBaseDomain() {
150 1
    return $this->defaultGraphBaseDomain;
151
  }
152
153
  /**
154
   * @param string $domain
155
   */
156 1
  public function setDefaultGraphBaseDomain($domain) {
157 1
    $this->defaultGraphBaseDomain = $domain;
158 1
  }
159
160
  /**
161
   * @return AdapterInterface
162
   */
163 2
  public function getAdapter() {
164 2
    if ($this->adapter === null) {
165 1
      $this->adapter = new CurlAdapter($this);
166 1
    }
167
168 2
    return $this->adapter;
169
  }
170
171
  /**
172
   * @param AdapterInterface $adapter
173
   */
174 2
  public function setAdapter(AdapterInterface $adapter) {
175 2
    $this->adapter = $adapter;
176 2
  }
177
178
  /**
179
   * @return string
180
   */
181 4
  public function getCaBundlePath() {
182 4
    if ($this->caBundlePath === null) {
183 4
      $this->caBundlePath = __DIR__.DIRECTORY_SEPARATOR
184 4
        .str_repeat('..'.DIRECTORY_SEPARATOR, 3)
185 4
        .'fb_ca_chain_bundle.crt';
186 4
    }
187
188 4
    return $this->caBundlePath;
189
  }
190
191
  /**
192
   * @param string $path
193
   */
194 1
  public function setCaBundlePath($path) {
195 1
    $this->caBundlePath = $path;
196 1
  }
197
198
  /**
199
   * @param RequestInterface $request
200
   * @return ResponseInterface
201
   * @throws RequestException
202
   */
203 1
  public function sendRequest(RequestInterface $request) {
204 1
    $response = $this->getAdapter()->sendRequest($request);
205 1
    $response->setRequest($request);
206 1
    $response_content = $response->getContent();
207
208 1
    if ($response_content === null) {
209 1
      throw new EmptyResponseException($response);
210
    }
211
212
    if (is_array($response_content)
213
      && array_key_exists('error', $response_content)) {
214
215
      throw RequestException::create($response);
216
    }
217
218
    return $response;
219
  }
220
}
221