Api::getSession()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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;
26
27
use FacebookAds\Http\Client;
28
use FacebookAds\Http\RequestInterface;
29
use FacebookAds\Http\ResponseInterface;
30
use FacebookAds\Logger\LoggerInterface;
31
use FacebookAds\Logger\NullLogger;
32
33
class Api {
34
35
  /**
36
   * @var string
37
   */
38
  const VERSION = ApiConfig::APIVersion;
39
40
  /**
41
   * @var Api
42
   */
43
  protected static $instance;
44
45
  /**
46
   * @var SessionInterface
47
   */
48
  private $session;
49
50
  /**
51
   * @var LoggerInterface
52
   */
53
  protected $logger;
54
55
  /**
56
   * @var Client
57
   */
58
  protected $httpClient;
59
60
  /**
61
   * @var string
62
   */
63
  protected $defaultGraphVersion;
64
65
  /**
66
   * @param Client $http_client
67
   * @param SessionInterface $session A Facebook API session
68
   */
69 6
  public function __construct(
70
    Client $http_client,
71
    SessionInterface $session) {
72 6
    $this->httpClient = $http_client;
73 6
    $this->session = $session;
74 6
  }
75
76
  /**
77
   * @param string $app_id
78
   * @param string $app_secret
79
   * @param string $access_token
80
   * @return static
81
   */
82 1
  public static function init($app_id, $app_secret, $access_token) {
83 1
    $session = new Session($app_id, $app_secret, $access_token);
84 1
    $api = new static(new Client(), $session);
85 1
    static::setInstance($api);
86
87 1
    return $api;
88
  }
89
90
  /**
91
   * @return Api|null
92
   */
93 26
  public static function instance() {
94 26
    return static::$instance;
95
  }
96
97
  /**
98
   * @param Api $instance
99
   */
100 2
  public static function setInstance(Api $instance) {
101 2
    static::$instance = $instance;
102 2
  }
103
104
  /**
105
   * @param SessionInterface $session
106
   * @return Api
107
   */
108
  public function getCopyWithSession(SessionInterface $session) {
109
    $api = new self($this->getHttpClient(), $session);
110
    $api->setDefaultGraphVersion($this->getDefaultGraphVersion());
111
    $api->setLogger($this->getLogger());
112
    return $api;
113
  }
114
115
  /**
116
   * @param string $string
117
   * @return string
118
   */
119 1
  public static function base64UrlEncode($string) {
120 1
    $str = strtr(base64_encode($string), '+/', '-_');
121 1
    $str = str_replace('=', '', $str);
122 1
    return $str;
123
  }
124
125
  /**
126
   * @param string $path
127
   * @param string $method
128
   * @param array $params
129
   * @return RequestInterface
130
   */
131 1
  public function prepareRequest(
132
    $path,
133
    $method = RequestInterface::METHOD_GET,
134
    array $params = array()) {
135
136 1
    $request = $this->getHttpClient()->createRequest();
137 1
    $request->setMethod($method);
138 1
    $request->setGraphVersion($this->getDefaultGraphVersion());
139 1
    $request->setPath($path);
140
141 1
    if ($method === RequestInterface::METHOD_GET) {
142 1
      $params_ref = $request->getQueryParams();
143 1
    } else {
144 1
      $params_ref = $request->getBodyParams();
145
    }
146
147 1
    if (!empty($params)) {
148 1
      $params_ref->enhance($params);
149 1
    }
150
151 1
    $params_ref->enhance($this->getSession()->getRequestParameters());
152
153 1
    return $request;
154
  }
155
156
  /**
157
   * @param RequestInterface $request
158
   * @return ResponseInterface
159
   */
160 1
  public function executeRequest(RequestInterface $request) {
161 1
    $this->getLogger()->logRequest('debug', $request);
162 1
    $response = $request->execute();
163 1
    $this->getLogger()->logResponse('debug', $response);
164
165 1
    return $response;
166
  }
167
168
  /**
169
   * @return string
170
   */
171 2
  public function getDefaultGraphVersion() {
172 2
    if ($this->defaultGraphVersion === null) {
173 2
      $match = array();
174 2
      if (preg_match("/^\d+\.\d+/", static::VERSION, $match)) {
175 2
        $this->defaultGraphVersion = $match[0];
176 2
      }
177 2
    }
178
179 2
    return $this->defaultGraphVersion;
180
  }
181
182
  /**
183
   * @param string $version
184
   */
185 1
  public function setDefaultGraphVersion($version) {
186 1
    $this->defaultGraphVersion = $version;
187 1
  }
188
189
  /**
190
   * Make graph api calls
191
   *
192
   * @param string $path Ads API endpoint
193
   * @param string $method Ads API request type
194
   * @param array $params Assoc of request parameters
195
   * @return ResponseInterface Graph API responses
196
   */
197 1
  public function call(
198
    $path,
199
    $method = RequestInterface::METHOD_GET,
200
    array $params = array()) {
201
202 1
    $request = $this->prepareRequest($path, $method, $params);
203
204 1
    return $this->executeRequest($request);
205
  }
206
207
  /**
208
   * @return SessionInterface
209
   */
210 3
  public function getSession() {
211 3
    return $this->session;
212
  }
213
214
  /**
215
   * @param LoggerInterface $logger
216
   */
217 2
  public function setLogger(LoggerInterface $logger) {
218 2
    $this->logger = $logger;
219 2
  }
220
221
  /**
222
   * @return LoggerInterface
223
   */
224 2
  public function getLogger() {
225 2
    if ($this->logger === null) {
226 1
      $this->logger = new NullLogger();
227 1
    }
228 2
    return $this->logger;
229
  }
230
231
  /**
232
   * @return Client
233
   */
234 2
  public function getHttpClient() {
235 2
    return $this->httpClient;
236
  }
237
}
238