Completed
Push — master ( a2b744...066f3a )
by WEBEWEB
01:53
created

ApiProvider::prevPage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
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 InvalidArgumentException;
15
use WBW\Library\Core\Exception\ApiException;
16
use WBW\Library\Pexels\API\PaginateResponseInterface;
17
use WBW\Library\Pexels\Model\AbstractResponse;
18
use WBW\Library\Pexels\Model\Request\CuratedPhotosRequest;
19
use WBW\Library\Pexels\Model\Request\GetPhotoRequest;
20
use WBW\Library\Pexels\Model\Request\GetVideoRequest;
21
use WBW\Library\Pexels\Model\Request\PopularVideosRequest;
22
use WBW\Library\Pexels\Model\Request\SearchPhotosRequest;
23
use WBW\Library\Pexels\Model\Request\SearchVideosRequest;
24
use WBW\Library\Pexels\Model\Response\PhotoResponse;
25
use WBW\Library\Pexels\Model\Response\PhotosResponse;
26
use WBW\Library\Pexels\Model\Response\VideoResponse;
27
use WBW\Library\Pexels\Model\Response\VideosResponse;
28
use WBW\Library\Pexels\Serializer\RequestSerializer;
29
use WBW\Library\Pexels\Serializer\ResponseDeserializer;
30
31
/**
32
 * API provider.
33
 *
34
 * @author webeweb <https://github.com/webeweb/>
35
 * @package WBW\Library\Pexels\Provider
36
 */
37
class ApiProvider extends AbstractProvider {
38
39
    /**
40
     * Before return a response.
41
     *
42
     * @param AbstractResponse $response The response.
43
     * @return AbstractResponse Returns the response.
44
     */
45
    protected function beforeReturnResponse(AbstractResponse $response) {
46
47
        $response->setLimit($this->getLimit());
48
        $response->setRemaining($this->getRemaining());
49
        $response->setReset($this->getReset());
50
51
        return $response;
52
    }
53
54
    /**
55
     * Curated photos.
56
     *
57
     * @param CuratedPhotosRequest $curatedPhotosRequest The curated photos request.
58
     * @return PhotosResponse Returns the photos response.
59
     * @throws ApiException Throws an API exception if an error occurs.
60
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
61
     */
62
    public function curatedPhotos(CuratedPhotosRequest $curatedPhotosRequest) {
63
64
        $queryData = RequestSerializer::serializeCuratedPhotosRequest($curatedPhotosRequest);
65
66
        $rawResponse = $this->callApiWithRequest($curatedPhotosRequest, $queryData);
67
68
        return $this->beforeReturnResponse(ResponseDeserializer::deserializePhotosResponse($rawResponse));
69
    }
70
71
    /**
72
     * Get a photo.
73
     *
74
     * @param GetPhotoRequest $getPhotoRequest The get photo request.
75
     * @return PhotoResponse Returns the photo response.
76
     * @throws ApiException Throws an API exception if an error occurs.
77
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
78
     */
79
    public function getPhoto(GetPhotoRequest $getPhotoRequest) {
80
81
        $rawResponse = $this->callApiWithRequest($getPhotoRequest, []);
82
83
        return $this->beforeReturnResponse(ResponseDeserializer::deserializePhotoResponse($rawResponse));
84
    }
85
86
    /**
87
     * Get a video.
88
     *
89
     * @param GetVideoRequest $getVideoRequest The get video request.
90
     * @return VideoResponse Returns the video response.
91
     * @throws ApiException Throws an API exception if an error occurs.
92
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
93
     */
94
    public function getVideo(GetVideoRequest $getVideoRequest) {
95
96
        $rawResponse = $this->callApiWithRequest($getVideoRequest, []);
97
98
        return $this->beforeReturnResponse(ResponseDeserializer::deserializeVideoResponse($rawResponse));
99
    }
100
101
    /**
102
     * Next page.
103
     *
104
     * @param PaginateResponseInterface $paginateResponse The response.
105
     * @return ImagesResponse|VideosResponse Returns the response.
106
     * @throws ApiException Throws an API exception if an error occurs.
107
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
108
     */
109
    public function nextPage(PaginateResponseInterface $paginateResponse) {
110
111
        $rawResponse = $this->callApiWithResponse($paginateResponse, true);
112
113
        if (true === ($paginateResponse instanceof PhotosResponse)) {
114
            return $this->beforeReturnResponse(ResponseDeserializer::deserializePhotosResponse($rawResponse));
115
        }
116
117
        return $this->beforeReturnResponse(ResponseDeserializer::deserializeVideosResponse($rawResponse));
118
    }
119
120
    /**
121
     * Popular videos.
122
     *
123
     * @param PopularVideosRequest $popularVideosRequest The popular videos request.
124
     * @return VideosResponse Returns the videos response.
125
     * @throws ApiException Throws an API exception if an error occurs.
126
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
127
     */
128
    public function popularVideos(PopularVideosRequest $popularVideosRequest) {
129
130
        $queryData = RequestSerializer::serializePopularVideosRequest($popularVideosRequest);
131
132
        $rawResponse = $this->callApiWithRequest($popularVideosRequest, $queryData);
133
134
        return $this->beforeReturnResponse(ResponseDeserializer::deserializeVideosResponse($rawResponse));
135
    }
136
137
    /**
138
     * Prev page.
139
     *
140
     * @param PaginateResponseInterface $paginateResponse The response.
141
     * @return ImagesResponse|VideosResponse Returns the response.
142
     * @throws ApiException Throws an API exception if an error occurs.
143
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
144
     */
145
    public function prevPage(PaginateResponseInterface $paginateResponse) {
146
147
        $rawResponse = $this->callApiWithResponse($paginateResponse, false);
148
149
        if (true === ($paginateResponse instanceof PhotosResponse)) {
150
            return $this->beforeReturnResponse(ResponseDeserializer::deserializePhotosResponse($rawResponse));
151
        }
152
153
        return $this->beforeReturnResponse(ResponseDeserializer::deserializeVideosResponse($rawResponse));
154
    }
155
156
    /**
157
     * Search photos.
158
     *
159
     * @param SearchPhotosRequest $searchPhotosRequest The search photos request.
160
     * @return PhotosResponse Returns the photo response.
161
     * @throws ApiException Throws an API exception if an error occurs.
162
     * @throws InvalidArgumentException Throws an invalid argument exception if a mandatory parameter is missing.
163
     */
164
    public function searchPhotos(SearchPhotosRequest $searchPhotosRequest) {
165
166
        $queryData = RequestSerializer::serializeSearchPhotosRequest($searchPhotosRequest);
167
168
        $rawResponse = $this->callApiWithRequest($searchPhotosRequest, $queryData);
169
170
        return $this->beforeReturnResponse(ResponseDeserializer::deserializePhotosResponse($rawResponse));
171
    }
172
173
    /**
174
     * Search videos.
175
     *
176
     * @param SearchVideosRequest $searchVideosRequest The search videos request.
177
     * @return VideosResponse Returns the videos response.
178
     * @throws ApiException Throws an API exception if an error occurs.
179
     * @throws InvalidArgumentException Throws an invalid argument exception if a mandatory parameter is missing.
180
     */
181
    public function searchVideos(SearchVideosRequest $searchVideosRequest) {
182
183
        $queryData = RequestSerializer::serializeSearchVideosRequest($searchVideosRequest);
184
185
        $rawResponse = $this->callApiWithRequest($searchVideosRequest, $queryData);
186
187
        return $this->beforeReturnResponse(ResponseDeserializer::deserializeVideosResponse($rawResponse));
188
    }
189
}
190