Lines 100.00% 23 / 23
Methods 100.00% 3 / 3
Classes 100.00% 1 / 1
Covered by tests of size
Name Lines Methods CRAP
 __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
23class 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}

From Icawebdesign\Hibp\Traits\HibpConfig

7trait HibpConfig
8{
9    protected string $userAgent = 'hibp-php/7';
10
11    protected string $hibpApiRoot = 'https://haveibeenpwned.com/api';
12
13    protected int $apiVersion = 3;
14
15    protected string $pwnedPasswordsApiRoot = 'https://api.pwnedpasswords.com';
16}