| Total Complexity | 4 |
| Total Lines | 46 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 14 | class Endpoint extends Psr7Message |
||
| 15 | { |
||
| 16 | use EndpointAware; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Determine whether the handler can handle the given source |
||
| 20 | * |
||
| 21 | * @param mixed $source |
||
| 22 | * @return bool |
||
| 23 | */ |
||
| 24 | 10 | public function handles($source): bool |
|
| 25 | { |
||
| 26 | 10 | return $this->isEndpoint($source); |
|
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Handle the given source |
||
| 31 | * |
||
| 32 | * @param mixed $source |
||
| 33 | * @param string $path |
||
| 34 | * @return Traversable |
||
| 35 | */ |
||
| 36 | 3 | public function handle($source, string $path): Traversable |
|
| 37 | { |
||
| 38 | 3 | if (!$this->guzzleIsLoaded()) { |
|
| 39 | 1 | throw new LazyJsonException('Guzzle is required to load JSON from endpoints'); |
|
| 40 | } |
||
| 41 | |||
| 42 | 2 | $response = (new Client())->get($source, [ |
|
| 43 | 'headers' => [ |
||
| 44 | 2 | 'Accept' => 'application/json', |
|
| 45 | 'Content-Type' => 'application/json', |
||
| 46 | ], |
||
| 47 | ]); |
||
| 48 | |||
| 49 | 2 | return parent::handle($response, $path); |
|
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Determine whether Guzzle is loaded, useful for testing |
||
| 54 | * |
||
| 55 | * @return bool |
||
| 56 | */ |
||
| 57 | 2 | protected function guzzleIsLoaded(): bool |
|
| 60 | } |
||
| 61 | } |
||
| 62 |