1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api\Providers; |
4
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Api\Request; |
6
|
|
|
use seregazhuk\PinterestBot\Contracts\RequestInterface; |
7
|
|
|
use seregazhuk\PinterestBot\Contracts\ResponseInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Provider. |
11
|
|
|
*/ |
12
|
|
|
abstract class Provider |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* List of methods that require logged status. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $loginRequiredFor = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Instance of the API RequestInterface. |
23
|
|
|
* |
24
|
|
|
* @var RequestInterface |
25
|
|
|
*/ |
26
|
|
|
protected $request; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Instance of the API ResponseInterface. |
30
|
|
|
* |
31
|
|
|
* @var ResponseInterface |
32
|
|
|
*/ |
33
|
|
|
protected $response; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param RequestInterface $request |
37
|
|
|
* @param ResponseInterface $response |
38
|
|
|
*/ |
39
|
|
|
public function __construct(RequestInterface $request, ResponseInterface $response) |
40
|
|
|
{ |
41
|
|
|
$this->request = $request; |
42
|
|
|
$this->response = $response; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Executes a POST request to Pinterest API. |
47
|
|
|
* |
48
|
|
|
* @param array $requestOptions |
49
|
|
|
* @param string $resourceUrl |
50
|
|
|
* @param bool $returnData |
51
|
|
|
* |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
protected function execPostRequest($requestOptions, $resourceUrl, $returnData = false) |
55
|
|
|
{ |
56
|
|
|
$postString = Request::createQuery(['options' => $requestOptions]); |
57
|
|
|
$response = $this->request->exec($resourceUrl, $postString); |
58
|
|
|
|
59
|
|
|
if ($returnData) { |
60
|
|
|
return $this->response->getData($response); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->response->hasErrors($response); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Executes a GET request to Pinterest API. |
68
|
|
|
* |
69
|
|
|
* @param array $requestOptions |
70
|
|
|
* @param string $resourceUrl |
71
|
|
|
* @return array|bool |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
protected function execGetRequest(array $requestOptions, $resourceUrl) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$query = Request::createQuery(['options' => $requestOptions]); |
76
|
|
|
$response = $this->request->exec($resourceUrl . "?{$query}"); |
77
|
|
|
|
78
|
|
|
return $this->response->getData($response); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Executes a GET request to Pinterest API with pagination. |
83
|
|
|
* |
84
|
|
|
* @param array $requestOptions |
85
|
|
|
* @param string $resourceUrl |
86
|
|
|
* @param array $bookmarks |
87
|
|
|
* @return array|bool |
88
|
|
|
*/ |
89
|
|
View Code Duplication |
protected function execGetRequestWithPagination(array $requestOptions, $resourceUrl, $bookmarks = []) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$query = Request::createQuery(['options' => $requestOptions], $bookmarks); |
92
|
|
|
$response = $this->request->exec($resourceUrl . "?{$query}"); |
93
|
|
|
|
94
|
|
|
return $this->response->getPaginationData($response); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function getEntityIdName() |
98
|
|
|
{ |
99
|
|
|
return property_exists($this, 'entityIdName') ? $this->entityIdName : ''; |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Executes pagination GET request. |
104
|
|
|
* |
105
|
|
|
* @param array $data |
106
|
|
|
* @param string $url |
107
|
|
|
* @param array $bookmarks |
108
|
|
|
* @return array|bool |
109
|
|
|
*/ |
110
|
|
|
public function getPaginatedData(array $data, $url, $bookmarks = []) |
111
|
|
|
{ |
112
|
|
|
return $this->execGetRequestWithPagination($data, $url, $bookmarks); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $method |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
public function checkMethodRequiresLogin($method) |
121
|
|
|
{ |
122
|
|
|
return in_array($method, $this->loginRequiredFor); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return RequestInterface |
127
|
|
|
*/ |
128
|
|
|
public function getRequest() |
129
|
|
|
{ |
130
|
|
|
return $this->request; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return ResponseInterface |
135
|
|
|
*/ |
136
|
|
|
public function getResponse() |
137
|
|
|
{ |
138
|
|
|
return $this->response; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.