|
2 | 2 |
|
3 | 3 | namespace Shopify\Service; |
4 | 4 |
|
| 5 | +use GuzzleHttp\Client; |
5 | 6 | use GuzzleHttp\Psr7\Request; |
6 | 7 | use GuzzleHttp\Psr7\Response; |
7 | 8 | use Shopify\ApiInterface; |
8 | | -use Shopify\Object\AbstractObject; |
9 | | -use Shopify\Inflector; |
10 | 9 |
|
11 | 10 | abstract class AbstractService |
12 | 11 | { |
13 | | - private $api; |
14 | | - private $mapper; |
| 12 | + /** |
| 13 | + * Instantiated Guzzle Client for requests |
| 14 | + * @var Client |
| 15 | + */ |
| 16 | + private $client; |
| 17 | + |
| 18 | + /** |
| 19 | + * The last API response from Shopify |
| 20 | + * @var Response|null |
| 21 | + */ |
| 22 | + private $lastResponse; |
15 | 23 |
|
16 | 24 | const REQUEST_METHOD_GET = 'GET'; |
17 | 25 | const REQUEST_METHOD_POST = 'POST'; |
18 | 26 | const REQUEST_METHOD_PUT = 'PUT'; |
19 | 27 | const REQUEST_METHOD_DELETE = 'DELETE'; |
20 | 28 |
|
21 | | - public static function factory(ApiInterface $api) |
| 29 | + public static function factory(ApiInterface $api): AbstractService |
22 | 30 | { |
23 | 31 | return new static($api); |
24 | 32 | } |
25 | 33 |
|
26 | 34 | public function __construct(ApiInterface $api) |
27 | 35 | { |
28 | | - $this->api = $api; |
| 36 | + $this->client = $api->getHttpHandler(); |
29 | 37 | } |
30 | 38 |
|
31 | | - public function getApi() |
| 39 | + /** |
| 40 | + * Get the client instance |
| 41 | + * |
| 42 | + * @return Client |
| 43 | + */ |
| 44 | + public function getClient() |
32 | 45 | { |
33 | | - return $this->api; |
| 46 | + return $this->client; |
34 | 47 | } |
35 | 48 |
|
36 | | - public function request($endpoint, $method = self::REQUEST_METHOD_GET, array $params = array()) |
| 49 | + /** |
| 50 | + * @param $endpoint |
| 51 | + * @param string $method |
| 52 | + * @param array $params |
| 53 | + * @return mixed |
| 54 | + */ |
| 55 | + public function request($endpoint, $method = self::REQUEST_METHOD_GET, array $params = []) |
37 | 56 | { |
38 | | - $request = $this->createRequest($endpoint, $method); |
39 | | - return $this->send($request, $params); |
| 57 | + return $this->send(new Request($method, $endpoint), $params); |
40 | 58 | } |
41 | 59 |
|
| 60 | + /** |
| 61 | + * @param $endpoint |
| 62 | + * @param string $method |
| 63 | + * @return Request |
| 64 | + */ |
42 | 65 | public function createRequest($endpoint, $method = self::REQUEST_METHOD_GET) |
43 | 66 | { |
44 | 67 | return new Request($method, $endpoint); |
45 | 68 | } |
46 | 69 |
|
| 70 | + /** |
| 71 | + * Get the last response from Shopify |
| 72 | + * @return Response |
| 73 | + */ |
| 74 | + public function getLastResponse() |
| 75 | + { |
| 76 | + return $this->lastResponse; |
| 77 | + } |
| 78 | + |
47 | 79 | public function send(Request $request, array $params = array()) |
48 | 80 | { |
49 | | - $handler = $this->getApi()->getHttpHandler(); |
50 | 81 | $args = array(); |
51 | 82 | if ($request->getMethod() === 'GET') { |
52 | 83 | $args['query'] = $params; |
53 | 84 | } else { |
54 | 85 | $args['json'] = $params; |
55 | 86 | } |
56 | | - $this->lastResponse = $handler->send($request, $args); |
57 | | - return json_decode($this->lastResponse->getBody()->getContents(), true); |
| 87 | + $this->lastResponse = $this->client->send($request, $args); |
| 88 | + return json_decode( |
| 89 | + $this->lastResponse->getBody()->getContents(), |
| 90 | + true |
| 91 | + ); |
58 | 92 | } |
59 | 93 |
|
60 | 94 | public function createObject($className, $data) |
|
0 commit comments