Passed
Push — master ( 03395d...78c946 )
by WEBEWEB
10:38
created

ApiProvider::populateResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 1
crap 2
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\Exception\GuzzleException;
15
use InvalidArgumentException;
16
use WBW\Library\Pexels\Api\PaginateResponseInterface;
17
use WBW\Library\Pexels\Request\AbstractRequest;
18
use WBW\Library\Pexels\Response\AbstractResponse;
19
use WBW\Library\Pexels\Response\PhotosResponse;
20
use WBW\Library\Pexels\Response\VideosResponse;
21
use WBW\Library\Provider\Exception\ApiException;
22
23
/**
24
 * API provider.
25
 *
26
 * @author webeweb <https://github.com/webeweb>
27
 * @package WBW\Library\Pexels\Provider
28
 */
29
class ApiProvider extends AbstractProvider {
30
31
    /**
32
     * Populates a response.
33
     *
34
     * @param AbstractResponse $response The response.
35
     * @return AbstractResponse Returns the response.
36
     */
37
    protected function populateResponse(AbstractResponse $response): AbstractResponse {
38
39
        $response->setLimit($this->getLimit());
40
        $response->setRemaining($this->getRemaining());
41
        $response->setReset($this->getReset());
42
43
        return $response;
44
    }
45
46 5
    /**
47
     * Requests a next page.
48 5
     *
49 5
     * @param PaginateResponseInterface $response The response.
50 5
     * @return PhotosResponse|VideosResponse Returns the response.
51
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
52 5
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
53
     * @throws ApiException Throws an API exception if an error occurs.
54
     */
55
    public function requestNextPage(PaginateResponseInterface $response): AbstractResponse {
56
57
        $rawResponse = $this->callApiWithResponse($response);
58
59
        return $this->populateResponse($response->deserializeResponse($rawResponse));
60
    }
61
62
    /**
63
     * Request a previous page.
64 10
     *
65
     * @param PaginateResponseInterface $response The response.
66 10
     * @return PhotosResponse|VideosResponse Returns the response.
67
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
68 10
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
69
     * @throws ApiException Throws an API exception if an error occurs.
70
     */
71
    public function requestPrevPage(PaginateResponseInterface $response): AbstractResponse {
72
73
        $rawResponse = $this->callApiWithResponse($response, false);
74
75
        return $this->populateResponse($response->deserializeResponse($rawResponse));
76
    }
77
78
    /**
79
     * Sends a request.
80
     *
81
     * @param AbstractRequest $request The request.
82 10
     * @return AbstractResponse Returns the response.
83
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
84 10
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
85
     * @throws ApiException Throws an API exception if an error occurs.
86
     */
87
    public function sendRequest(AbstractRequest $request): AbstractResponse {
88
89
        $queryData   = $request->serializeRequest();
90
        $rawResponse = $this->callApiWithRequest($request, $queryData);
91
92
        return $this->populateResponse($request->deserializeResponse($rawResponse));
93
    }
94
}
95