1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api\Providers; |
4
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Api\Request; |
6
|
|
|
use seregazhuk\PinterestBot\Api\Response; |
7
|
|
|
use seregazhuk\PinterestBot\Helpers\UrlHelper; |
8
|
|
|
use seregazhuk\PinterestBot\Contracts\RequestInterface; |
9
|
|
|
use seregazhuk\PinterestBot\Contracts\ResponseInterface; |
10
|
|
|
use seregazhuk\PinterestBot\Helpers\Providers\Traits\ProviderTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Provider |
14
|
|
|
* |
15
|
|
|
* @package seregazhuk\PinterestBot\Contracts |
16
|
|
|
*/ |
17
|
|
|
abstract class Provider |
18
|
|
|
{ |
19
|
|
|
use ProviderTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* List of methods that require logged status |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $loginRequired = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Instance of the API RequestInterface |
29
|
|
|
* |
30
|
|
|
* @var RequestInterface |
31
|
|
|
*/ |
32
|
|
|
protected $request; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Instance of the API ResponseInterface |
36
|
|
|
* |
37
|
|
|
* @var ResponseInterface |
38
|
|
|
*/ |
39
|
|
|
protected $response; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param RequestInterface $request |
43
|
|
|
* @param ResponseInterface $response |
44
|
|
|
*/ |
45
|
|
|
public function __construct(RequestInterface $request, ResponseInterface $response) |
46
|
|
|
{ |
47
|
|
|
$this->request = $request; |
48
|
|
|
$this->response = $response; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Executes a POST request to Pinterest API |
53
|
|
|
* |
54
|
|
|
* @param array $requestOptions |
55
|
|
|
* @param string $resourceUrl |
56
|
|
|
* @param bool $returnData |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
|
|
public function callPostRequest($requestOptions, $resourceUrl, $returnData = null) |
60
|
|
|
{ |
61
|
|
|
$data = array("options" => $requestOptions); |
62
|
|
|
$request = Request::createRequestData($data); |
63
|
|
|
|
64
|
|
|
$postString = UrlHelper::buildRequestString($request); |
65
|
|
|
$response = $this->request->exec($resourceUrl, $postString); |
66
|
|
|
|
67
|
|
|
if ($returnData) { |
68
|
|
|
return $this->response->getData($response); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->response->checkResponse($response); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Run login check before every method if needed |
76
|
|
|
* @param $method |
77
|
|
|
* @param $arguments |
78
|
|
|
* @return mixed |
79
|
|
|
*/ |
80
|
|
|
public function __call($method, $arguments) |
81
|
|
|
{ |
82
|
|
|
|
83
|
|
|
if (method_exists($this, $method)) { |
84
|
|
|
$this->checkMethodForLoginNeed($method); |
85
|
|
|
|
86
|
|
|
return call_user_func_array(array($this, $method), $arguments); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Checks if method requires login |
92
|
|
|
* |
93
|
|
|
* @param $method |
94
|
|
|
*/ |
95
|
|
|
protected function checkMethodForLoginNeed($method) |
96
|
|
|
{ |
97
|
|
|
if (in_array($method, $this->loginRequired)) { |
98
|
|
|
$this->request->checkLoggedIn(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return Request |
105
|
|
|
*/ |
106
|
|
|
protected function getRequest() |
107
|
|
|
{ |
108
|
|
|
return $this->request; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return Response |
113
|
|
|
*/ |
114
|
|
|
protected function getResponse() |
115
|
|
|
{ |
116
|
|
|
return $this->response; |
117
|
|
|
} |
118
|
|
|
} |