| @@ 11-47 (lines=37) @@ | ||
| 8 | use function GuzzleHttp\json_decode; |
|
| 9 | use Psr\Http\Message\ResponseInterface; |
|
| 10 | ||
| 11 | class FetchPlan extends AbstractFetchPlan |
|
| 12 | { |
|
| 13 | ||
| 14 | use VerifyHttpStatusResponseCode; |
|
| 15 | ||
| 16 | /** |
|
| 17 | * @var string The relative link for fetching a certain plan |
|
| 18 | */ |
|
| 19 | const FETCH_PLAN_LINK = '/plan/:identifier'; |
|
| 20 | ||
| 21 | protected $baseUrl; |
|
| 22 | ||
| 23 | public function __construct(string $baseUrl) |
|
| 24 | { |
|
| 25 | $this->baseUrl = $baseUrl; |
|
| 26 | } |
|
| 27 | ||
| 28 | /** |
|
| 29 | * @param string|int $identifier |
|
| 30 | * @return mixed |
|
| 31 | * @throws \Gbowo\Exception\InvalidHttpResponseException if ann http response of 200 isn't returned |
|
| 32 | */ |
|
| 33 | public function handle($identifier) |
|
| 34 | { |
|
| 35 | $link = $this->baseUrl . str_replace(":identifier", $identifier, self::FETCH_PLAN_LINK); |
|
| 36 | ||
| 37 | /** |
|
| 38 | * @var ResponseInterface $response |
|
| 39 | */ |
|
| 40 | $response = $this->adapter->getHttpClient() |
|
| 41 | ->get($link); |
|
| 42 | ||
| 43 | $this->verifyResponse($response); |
|
| 44 | ||
| 45 | return json_decode($response->getBody(), true)['data']; |
|
| 46 | } |
|
| 47 | } |
|
| 48 | ||
| @@ 9-47 (lines=39) @@ | ||
| 6 | use Gbowo\Plugin\AbstractPlugin; |
|
| 7 | use function GuzzleHttp\json_decode; |
|
| 8 | ||
| 9 | class GetCustomer extends AbstractPlugin |
|
| 10 | { |
|
| 11 | use VerifyHttpStatusResponseCode; |
|
| 12 | ||
| 13 | const CUSTOMER_LINK = '/customer/:id'; |
|
| 14 | ||
| 15 | protected $baseUrl; |
|
| 16 | ||
| 17 | public function __construct(string $baseUrl) |
|
| 18 | { |
|
| 19 | $this->baseUrl = $baseUrl; |
|
| 20 | } |
|
| 21 | ||
| 22 | /** |
|
| 23 | * {@inheritdoc} |
|
| 24 | */ |
|
| 25 | public function getPluginAccessor() : string |
|
| 26 | { |
|
| 27 | return "getCustomer"; |
|
| 28 | } |
|
| 29 | ||
| 30 | public function handle(string $customerId) : array |
|
| 31 | { |
|
| 32 | $link = $this->baseUrl . str_replace(":id", $customerId, self::CUSTOMER_LINK); |
|
| 33 | ||
| 34 | $response = $this->retrieveCustomerDetails($link); |
|
| 35 | ||
| 36 | $this->verifyResponse($response); |
|
| 37 | ||
| 38 | return json_decode($response->getBody(), true)["data"]; |
|
| 39 | ||
| 40 | } |
|
| 41 | ||
| 42 | protected function retrieveCustomerDetails(string $link) |
|
| 43 | { |
|
| 44 | return $this->adapter->getHttpClient() |
|
| 45 | ->get($link); |
|
| 46 | } |
|
| 47 | } |
|
| 48 | ||