1 | <?php |
||
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) { |
|
89 | |||
90 | /** |
||
91 | * @return Api|null |
||
92 | */ |
||
93 | 26 | public static function instance() { |
|
96 | |||
97 | /** |
||
98 | * @param Api $instance |
||
99 | */ |
||
100 | 2 | public static function setInstance(Api $instance) { |
|
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( |
|
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() { |
|
181 | |||
182 | /** |
||
183 | * @param string $version |
||
184 | */ |
||
185 | 1 | public function setDefaultGraphVersion($version) { |
|
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() { |
|
213 | |||
214 | /** |
||
215 | * @param LoggerInterface $logger |
||
216 | */ |
||
217 | 2 | public function setLogger(LoggerInterface $logger) { |
|
220 | |||
221 | /** |
||
222 | * @return LoggerInterface |
||
223 | */ |
||
224 | 2 | public function getLogger() { |
|
230 | |||
231 | /** |
||
232 | * @return Client |
||
233 | */ |
||
234 | 2 | public function getHttpClient() { |
|
237 | } |
||
238 |