1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the fnayou/instapush-php project. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2017. Aymen FNAYOU <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Fnayou\InstapushPHP\Api; |
12
|
|
|
|
13
|
|
|
use Fnayou\InstapushPHP\InstapushClient; |
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class AbstractApi. |
18
|
|
|
*/ |
19
|
|
|
abstract class AbstractApi |
20
|
|
|
{ |
21
|
|
|
/** @var \Fnayou\InstapushPHP\InstapushClient */ |
22
|
|
|
protected $instapushClient; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param \Fnayou\InstapushPHP\InstapushClient $instapushClient |
26
|
|
|
*/ |
27
|
|
|
public function __construct(InstapushClient $instapushClient) |
28
|
|
|
{ |
29
|
|
|
$this->instapushClient = $instapushClient; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return \Fnayou\InstapushPHP\InstapushClient |
34
|
|
|
*/ |
35
|
|
|
protected function getInstapushClient() |
36
|
|
|
{ |
37
|
|
|
return $this->instapushClient; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param \Fnayou\InstapushPHP\InstapushClient $instapushClient |
42
|
|
|
*/ |
43
|
|
|
protected function setInstapushClient(InstapushClient $instapushClient) |
44
|
|
|
{ |
45
|
|
|
$this->instapushClient = $instapushClient; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function transformResponse(ResponseInterface $response, $class) |
49
|
|
|
{ |
50
|
|
|
if (!$this->getInstapushClient()->getTransformer()) { |
51
|
|
|
return $response; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
//TODO: handle errors |
55
|
|
|
|
56
|
|
|
return $this->getInstapushClient()->getTransformer()->transform($response, $class); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function doGet(string $path, array $parameters = [], array $headers = []) |
60
|
|
|
{ |
61
|
|
|
if (count($parameters) > 0) { |
62
|
|
|
$path .= '?'.\http_build_query($parameters); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$request = $this |
66
|
|
|
->getInstapushClient() |
67
|
|
|
->getRequestFactory() |
68
|
|
|
->createRequest('GET', $path, $headers); |
69
|
|
|
|
70
|
|
|
return $this->instapushClient->getHttpClient()->sendRequest($request); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|