Completed
Push — master ( f90dd6...0a073f )
by WEBEWEB
05:01
created

APIProvider::getPhoto()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\Pexels\Exception\APIException;
16
use WBW\Library\Pexels\Model\AbstractResponse;
17
use WBW\Library\Pexels\Model\Request\CuratedPhotosRequest;
18
use WBW\Library\Pexels\Model\Request\GetPhotoRequest;
19
use WBW\Library\Pexels\Model\Request\GetVideoRequest;
20
use WBW\Library\Pexels\Model\Request\PopularVideosRequest;
21
use WBW\Library\Pexels\Model\Request\SearchPhotosRequest;
22
use WBW\Library\Pexels\Model\Request\SearchVideosRequest;
23
use WBW\Library\Pexels\Model\Response\PhotoResponse;
24
use WBW\Library\Pexels\Model\Response\PhotosResponse;
25
use WBW\Library\Pexels\Model\Response\VideoResponse;
26
use WBW\Library\Pexels\Model\Response\VideosResponse;
27
use WBW\Library\Pexels\Normalizer\RequestNormalizer;
28
use WBW\Library\Pexels\Normalizer\ResponseNormalizer;
29
30
/**
31
 * API provider.
32
 *
33
 * @author webeweb <https://github.com/webeweb/>
34
 * @package WBW\Library\Pexels\Provider
35
 */
36
class APIProvider extends AbstractProvider {
37
38
    /**
39
     * Before return a response.
40
     *
41
     * @param AbstractResponse $response The response.
42
     * @return AbstractResponse Returns the response.
43
     */
44
    protected function beforeReturnResponse(AbstractResponse $response) {
45
46
        $response->setLimit($this->getLimit());
47
        $response->setRemaining($this->getRemaining());
48
        $response->setReset($this->getReset());
49
50
        return $response;
51
    }
52
53
    /**
54
     * Curated photos.
55
     *
56
     * @param CuratedPhotosRequest $curatedPhotosRequest The curated photos request.
57
     * @return PhotosResponse Returns the photos response.
58
     * @throws APIException Throws an API exception if an error occurs.
59
     */
60
    public function curatedPhotos(CuratedPhotosRequest $curatedPhotosRequest) {
61
62
        $queryData = RequestNormalizer::normalizeCuratedPhotosRequest($curatedPhotosRequest);
63
64
        $rawResponse = $this->callAPI($curatedPhotosRequest, $queryData);
65
66
        $response = ResponseNormalizer::denormalizePhotosResponse($rawResponse);
67
68
        return $this->beforeReturnResponse($response);
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
     */
78
    public function getPhoto(GetPhotoRequest $getPhotoRequest) {
79
80
        $rawResponse = $this->callAPI($getPhotoRequest, []);
81
82
        $response = ResponseNormalizer::denormalizePhotoResponse($rawResponse);
83
84
        return $this->beforeReturnResponse($response);
85
    }
86
87
    /**
88
     * Get a video.
89
     *
90
     * @param GetVideoRequest $getVideoRequest The get video request.
91
     * @return VideoResponse Returns the video response.
92
     * @throws APIException Throws an API exception if an error occurs.
93
     */
94
    public function getVideo(GetVideoRequest $getVideoRequest) {
95
96
        $rawResponse = $this->callAPI($getVideoRequest, []);
97
98
        $response = ResponseNormalizer::denormalizeVideoResponse($rawResponse);
99
100
        return $this->beforeReturnResponse($response);
101
    }
102
103
    /**
104
     * Popular videos.
105
     *
106
     * @param PopularVideosRequest $popularVideosRequest The popular videos request.
107
     * @return VideosResponse Returns the videos response.
108
     * @throws APIException Throws an API exception if an error occurs.
109
     */
110
    public function popularVideos(PopularVideosRequest $popularVideosRequest) {
111
112
        $queryData = RequestNormalizer::normalizePopularVideosRequest($popularVideosRequest);
113
114
        $rawResponse = $this->callAPI($popularVideosRequest, $queryData);
115
116
        $response = ResponseNormalizer::denormalizeVideosResponse($rawResponse);
117
118
        return $this->beforeReturnResponse($response);
119
    }
120
121
    /**
122
     * Search photos.
123
     *
124
     * @param SearchPhotosRequest $searchPhotosRequest The search photos request.
125
     * @return PhotosResponse Returns the photo response.
126
     * @throws APIException Throws an API exception if an error occurs.
127
     * @throws InvalidArgumentException Throws an invalid argument exception if a mandatory parameter is missing.
128
     */
129
    public function searchPhotos(SearchPhotosRequest $searchPhotosRequest) {
130
131
        $queryData = RequestNormalizer::normalizeSearchPhotosRequest($searchPhotosRequest);
132
133
        $rawResponse = $this->callAPI($searchPhotosRequest, $queryData);
134
135
        $response = ResponseNormalizer::denormalizePhotosResponse($rawResponse);
136
137
        return $this->beforeReturnResponse($response);
138
    }
139
140
    /**
141
     * Search videos.
142
     *
143
     * @param SearchVideosRequest $searchVideosRequest The search videos request.
144
     * @return VideosResponse Returns the videos response.
145
     * @throws APIException Throws an API exception if an error occurs.
146
     * @throws InvalidArgumentException Throws an invalid argument exception if a mandatory parameter is missing.
147
     */
148
    public function searchVideos(SearchVideosRequest $searchVideosRequest) {
149
150
        $queryData = RequestNormalizer::normalizeSearchVideosRequest($searchVideosRequest);
151
152
        $rawResponse = $this->callAPI($searchVideosRequest, $queryData);
153
154
        $response = ResponseNormalizer::denormalizeVideosResponse($rawResponse);
155
156
        return $this->beforeReturnResponse($response);
157
    }
158
}
159