Lines
100.00%
23 / 23
Functions and Methods
100.00%
3 / 3
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Paste | 100.00% | 23 / 23 | 100.00% | 3 / 3 | 8 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | |||||
| getRequest | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 1 | |||||
| lookup | 100.00% | 16 / 16 | 100.00% | 1 / 1 | 6 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Icawebdesign\Hibp\Paste; | |
| 6 | ||
| 7 | use Exception; | |
| 8 | use GuzzleHttp\ClientInterface; | |
| 9 | use GuzzleHttp\Exception\ClientException; | |
| 10 | use GuzzleHttp\Exception\GuzzleException; | |
| 11 | use GuzzleHttp\Exception\RequestException; | |
| 12 | use Icawebdesign\Hibp\Exception\InvalidPasteDataException; | |
| 13 | use Icawebdesign\Hibp\Exception\PasteNotFoundException; | |
| 14 | use Icawebdesign\Hibp\HibpHttp; | |
| 15 | use Icawebdesign\Hibp\Traits\HibpConfig; | |
| 16 | use Illuminate\Support\Collection; | |
| 17 | use JsonException; | |
| 18 | use Psr\Http\Message\ResponseInterface; | |
| 19 | use stdClass; | |
| 20 | ||
| 21 | use const JSON_THROW_ON_ERROR; | |
| 22 | ||
| 23 | class Paste implements PasteInterface | |
| 24 | { | |
| 25 | use HibpConfig; | |
| 26 | ||
| 27 | protected ClientInterface $client; | |
| 28 | ||
| 29 | public int $statusCode; | |
| 30 | ||
| 31 | protected string $apiRoot; | |
| 32 | ||
| 33 | public function __construct(HibpHttp $hibpHttp) | |
| 34 | { | |
| 35 | $this->apiRoot = "{$this->hibpApiRoot}/v{$this->apiVersion}"; | |
| 36 | $this->client = $hibpHttp->client(); | |
| 37 | } | |
| 38 | ||
| 39 | /** | |
| 40 | * @param string $uri | |
| 41 | * @param array<string, mixed> $options | |
| 42 | * | |
| 43 | * @return ResponseInterface | |
| 44 | * @throws GuzzleException | |
| 45 | */ | |
| 46 | private function getRequest(string $uri, array $options = []): ResponseInterface | |
| 47 | { | |
| 48 | return $this->client->request( | |
| 49 | 'GET', | |
| 50 | $uri, | |
| 51 | $options, | |
| 52 | ); | |
| 53 | } | |
| 54 | ||
| 55 | /** | |
| 56 | * Check for any pastes containing specified email address | |
| 57 | * | |
| 58 | * @param string $emailAddress | |
| 59 | * @param array<string, mixed> $options | |
| 60 | * | |
| 61 | * @return Collection<int, PasteEntity> | |
| 62 | * @throws GuzzleException|InvalidPasteDataException|Exception | |
| 63 | */ | |
| 64 | public function lookup(string $emailAddress, array $options = []): Collection | |
| 65 | { | |
| 66 | try { | |
| 67 | $response = $this->getRequest( | |
| 68 | uri: sprintf('%s/pasteaccount/%s', $this->apiRoot, urlencode($emailAddress)), | |
| 69 | options: $options, | |
| 70 | ); | |
| 71 | } catch (ClientException $exception) { | |
| 72 | $this->statusCode = $exception->getCode(); | |
| 73 | ||
| 74 | throw match ($exception->getCode()) { | |
| 75 | 404 => new PasteNotFoundException($exception->getMessage()), | |
| 76 | 400 => new RequestException($exception->getMessage(), $exception->getRequest()), | |
| 77 | default => $exception, | |
| 78 | }; | |
| 79 | } | |
| 80 | ||
| 81 | $this->statusCode = $response->getStatusCode(); | |
| 82 | ||
| 83 | try { | |
| 84 | /** @var array<int, stdClass> $data */ | |
| 85 | $data = json_decode((string)$response->getBody(), associative: false, flags: JSON_THROW_ON_ERROR); | |
| 86 | } catch (JsonException $exception) { | |
| 87 | throw new InvalidPasteDataException($exception->getMessage()); | |
| 88 | } | |
| 89 | ||
| 90 | return Collection::make($data) | |
| 91 | ->map(static fn (stdClass $paste): PasteEntity => new PasteEntity(data: $paste)); | |
| 92 | } | |
| 93 | } |