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

webeweb / haveibeenpwned-library / 9267781334

28 May 2024 10:32AM UTC coverage: 98.917%. Remained the same
9267781334

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

12
declare(strict_types = 1);
13

14
namespace WBW\Library\HaveIBeenPwned\Provider;
15

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

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

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

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

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

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

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

73
        return $config;
128✔
74
    }
75

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

89
        try {
90

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

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

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

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

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

106
            return $response->getBody()->getContents();
64✔
107
        } catch (InvalidArgumentException $ex) {
64✔
108
            throw $ex;
8✔
109
        } catch (Throwable $ex) {
56✔
110

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

115
            throw new ProviderException("Call HaveIBeenPwned API failed", 500, $ex);
56✔
116
        }
117
    }
118

119
    /**
120
     * Get the endpoint version.
121
     *
122
     * @return string Returns the endpoint version.
123
     */
124
    abstract public function getEndpointVersion(): string;
125
}
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