1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api\Providers; |
4
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Helpers\UrlHelper; |
6
|
|
|
use seregazhuk\PinterestBot\Exceptions\AuthException; |
7
|
|
|
use seregazhuk\PinterestBot\Api\Traits\UploadsImages; |
8
|
|
|
use seregazhuk\PinterestBot\Helpers\Requests\PinnerHelper; |
9
|
|
|
|
10
|
|
|
class User extends Provider |
11
|
|
|
{ |
12
|
|
|
use UploadsImages; |
13
|
|
|
|
14
|
|
|
protected $loginRequiredFor = ['profile']; |
15
|
|
|
|
16
|
|
|
const REGISTRATION_COMPLETE_EXPERIENCE_ID = '11:10105'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Update user profile info. Gets associative array as a param. Available keys of array are: |
20
|
|
|
* 'last_name', 'first_name', 'username', 'about', 'location' and 'website_url'. |
21
|
|
|
* You can also change user avatar by passing 'profile_image'. |
22
|
|
|
* |
23
|
|
|
* @param array $userInfo |
24
|
|
|
* |
25
|
|
|
* @return mixed |
26
|
|
|
*/ |
27
|
|
|
public function profile($userInfo) |
28
|
|
|
{ |
29
|
|
|
if (isset($userInfo['profile_image'])) { |
30
|
|
|
$userInfo['profile_image_url'] = $this->upload($userInfo['profile_image']); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $this->execPostRequest($userInfo, UrlHelper::RESOURCE_UPDATE_USER_SETTINGS); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Register a new user. |
38
|
|
|
* |
39
|
|
|
* @param string $email |
40
|
|
|
* @param string $password |
41
|
|
|
* @param string $name |
42
|
|
|
* @param string $county |
43
|
|
|
* @param int $age |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
public function register($email, $password, $name, $county = "UK", $age = 18) |
47
|
|
|
{ |
48
|
|
|
$this->execGetRequest([], ''); |
49
|
|
|
$this->request->setTokenFromCookies(); |
50
|
|
|
|
51
|
|
|
$data = [ |
52
|
|
|
"age" => $age, |
53
|
|
|
"email" => $email, |
54
|
|
|
"password" => $password, |
55
|
|
|
"country" => $county, |
56
|
|
|
"first_name" => $name, |
57
|
|
|
"container" => "home_page" |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
if (!$this->execPostRequest($data, UrlHelper::RESOURCE_CREATE_REGISTER)) { |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->completeRegistration(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Login as pinner. |
69
|
|
|
* |
70
|
|
|
* @param string $username |
71
|
|
|
* @param string $password |
72
|
|
|
* |
73
|
|
|
* @throws AuthException |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function login($username, $password) |
78
|
|
|
{ |
79
|
|
|
if ($this->request->isLoggedIn()) { |
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->checkCredentials($username, $password); |
84
|
|
|
|
85
|
|
|
$postString = PinnerHelper::createLoginQuery($username, $password); |
86
|
|
|
$this->request->clearToken(); |
87
|
|
|
|
88
|
|
|
$response = $this->request->exec(UrlHelper::RESOURCE_LOGIN, $postString); |
89
|
|
|
if ($this->response->hasErrors($response)) { |
90
|
|
|
throw new AuthException($this->response->getLastError()['message']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->request->login(); |
94
|
|
|
|
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function logout() |
99
|
|
|
{ |
100
|
|
|
$this->request->logout(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function isLoggedIn() |
104
|
|
|
{ |
105
|
|
|
return $this->request->isLoggedIn(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Validates password and login. |
110
|
|
|
* |
111
|
|
|
* @param string $username |
112
|
|
|
* @param string $password |
113
|
|
|
*/ |
114
|
|
|
protected function checkCredentials($username, $password) |
115
|
|
|
{ |
116
|
|
|
if (!$username || !$password) { |
117
|
|
|
throw new \LogicException('You must set username and password to login.'); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function completeRegistration() |
122
|
|
|
{ |
123
|
|
|
$this->request->setTokenFromCookies(); |
124
|
|
|
|
125
|
|
|
return $this->execPostRequest( |
126
|
|
|
['placed_experience_id' => self::REGISTRATION_COMPLETE_EXPERIENCE_ID], UrlHelper::RESOURCE_REGISTRATION_COMPLETE |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|