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

webeweb / pexels-library / 4234786235

pending completion
4234786235

push

github

webeweb
Update CHANGELOG

408 of 409 relevant lines covered (99.76%)

44.62 hits per line

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

96.43
/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
     * Builds 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 $this->newMandatoryParameterException("authorization");
7✔
94
        }
95

96
        try {
97

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

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

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

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

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

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

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

118
    /**
119
     * Calls the API with a request.
120
     *
121
     * @param AbstractRequest $request The request.
122
     * @param array $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 GuzzleException Throws a Guzzle exception if an error occurs.
126
     * @throws ApiException Throws an API exception if an error occurs.
127
     */
128
    protected function callApiWithRequest(AbstractRequest $request, array $queryData): string {
129

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

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

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

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

152
        return $this->callApi($uri, []);
28✔
153
    }
154

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

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