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

webeweb / pexels-library / 4086335938

pending completion
4086335938

push

github

webeweb
Update documentation

397 of 409 relevant lines covered (97.07%)

28.74 hits per line

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

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

3
/*
4
 * This file is part of the pexels-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
namespace WBW\Library\Pexels\Provider;
13

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

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

34
    use CompoundRateLimitTrait;
35

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

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

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

59
        $this->setAuthorization($authorization);
119✔
60
    }
34✔
61

62
    /**
63
     * Build the configuration.
64
     *
65
     * @return array Returns the configuration.
66
     */
67
    private function buildConfiguration(): array {
68

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

80
    /**
81
     * Calls the API.
82
     *
83
     * @param string $uri The URI.
84
     * @param array $queryData The query data.
85
     * @return string Returns the raw response.
86
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
87
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
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()) {
91✔
93
            throw new InvalidArgumentException('The mandatory parameter "authorization" is missing');
7✔
94
        }
95

96
        try {
97

98
            $config = $this->buildConfiguration();
84✔
99

100
            $client = new Client($config);
84✔
101

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

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

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

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

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

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

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

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

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

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

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

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

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

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

© 2026 Coveralls, Inc