seregazhuk /
php-pinterest-bot
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace seregazhuk\PinterestBot\Api\Providers; |
||||
| 4 | |||||
| 5 | use LogicException; |
||||
| 6 | use seregazhuk\PinterestBot\Helpers\UrlBuilder; |
||||
| 7 | use seregazhuk\PinterestBot\Api\Forms\Registration; |
||||
| 8 | use seregazhuk\PinterestBot\Api\Providers\Core\Provider; |
||||
| 9 | use seregazhuk\PinterestBot\Api\Traits\ResolvesCurrentUser; |
||||
| 10 | use seregazhuk\PinterestBot\Api\Traits\SendsRegisterActions; |
||||
| 11 | |||||
| 12 | class Auth extends Provider |
||||
| 13 | { |
||||
| 14 | use SendsRegisterActions, ResolvesCurrentUser; |
||||
| 15 | |||||
| 16 | /** |
||||
| 17 | * @var array |
||||
| 18 | */ |
||||
| 19 | protected $loginRequiredFor = [ |
||||
| 20 | 'logout', |
||||
| 21 | 'convertToBusiness', |
||||
| 22 | ]; |
||||
| 23 | |||||
| 24 | const REGISTRATION_COMPLETE_EXPERIENCE_ID = '11:10105'; |
||||
| 25 | |||||
| 26 | /** |
||||
| 27 | * Login as pinner. |
||||
| 28 | * |
||||
| 29 | * @param string $username |
||||
| 30 | * @param string $password |
||||
| 31 | * @param bool $autoLogin |
||||
| 32 | * @return bool |
||||
| 33 | */ |
||||
| 34 | public function login($username, $password, $autoLogin = true) |
||||
| 35 | { |
||||
| 36 | if ($this->isLoggedIn()) { |
||||
| 37 | return true; |
||||
| 38 | } |
||||
| 39 | |||||
| 40 | $this->checkCredentials($username, $password); |
||||
| 41 | |||||
| 42 | // Trying to load previously saved cookies from last login session for this username. |
||||
| 43 | // Then grab user profile info to check, if cookies are ok. If an empty response |
||||
| 44 | // was returned, then send login request. |
||||
| 45 | if ($autoLogin && $this->processAutoLogin($username)) { |
||||
| 46 | return true; |
||||
| 47 | } |
||||
| 48 | |||||
| 49 | return $this->processLogin($username, $password); |
||||
| 50 | } |
||||
| 51 | |||||
| 52 | public function logout() |
||||
| 53 | { |
||||
| 54 | $this->request->logout(); |
||||
| 55 | } |
||||
| 56 | |||||
| 57 | /** |
||||
| 58 | * Register a new user. |
||||
| 59 | * |
||||
| 60 | * @param string|Registration $email |
||||
| 61 | * @param string|null $password |
||||
| 62 | * @param string|null $name |
||||
| 63 | * @param string $country @deprecated |
||||
| 64 | * @param int $age @deprecated |
||||
| 65 | * |
||||
| 66 | * @return bool |
||||
| 67 | */ |
||||
| 68 | public function register($email, $password = null, $name = null, $country = 'GB', $age = 18) |
||||
| 69 | { |
||||
| 70 | $registrationForm = $this->getRegistrationForm($email, $password, $name, $country, $age); |
||||
| 71 | |||||
| 72 | return $this->makeRegisterCall($registrationForm); |
||||
| 73 | } |
||||
| 74 | |||||
| 75 | /** |
||||
| 76 | * Register a new business account. At first we register a basic type account. |
||||
| 77 | * Then convert it to a business one. This is done to receive a confirmation |
||||
| 78 | * email after registration. |
||||
| 79 | * |
||||
| 80 | * @param string|Registration $registrationForm |
||||
| 81 | * @param string $password |
||||
| 82 | * @param string $name |
||||
| 83 | * @param string $website |
||||
| 84 | * @return bool|mixed |
||||
| 85 | */ |
||||
| 86 | public function registerBusiness($registrationForm, $password = null, $name = null, $website = '') |
||||
| 87 | { |
||||
| 88 | $registration = $this->register($registrationForm, $password, $name); |
||||
|
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||||
| 89 | |||||
| 90 | if (!$registration) { |
||||
| 91 | return false; |
||||
| 92 | } |
||||
| 93 | |||||
| 94 | $website = ($registrationForm instanceof Registration) ? |
||||
| 95 | $registrationForm->getSite() : $website; |
||||
| 96 | |||||
| 97 | return $this->convertToBusiness($name, $website); |
||||
| 98 | } |
||||
| 99 | |||||
| 100 | /** |
||||
| 101 | * Convert your account to a business one. |
||||
| 102 | * |
||||
| 103 | * @param string $businessName |
||||
| 104 | * @param string $websiteUrl |
||||
| 105 | * @return bool |
||||
| 106 | */ |
||||
| 107 | public function convertToBusiness($businessName, $websiteUrl = '') |
||||
| 108 | { |
||||
| 109 | $data = [ |
||||
| 110 | 'business_name' => $businessName, |
||||
| 111 | 'website_url' => $websiteUrl, |
||||
| 112 | 'account_type' => 'other', |
||||
| 113 | ]; |
||||
| 114 | |||||
| 115 | return $this->post(UrlBuilder::RESOURCE_CONVERT_TO_BUSINESS, $data); |
||||
|
0 ignored issues
–
show
|
|||||
| 116 | } |
||||
| 117 | |||||
| 118 | /** |
||||
| 119 | * @param string $link |
||||
| 120 | * @return array|bool |
||||
| 121 | */ |
||||
| 122 | public function confirmEmail($link) |
||||
| 123 | { |
||||
| 124 | return $this->get($link); |
||||
| 125 | } |
||||
| 126 | |||||
| 127 | /** |
||||
| 128 | * Validates password and login. |
||||
| 129 | * |
||||
| 130 | * @param string $username |
||||
| 131 | * @param string $password |
||||
| 132 | * @throws LogicException |
||||
| 133 | */ |
||||
| 134 | protected function checkCredentials($username, $password) |
||||
| 135 | { |
||||
| 136 | if (!$username || !$password) { |
||||
| 137 | throw new LogicException('You must set username and password to login.'); |
||||
| 138 | } |
||||
| 139 | } |
||||
| 140 | |||||
| 141 | /** |
||||
| 142 | * @return bool |
||||
| 143 | */ |
||||
| 144 | protected function completeRegistration() |
||||
| 145 | { |
||||
| 146 | return $this->post( |
||||
|
0 ignored issues
–
show
|
|||||
| 147 | UrlBuilder::RESOURCE_REGISTRATION_COMPLETE, |
||||
| 148 | ['placed_experience_id' => self::REGISTRATION_COMPLETE_EXPERIENCE_ID] |
||||
| 149 | ); |
||||
| 150 | } |
||||
| 151 | |||||
| 152 | /** |
||||
| 153 | * @param Registration $registrationForm |
||||
| 154 | * @return bool|mixed |
||||
| 155 | */ |
||||
| 156 | protected function makeRegisterCall(Registration $registrationForm) |
||||
| 157 | { |
||||
| 158 | if (!$this->sendEmailVerificationAction()) { |
||||
| 159 | return false; |
||||
| 160 | } |
||||
| 161 | |||||
| 162 | if (!$this->post(UrlBuilder::RESOURCE_CREATE_REGISTER, $registrationForm->toArray())) { |
||||
| 163 | return false; |
||||
| 164 | } |
||||
| 165 | |||||
| 166 | if (!$this->sendRegistrationActions()) { |
||||
| 167 | return false; |
||||
| 168 | } |
||||
| 169 | |||||
| 170 | return $this->completeRegistration(); |
||||
| 171 | } |
||||
| 172 | |||||
| 173 | /** |
||||
| 174 | * @param string $username |
||||
| 175 | * @param string $password |
||||
| 176 | * @return bool |
||||
| 177 | */ |
||||
| 178 | protected function processLogin($username, $password) |
||||
| 179 | { |
||||
| 180 | $this->request->loadCookiesFor($username); |
||||
| 181 | |||||
| 182 | $credentials = [ |
||||
| 183 | 'username_or_email' => $username, |
||||
| 184 | 'password' => $password, |
||||
| 185 | ]; |
||||
| 186 | |||||
| 187 | $this->post(UrlBuilder::RESOURCE_LOGIN, $credentials); |
||||
| 188 | |||||
| 189 | if ($this->response->isEmpty()) { |
||||
| 190 | return false; |
||||
| 191 | } |
||||
| 192 | |||||
| 193 | $this->request->login(); |
||||
| 194 | |||||
| 195 | return true; |
||||
| 196 | } |
||||
| 197 | |||||
| 198 | /** |
||||
| 199 | * @param string $username |
||||
| 200 | * @return bool |
||||
| 201 | */ |
||||
| 202 | protected function processAutoLogin($username) |
||||
| 203 | { |
||||
| 204 | return $this->request->autoLogin($username) && $this->resolveCurrentUserId(); |
||||
| 205 | } |
||||
| 206 | |||||
| 207 | /** |
||||
| 208 | * @param string $registrationForm |
||||
| 209 | * @param string $password |
||||
| 210 | * @param string $name |
||||
| 211 | * @param string $country |
||||
| 212 | * @param string $age |
||||
| 213 | * @return Registration |
||||
| 214 | */ |
||||
| 215 | protected function fillRegistrationForm($registrationForm, $password, $name, $country, $age) |
||||
| 216 | { |
||||
| 217 | return (new Registration($registrationForm, $password, $name)) |
||||
| 218 | ->setCountry($country) |
||||
| 219 | ->setAge($age) |
||||
|
0 ignored issues
–
show
$age of type string is incompatible with the type integer expected by parameter $age of seregazhuk\PinterestBot\...\Registration::setAge().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 220 | ->setGender('male'); |
||||
| 221 | } |
||||
| 222 | |||||
| 223 | /** |
||||
| 224 | * @param string|Registration $email |
||||
| 225 | * @param string $password |
||||
| 226 | * @param string $name |
||||
| 227 | * @param string $country |
||||
| 228 | * @param string $age |
||||
| 229 | * @return Registration |
||||
| 230 | */ |
||||
| 231 | protected function getRegistrationForm($email, $password, $name, $country, $age) |
||||
| 232 | { |
||||
| 233 | if ($email instanceof Registration) { |
||||
| 234 | return $email; |
||||
| 235 | } |
||||
| 236 | |||||
| 237 | return $this->fillRegistrationForm( |
||||
| 238 | $email, $password, $name, $country, $age |
||||
| 239 | ); |
||||
| 240 | } |
||||
| 241 | } |
||||
| 242 |