1 | <?php |
||
2 | |||
3 | namespace seregazhuk\PinterestBot\Api\Providers; |
||
4 | |||
5 | use seregazhuk\PinterestBot\Api\Response; |
||
6 | use seregazhuk\PinterestBot\Api\Forms\Profile; |
||
7 | use seregazhuk\PinterestBot\Api\Traits\HasProfileSettings; |
||
8 | use seregazhuk\PinterestBot\Helpers\UrlBuilder; |
||
9 | use seregazhuk\PinterestBot\Api\Traits\UploadsImages; |
||
10 | use seregazhuk\PinterestBot\Api\Providers\Core\Provider; |
||
11 | |||
12 | class User extends Provider |
||
13 | { |
||
14 | use UploadsImages, HasProfileSettings; |
||
15 | |||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $loginRequiredFor = [ |
||
20 | 'id', |
||
21 | 'invite', |
||
22 | 'profile', |
||
23 | 'username', |
||
24 | 'isBanned', |
||
25 | 'deactivate', |
||
26 | 'sessionsHistory', |
||
27 | 'convertToBusiness', |
||
28 | ]; |
||
29 | |||
30 | /** |
||
31 | * Updates or returns user profile info. Gets associative array as a param. Available keys of array are: |
||
32 | * 'last_name', 'first_name', 'username', 'about', 'location' and 'website_url'. |
||
33 | * You can also change user avatar by passing 'profile_image'. |
||
34 | * |
||
35 | * @param array $userInfo If empty returns current user profile. |
||
36 | * |
||
37 | * @return bool|array|Profile |
||
38 | */ |
||
39 | public function profile($userInfo = null) |
||
40 | { |
||
41 | // If we call method without params, return current user profile data. |
||
42 | if (empty($userInfo)) { |
||
43 | return $this->getProfile(); |
||
44 | } |
||
45 | |||
46 | return $this->updateProfile($userInfo); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Checks if current user is banned |
||
51 | * |
||
52 | * @return bool |
||
53 | */ |
||
54 | public function isBanned() |
||
55 | { |
||
56 | return (bool)$this->getProfileData('is_write_banned'); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Returns current user username |
||
61 | * |
||
62 | * @return string |
||
63 | */ |
||
64 | public function username() |
||
65 | { |
||
66 | return $this->getProfileData('username'); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Returns current user id |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | public function id() |
||
75 | { |
||
76 | return $this->getProfileData('id'); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Deactivates your account. |
||
81 | * |
||
82 | * @param string $reason |
||
83 | * @param string $explanation |
||
84 | * @return bool |
||
85 | */ |
||
86 | public function deactivate($reason = 'other', $explanation = '') |
||
87 | { |
||
88 | $userId = $this->id(); |
||
89 | |||
90 | if ($userId === null) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
91 | return false; |
||
92 | } |
||
93 | |||
94 | $request = [ |
||
95 | 'user_id' => $userId, |
||
96 | 'reason' => $reason, |
||
97 | 'explanation' => $explanation, |
||
98 | ]; |
||
99 | |||
100 | return $this->post(UrlBuilder::RESOURCE_DEACTIVATE_ACCOUNT, $request); |
||
0 ignored issues
–
show
|
|||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Send invite to email |
||
105 | * @param string $email |
||
106 | * @return bool|Response |
||
107 | */ |
||
108 | public function invite($email) |
||
109 | { |
||
110 | $data = [ |
||
111 | 'email' => $email, |
||
112 | 'type' => 'email', |
||
113 | ]; |
||
114 | |||
115 | return $this->post(UrlBuilder::RESOURCE_INVITE, $data); |
||
0 ignored issues
–
show
|
|||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Remove things you’ve recently searched for from search suggestions. |
||
120 | * @return bool|Response |
||
121 | */ |
||
122 | public function clearSearchHistory() |
||
123 | { |
||
124 | return $this->post(UrlBuilder::RESOURCE_CLEAR_SEARCH_HISTORY); |
||
0 ignored issues
–
show
|
|||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Simply makes GET request to some url. |
||
129 | * @param string $url |
||
130 | * @return array|bool |
||
131 | */ |
||
132 | public function visitPage($url = '') |
||
133 | { |
||
134 | return $this->get($url); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Get your account sessions history |
||
139 | * @return array |
||
140 | */ |
||
141 | public function sessionsHistory() |
||
142 | { |
||
143 | return $this->get(UrlBuilder::RESOURCE_SESSIONS_HISTORY); |
||
0 ignored issues
–
show
|
|||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @return array |
||
148 | */ |
||
149 | protected function getProfile() |
||
150 | { |
||
151 | return $this->get(UrlBuilder::RESOURCE_GET_USER_SETTINGS); |
||
0 ignored issues
–
show
|
|||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @param array|Profile $userInfo |
||
156 | * @return bool|Response |
||
157 | */ |
||
158 | protected function updateProfile($userInfo) |
||
159 | { |
||
160 | // If we have a form object, convert it to array |
||
161 | if ($userInfo instanceof Profile) { |
||
162 | $userInfo = $userInfo->toArray(); |
||
163 | } |
||
164 | |||
165 | if (isset($userInfo['profile_image'])) { |
||
166 | $userInfo['profile_image_url'] = $this->upload($userInfo['profile_image']); |
||
167 | } |
||
168 | |||
169 | return $this->post(UrlBuilder::RESOURCE_UPDATE_USER_SETTINGS, $userInfo); |
||
0 ignored issues
–
show
|
|||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @param string $key |
||
174 | * @return mixed|string |
||
175 | */ |
||
176 | protected function getProfileData($key) |
||
177 | { |
||
178 | $profile = $this->getProfile(); |
||
179 | |||
180 | return $profile[$key] ?? ''; |
||
181 | } |
||
182 | } |
||
183 |