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

webeweb / pexels-library / 8306846224

16 Mar 2024 09:32AM UTC coverage: 97.216%. Remained the same
8306846224

push

github

webeweb
Update CHANGELOG

419 of 431 relevant lines covered (97.22%)

34.5 hits per line

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

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

3
declare(strict_types = 1);
4

5
/*
6
 * This file is part of the pexels-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\Pexels\Provider;
15

16
use GuzzleHttp\Client;
17
use GuzzleHttp\Exception\GuzzleException;
18
use InvalidArgumentException;
19
use Psr\Log\LoggerInterface;
20
use Throwable;
21
use WBW\Library\Pexels\Api\PaginateResponseInterface;
22
use WBW\Library\Pexels\Request\AbstractRequest;
23
use WBW\Library\Provider\AbstractProvider as BaseProvider;
24
use WBW\Library\Provider\Exception\ApiException;
25
use WBW\Library\Traits\Compounds\CompoundRateLimitTrait;
26

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

36
    use CompoundRateLimitTrait;
37

38
    /**
39
     * Endpoint path.
40
     *
41
     * @var string
42
     */
43
    const ENDPOINT_PATH = "https://api.pexels.com";
44

45
    /**
46
     * Authorization.
47
     *
48
     * @var string
49
     */
50
    private $authorization;
51

52
    /**
53
     * Constructor.
54
     *
55
     * @param string|null $authorization The authorization.
56
     * @param LoggerInterface|null $logger The logger.
57
     */
58
    public function __construct(string $authorization = null, LoggerInterface $logger = null) {
59
        parent::__construct($logger);
136✔
60

61
        $this->setAuthorization($authorization);
136✔
62
    }
34✔
63

64
    /**
65
     * Build the configuration.
66
     *
67
     * @return array<string,mixed> Returns the configuration.
68
     */
69
    private function buildConfiguration(): array {
70

71
        return [
72✔
72
            "debug"       => $this->getDebug(),
96✔
73
            "headers"     => [
72✔
74
                "Accept"        => "application/json",
96✔
75
                "User-Agent"    => "webeweb/pexels-library",
96✔
76
                "Authorization" => $this->getAuthorization(),
96✔
77
            ],
72✔
78
            "synchronous" => true,
72✔
79
        ];
72✔
80
    }
81

82
    /**
83
     * Call the API.
84
     *
85
     * @param string $uri The URI.
86
     * @param array<string,mixed> $queryData The query data.
87
     * @return string Returns the raw response.
88
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
89
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
90
     * @throws ApiException Throws an API exception if an error occurs.
91
     */
92
    private function callApi(string $uri, array $queryData): string {
93

94
        if (null === $this->getAuthorization()) {
104✔
95
            throw $this->newMandatoryParameterException("authorization");
8✔
96
        }
97

98
        try {
99

100
            $config = $this->buildConfiguration();
96✔
101
            $client = new Client($config);
96✔
102

103
            $method  = "GET";
96✔
104
            $options = 0 < count($queryData) ? ["query" => $queryData] : [];
96✔
105

106
            $this->logInfo(sprintf("Call Pexels API %s %s", $method, $uri), ["config" => $config, "options" => $options]);
96✔
107

108
            $response = $client->request($method, $uri, $options);
96✔
109

110
            $this->setLimit(intval($response->getHeaderLine("X-Ratelimit-Limit")));
×
111
            $this->setRemaining(intval($response->getHeaderLine("X-Ratelimit-Remaining")));
×
112

113
            return $response->getBody()->getContents();
×
114
        } catch (Throwable $ex) {
96✔
115

116
            throw new ApiException("Call Pexels API failed", 500, $ex);
96✔
117
        }
118
    }
119

120
    /**
121
     * Call the API with a request.
122
     *
123
     * @param AbstractRequest $request The request.
124
     * @param array<string,mixed> $queryData The query data.
125
     * @return string Returns the raw response.
126
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
127
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
128
     * @throws ApiException Throws an API exception if an error occurs.
129
     */
130
    protected function callApiWithRequest(AbstractRequest $request, array $queryData): string {
131

132
        $uri = self::ENDPOINT_PATH . $this->buildResourcePath($request);
112✔
133

134
        return $this->callApi($uri, $queryData);
104✔
135
    }
136

137
    /**
138
     * Call the API with a response.
139
     *
140
     * @param PaginateResponseInterface $response The request.
141
     * @param bool $nextPage Next page ?
142
     * @return string Returns the raw response.
143
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
144
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
145
     * @throws ApiException Throws an API exception if an error occurs.
146
     */
147
    protected function callApiWithResponse(PaginateResponseInterface $response, bool $nextPage = true): string {
148

149
        $uri = true === $nextPage ? $response->getNextPage() : $response->getPrevPage();
×
150
        if (null === $uri) {
×
151
            return "";
×
152
        }
153

154
        return $this->callApi($uri, []);
×
155
    }
156

157
    /**
158
     * Get the authorization.
159
     *
160
     * @return string|null Returns the authorization.
161
     */
162
    public function getAuthorization(): ?string {
163
        return $this->authorization;
120✔
164
    }
165

166
    /**
167
     * Set the authorization.
168
     *
169
     * @param string|null $authorization The authorization.
170
     * @return AbstractProvider Returns this provider.
171
     */
172
    public function setAuthorization(?string $authorization): AbstractProvider {
173
        $this->authorization = $authorization;
136✔
174
        return $this;
136✔
175
    }
176
}
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