|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Exceptions\AuthException; |
|
6
|
|
|
|
|
7
|
|
|
class PinnerHelper extends RequestHelper |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Checks Pinterest API pinners response, and parses data |
|
11
|
|
|
* with bookmarks info from it. |
|
12
|
|
|
* |
|
13
|
|
|
* @param array $res |
|
14
|
|
|
* @return array |
|
15
|
|
|
*/ |
|
16
|
|
|
public static function checkUserDataResponse($res) |
|
17
|
|
|
{ |
|
18
|
|
|
if ($res === null) { |
|
19
|
|
|
return []; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
$bookmarks = self::_getBookmarksFromResponse($res); |
|
23
|
|
|
if ($data = self::_getDataFromResponse($res)) { |
|
24
|
|
|
return ['data' => $data, 'bookmarks' => $bookmarks]; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
return []; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Creates Pinterest API request to get user info according to |
|
32
|
|
|
* username, API url and bookmarks for pagination |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $username |
|
35
|
|
|
* @param string $sourceUrl |
|
36
|
|
|
* @param array $bookmarks |
|
37
|
|
|
* @return array |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function createUserDataRequest($username, $sourceUrl, $bookmarks) |
|
40
|
|
|
{ |
|
41
|
|
|
$dataJson = self::createPinnerRequestData($username); |
|
42
|
|
|
|
|
43
|
|
|
if ( ! empty($bookmarks)) { |
|
44
|
|
|
$dataJson["options"]["bookmarks"] = $bookmarks; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return self::createRequestData($dataJson, $sourceUrl); |
|
48
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Parses Pinterest API response with pinner name |
|
53
|
|
|
* |
|
54
|
|
|
* @param $res |
|
55
|
|
|
* @return null |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function parseAccountNameResponse($res) |
|
58
|
|
|
{ |
|
59
|
|
|
if (isset($res['resource_data_cache'][1]['resource']['options']['username'])) { |
|
60
|
|
|
return $res['resource_data_cache'][1]['resource']['options']['username']; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Creates Pinterest API request to login |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $username |
|
70
|
|
|
* @param string $password |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function createLoginRequest($username, $password) |
|
74
|
|
|
{ |
|
75
|
|
|
$dataJson = [ |
|
76
|
|
|
"options" => [ |
|
77
|
|
|
"username_or_email" => $username, |
|
78
|
|
|
"password" => $password |
|
79
|
|
|
], |
|
80
|
|
|
"context" => new \stdClass(), |
|
81
|
|
|
]; |
|
82
|
|
|
return self::createRequestData($dataJson, "/login/"); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Parses Pintrest Api response after login |
|
87
|
|
|
* |
|
88
|
|
|
* @param array $res |
|
89
|
|
|
* @return bool |
|
90
|
|
|
* @throws AuthException |
|
91
|
|
|
*/ |
|
92
|
|
|
public static function parseLoginResponse($res) |
|
93
|
|
|
{ |
|
94
|
|
|
if (isset($res['resource_response']['error'])) { |
|
95
|
|
|
throw new AuthException($res['resource_response']['error']['message']); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return true; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Creates common Pinner request data by username |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $username |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
protected static function createPinnerRequestData($username) |
|
108
|
|
|
{ |
|
109
|
|
|
return [ |
|
110
|
|
|
"options" => [ |
|
111
|
|
|
"username" => $username, |
|
112
|
|
|
], |
|
113
|
|
|
"context" => new \stdClass(), |
|
114
|
|
|
]; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Parse bookmarks from response |
|
119
|
|
|
* @param array $response |
|
120
|
|
|
* @return string|null |
|
121
|
|
|
*/ |
|
122
|
|
|
protected static function _getBookmarksFromResponse($response) |
|
123
|
|
|
{ |
|
124
|
|
|
if (isset($response['resource']['options']['bookmarks'][0])) { |
|
125
|
|
|
return [$response['resource']['options']['bookmarks'][0]]; |
|
126
|
|
|
} |
|
127
|
|
|
return null; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Parse data from response |
|
132
|
|
|
* @param $response |
|
133
|
|
|
* @return array|null |
|
134
|
|
|
*/ |
|
135
|
|
View Code Duplication |
protected static function _getDataFromResponse($response) |
|
|
|
|
|
|
136
|
|
|
{ |
|
137
|
|
|
if (isset($response['resource_response']['data'])) { |
|
138
|
|
|
return $response['resource_response']['data']; |
|
139
|
|
|
} |
|
140
|
|
|
return null; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
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.