• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

webeweb / haveibeenpwned-library / 8306904586

16 Mar 2024 09:46AM UTC coverage: 98.917%. Remained the same
8306904586

push

github

webeweb
Update CHANGELOG

274 of 277 relevant lines covered (98.92%)

49.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.77
/src/Provider/AbstractProvider.php
1
<?php
2

3
declare(strict_types = 1);
4

5
/*
6
 * This file is part of the haveibeenpwned-library package.
7
 *
8
 * (c) 2019 WEBEWEB
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13

14
namespace WBW\Library\HaveIBeenPwned\Provider;
15

16
use GuzzleHttp\Client;
17
use GuzzleHttp\Exception\ClientException;
18
use GuzzleHttp\Exception\GuzzleException;
19
use InvalidArgumentException;
20
use Psr\Log\LoggerInterface;
21
use Throwable;
22
use WBW\Library\HaveIBeenPwned\Request\AbstractRequest;
23
use WBW\Library\Provider\AbstractProvider as BaseProvider;
24
use WBW\Library\Provider\Exception\ApiException;
25

26
/**
27
 * Abstract provider.
28
 *
29
 * @author webeweb <https://github.com/webeweb>
30
 * @package WBW\Library\HaveIBeenPwned\Provider
31
 * @abstract
32
 */
33
abstract class AbstractProvider extends BaseProvider {
34

35
    /**
36
     * Endpoint path.
37
     *
38
     * @var string
39
     */
40
    const ENDPOINT_PATH = "https://haveibeenpwned.com/api";
41

42
    /**
43
     * Constructor.
44
     *
45
     * @param LoggerInterface|null $logger The logger.
46
     */
47
    public function __construct(LoggerInterface $logger = null) {
48
        parent::__construct($logger);
168✔
49
    }
42✔
50

51
    /**
52
     * Build the configuration.
53
     *
54
     * @param string $host The host.
55
     * @param string|null $apiKey The API key.
56
     * @return array<string,mixed> Returns the configuration.
57
     */
58
    private function buildConfiguration(string $host, string $apiKey = null): array {
59

60
        $config = [
96✔
61
            "base_uri"    => $host . "/",
128✔
62
            "debug"       => $this->getDebug(),
128✔
63
            "headers"     => [
96✔
64
                "Accept"     => "application/json",
96✔
65
                "User-Agent" => "webeweb/haveibeenpwnd-library",
96✔
66
            ],
96✔
67
            "synchronous" => true,
96✔
68
        ];
96✔
69

70
        if (null !== $apiKey) {
128✔
71
            $config["headers"]["hibp-api-key"] = $apiKey;
24✔
72
        }
73

74
        return $config;
128✔
75
    }
76

77
    /**
78
     * Call the API.
79
     *
80
     * @param AbstractRequest $request The request.
81
     * @param array<string,mixed> $queryData The query data.
82
     * @param string|null $endpointPath The endpoint path.
83
     * @param string|null $apiKey The API key.
84
     * @return string Returns the raw response.
85
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
86
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
87
     * @throws ApiException Throws an API exception if an error occurs.
88
     */
89
    protected function callApi(AbstractRequest $request, array $queryData, string $endpointPath = null, string $apiKey = null): string {
90

91
        try {
92

93
            $host = null === $endpointPath ? self::ENDPOINT_PATH . $this->getEndpointVersion() : $endpointPath;
128✔
94

95
            $config = $this->buildConfiguration($host, $apiKey);
128✔
96
            $client = new Client($config);
128✔
97

98
            $method  = "GET";
128✔
99
            $uri     = substr($this->buildResourcePath($request), 1);
128✔
100
            $options = [
90✔
101
                "query" => $queryData,
120✔
102
            ];
90✔
103

104
            $this->logInfo(sprintf("Call HaveIBeenPwned API %s %s", $method, $uri), ["config" => $config, "options" => $options]);
120✔
105

106
            $response = $client->request($method, $uri, $options);
120✔
107

108
            return $response->getBody()->getContents();
64✔
109
        } catch (InvalidArgumentException $ex) {
64✔
110

111
            throw $ex;
8✔
112
        } catch (Throwable $ex) {
56✔
113

114
            if (true === ($ex instanceof ClientException) && 404 === $ex->getCode()) {
56✔
115
                return "[]";
×
116
            }
117

118
            throw new ApiException("Call HaveIBeenPwned API failed", 500, $ex);
56✔
119
        }
120
    }
121

122
    /**
123
     * Get the endpoint version.
124
     *
125
     * @return string Returns the endpoint version.
126
     */
127
    abstract public function getEndpointVersion(): string;
128
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc