| @@ 12-51 (lines=40) @@ | ||
| 9 | use Psr\Http\Message\ResponseInterface; |
|
| 10 | use function GuzzleHttp\json_decode; |
|
| 11 | ||
| 12 | class FetchAllPlans extends AbstractFetchAllPlans |
|
| 13 | { |
|
| 14 | const FETCH_ALL_PLANS_RELATIVE_LINK = "/plan"; |
|
| 15 | ||
| 16 | /** |
|
| 17 | * @var \Gbowo\Contract\Adapter\AdapterInterface |
|
| 18 | */ |
|
| 19 | protected $adapter; |
|
| 20 | ||
| 21 | protected $baseUrl; |
|
| 22 | ||
| 23 | public function __construct(string $baseUrl) |
|
| 24 | { |
|
| 25 | $this->baseUrl = $baseUrl; |
|
| 26 | } |
|
| 27 | ||
| 28 | /** |
|
| 29 | * @param array $args |
|
| 30 | * @return mixed |
|
| 31 | * @throws \Gbowo\Exception\InvalidHttpResponseException if the HTTP response status code is not 200 |
|
| 32 | */ |
|
| 33 | public function handle(array $args = []) |
|
| 34 | { |
|
| 35 | $params = toQueryParams($args); |
|
| 36 | ||
| 37 | /** |
|
| 38 | * @var ResponseInterface $response |
|
| 39 | */ |
|
| 40 | $response = $this->adapter->getHttpClient() |
|
| 41 | ->get($this->baseUrl . self::FETCH_ALL_PLANS_RELATIVE_LINK . $params); |
|
| 42 | ||
| 43 | if (200 !== $response->getStatusCode()) { |
|
| 44 | throw new InvalidHttpResponseException( |
|
| 45 | "Expected 200. Got {$response->getStatusCode()} instead" |
|
| 46 | ); |
|
| 47 | } |
|
| 48 | ||
| 49 | return json_decode($response->getBody(), true)['data']; |
|
| 50 | } |
|
| 51 | } |
|
| 52 | ||
| @@ 11-49 (lines=39) @@ | ||
| 8 | use Psr\Http\Message\ResponseInterface; |
|
| 9 | use Gbowo\Exception\InvalidHttpResponseException; |
|
| 10 | ||
| 11 | class FetchPlan extends AbstractFetchPlan |
|
| 12 | { |
|
| 13 | ||
| 14 | /** |
|
| 15 | * @var string The relative link for fetching a certain plan |
|
| 16 | */ |
|
| 17 | const FETCH_PLAN_LINK = '/plan/:identifier'; |
|
| 18 | ||
| 19 | protected $baseUrl; |
|
| 20 | ||
| 21 | public function __construct(string $baseUrl) |
|
| 22 | { |
|
| 23 | $this->baseUrl = $baseUrl; |
|
| 24 | } |
|
| 25 | ||
| 26 | /** |
|
| 27 | * @param string|int $identifier |
|
| 28 | * @return mixed |
|
| 29 | * @throws \Gbowo\Exception\InvalidHttpResponseException if ann http response of 200 isn't returned |
|
| 30 | */ |
|
| 31 | public function handle($identifier) |
|
| 32 | { |
|
| 33 | $link = $this->baseUrl . str_replace(":identifier", $identifier, self::FETCH_PLAN_LINK); |
|
| 34 | ||
| 35 | /** |
|
| 36 | * @var ResponseInterface $response |
|
| 37 | */ |
|
| 38 | $response = $this->adapter->getHttpClient() |
|
| 39 | ->get($link); |
|
| 40 | ||
| 41 | if (200 !== $response->getStatusCode()) { |
|
| 42 | throw new InvalidHttpResponseException( |
|
| 43 | "Expected 200. Got {$response->getStatusCode()} instead" |
|
| 44 | ); |
|
| 45 | } |
|
| 46 | ||
| 47 | return json_decode($response->getBody(), true)['data']; |
|
| 48 | } |
|
| 49 | } |
|
| 50 | ||