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

webeweb / pexels-library / 8308898083

16 Mar 2024 04:02PM UTC coverage: 97.216%. Remained the same
8308898083

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 InvalidArgumentException;
18
use Psr\Log\LoggerInterface;
19
use Throwable;
20
use WBW\Library\Pexels\Api\PaginateResponseInterface;
21
use WBW\Library\Pexels\Request\AbstractRequest;
22
use WBW\Library\Provider\AbstractProvider as BaseProvider;
23
use WBW\Library\Provider\Exception\ApiException;
24
use WBW\Library\Traits\Compounds\CompoundRateLimitTrait;
25

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

35
    use CompoundRateLimitTrait;
36

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

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

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

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

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

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

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

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

96
        try {
97

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

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

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

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

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

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

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

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

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

131
        return $this->callApi($uri, $queryData);
104✔
132
    }
133

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

145
        $uri = true === $nextPage ? $response->getNextPage() : $response->getPrevPage();
×
146
        if (null === $uri) {
×
147
            return "";
×
148
        }
149

150
        return $this->callApi($uri, []);
×
151
    }
152

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

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