diff --git a/src/Api/Forms/Registration.php b/src/Api/Forms/Registration.php index 18a30e66..82315528 100644 --- a/src/Api/Forms/Registration.php +++ b/src/Api/Forms/Registration.php @@ -52,115 +52,143 @@ public function __construct($email, $password, $name) } /** - * @param mixed $email + * @param string $country * @return Registration */ - public function setEmail($email) + public function setCountry($country) { - $this->email = $email; + $this->country = $country; return $this; } /** - * @param string $password + * @param int $age * @return Registration */ - public function setPassword($password) + public function setAge($age) { - $this->password = $password; + // Pinterest requires age to be a string + $this->age = (string)$age; return $this; } /** - * @param string $name * @return Registration */ - public function setName($name) + public function setMaleGender() { - $this->name = $name; - return $this; + return $this->setGender('male'); } /** - * @param string $country + * @param string $gender * @return Registration */ - public function setCountry($country) + public function setGender($gender) { - $this->country = $country; + $this->gender = $gender; return $this; } /** - * @param int $age * @return Registration */ - public function setAge($age) + public function setFemaleGender() { - // Pinterest requires age to be a string - $this->age = (string)$age; - return $this; + return $this->setGender('female'); } /** - * @param string $gender + * @return array + */ + public function getData() + { + return [ + 'first_name' => $this->name, + 'email' => $this->email, + 'password' => $this->password, + 'age' => $this->age, + 'gender' => $this->gender, + 'country' => $this->country, + 'site' => $this->site, + 'container' => 'home_page', + 'signupSource' => 'home_page', + 'hybridTier' => 'open', + 'page' => 'home', + 'recaptchaV3Token' => '', + 'user_behavior_data' => "{}", + ]; + } + + /** + * @return string + */ + public function getSite() + { + return $this->site; + } + + /** + * @param string $site * @return Registration */ - public function setGender($gender) + public function setSite($site) { - $this->gender = $gender; + $this->site = $site; return $this; } /** - * @return Registration + * @return string */ - public function setMaleGender() + public function getName(): string { - return $this->setGender('male'); + return $this->name; } /** + * @param string $name * @return Registration */ - public function setFemaleGender() + public function setName($name) { - return $this->setGender('female'); + $this->name = $name; + return $this; } /** - * @return array + * @return string */ - public function getData() + public function getEmail(): string { - return [ - 'first_name' => $this->name, - 'email' => $this->email, - 'password' => $this->password, - 'age' => $this->age, - 'gender' => $this->gender, - 'country' => $this->country, - 'site' => $this->site, - 'container' => 'home_page', - 'visited_pages' => [], - ]; + return $this->email; } /** - * @param string $site + * @param mixed $email * @return Registration */ - public function setSite($site) + public function setEmail($email) { - $this->site = $site; + $this->email = $email; return $this; } /** * @return string */ - public function getSite() + public function getPassword(): string { - return $this->site; + return $this->password; + } + + /** + * @param string $password + * @return Registration + */ + public function setPassword($password) + { + $this->password = $password; + return $this; } } diff --git a/src/Api/Providers/Auth.php b/src/Api/Providers/Auth.php index 1adda7cc..d081269e 100644 --- a/src/Api/Providers/Auth.php +++ b/src/Api/Providers/Auth.php @@ -3,16 +3,17 @@ namespace seregazhuk\PinterestBot\Api\Providers; use LogicException; -use seregazhuk\PinterestBot\Helpers\UrlBuilder; use seregazhuk\PinterestBot\Api\Forms\Registration; use seregazhuk\PinterestBot\Api\Providers\Core\Provider; use seregazhuk\PinterestBot\Api\Traits\ResolvesCurrentUser; use seregazhuk\PinterestBot\Api\Traits\SendsRegisterActions; +use seregazhuk\PinterestBot\Helpers\UrlBuilder; class Auth extends Provider { use SendsRegisterActions, ResolvesCurrentUser; + const REGISTRATION_COMPLETE_EXPERIENCE_ID = '11:10105'; /** * @var array */ @@ -21,37 +22,44 @@ class Auth extends Provider 'convertToBusiness', ]; - const REGISTRATION_COMPLETE_EXPERIENCE_ID = '11:10105'; + public function logout() + { + $this->request->logout(); + } /** - * Login as pinner. + * Register a new business account. At first we register a basic type account. + * Then convert it to a business one. This is done to receive a confirmation + * email after registration. * - * @param string $username + * @param string|Registration $registrationForm * @param string $password - * @param bool $autoLogin - * @return bool + * @param string $name + * @param string $website + * @return bool|mixed */ - public function login($username, $password, $autoLogin = true) + public function registerBusiness($registrationForm, $password = null, $name = null, $website = '') { - if ($this->isLoggedIn()) { - return true; - } + $registration = $this->register($registrationForm, $password, $name); - $this->checkCredentials($username, $password); + if (!$registration) { + return false; + } - // Trying to load previously saved cookies from last login session for this username. - // Then grab user profile info to check, if cookies are ok. If an empty response - // was returned, then send login request. - if ($autoLogin && $this->processAutoLogin($username)) { - return true; + // Re-Login + $email = ($registrationForm instanceof Registration) ? $registrationForm->getEmail() : $registrationForm; + $password = ($registrationForm instanceof Registration) ? $registrationForm->getPassword() : $password; + if (!$this->login($email, $password)) { + return false; } - return $this->processLogin($username, $password); - } + $website = ($registrationForm instanceof Registration) ? + $registrationForm->getSite() : $website; - public function logout() - { - $this->request->logout(); + $name = ($registrationForm instanceof Registration) ? + $registrationForm->getName() : $name; + + return $this->convertToBusiness($name, $website); } /** @@ -73,55 +81,96 @@ public function register($email, $password = null, $name = null, $country = 'GB' } /** - * Register a new business account. At first we register a basic type account. - * Then convert it to a business one. This is done to receive a confirmation - * email after registration. - * - * @param string|Registration $registrationForm + * @param string|Registration $email * @param string $password * @param string $name - * @param string $website + * @param string $country + * @param string $age + * @return Registration + */ + protected function getRegistrationForm($email, $password, $name, $country, $age) + { + if ($email instanceof Registration) { + return $email; + } + + return $this->fillRegistrationForm( + $email, $password, $name, $country, $age + ); + } + + /** + * @param string $registrationForm + * @param string $password + * @param string $name + * @param string $country + * @param string $age + * @return Registration + */ + protected function fillRegistrationForm($registrationForm, $password, $name, $country, $age) + { + return (new Registration($registrationForm, $password, $name)) + ->setCountry($country) + ->setAge($age) + ->setGender('male'); + } + + /** + * @param Registration $registrationForm * @return bool|mixed */ - public function registerBusiness($registrationForm, $password = null, $name = null, $website = '') + protected function makeRegisterCall(Registration $registrationForm) { - $registration = $this->register($registrationForm, $password, $name); + if (!$this->sendEmailVerificationAction()) { + return false; + } - if (!$registration) { + if (!$this->post(UrlBuilder::RESOURCE_CREATE_REGISTER, $registrationForm->toArray())) { return false; } - $website = ($registrationForm instanceof Registration) ? - $registrationForm->getSite() : $website; + if (!$this->sendRegistrationActions()) { + return false; + } - return $this->convertToBusiness($name, $website); + return $this->completeRegistration(); } /** - * Convert your account to a business one. - * - * @param string $businessName - * @param string $websiteUrl * @return bool */ - public function convertToBusiness($businessName, $websiteUrl = '') + protected function completeRegistration() { - $data = [ - 'business_name' => $businessName, - 'website_url' => $websiteUrl, - 'account_type' => 'other', - ]; - - return $this->post(UrlBuilder::RESOURCE_CONVERT_TO_BUSINESS, $data); + return $this->post( + UrlBuilder::RESOURCE_REGISTRATION_COMPLETE, + ['placed_experience_id' => self::REGISTRATION_COMPLETE_EXPERIENCE_ID] + ); } /** - * @param string $link - * @return array|bool + * Login as pinner. + * + * @param string $username + * @param string $password + * @param bool $autoLogin + * @return bool */ - public function confirmEmail($link) + public function login($username, $password, $autoLogin = true) { - return $this->get($link); + if ($this->isLoggedIn()) { + return true; + } + + $this->checkCredentials($username, $password); + + // Trying to load previously saved cookies from last login session for this username. + // Then grab user profile info to check, if cookies are ok. If an empty response + // was returned, then send login request. + if ($autoLogin && $this->processAutoLogin($username)) { + return true; + } + + return $this->processLogin($username, $password); } /** @@ -139,35 +188,12 @@ protected function checkCredentials($username, $password) } /** + * @param string $username * @return bool */ - protected function completeRegistration() - { - return $this->post( - UrlBuilder::RESOURCE_REGISTRATION_COMPLETE, - ['placed_experience_id' => self::REGISTRATION_COMPLETE_EXPERIENCE_ID] - ); - } - - /** - * @param Registration $registrationForm - * @return bool|mixed - */ - protected function makeRegisterCall(Registration $registrationForm) + protected function processAutoLogin($username) { - if (!$this->sendEmailVerificationAction()) { - return false; - } - - if (!$this->post(UrlBuilder::RESOURCE_CREATE_REGISTER, $registrationForm->toArray())) { - return false; - } - - if (!$this->sendRegistrationActions()) { - return false; - } - - return $this->completeRegistration(); + return $this->request->autoLogin($username) && $this->resolveCurrentUserId(); } /** @@ -196,46 +222,29 @@ protected function processLogin($username, $password) } /** - * @param string $username + * Convert your account to a business one. + * + * @param string $businessName + * @param string $websiteUrl * @return bool */ - protected function processAutoLogin($username) + public function convertToBusiness($businessName, $websiteUrl = '') { - return $this->request->autoLogin($username) && $this->resolveCurrentUserId(); - } + $data = [ + 'business_name' => $businessName, + 'website_url' => $websiteUrl, + 'has_ads_credits' => '', + ]; - /** - * @param string $registrationForm - * @param string $password - * @param string $name - * @param string $country - * @param string $age - * @return Registration - */ - protected function fillRegistrationForm($registrationForm, $password, $name, $country, $age) - { - return (new Registration($registrationForm, $password, $name)) - ->setCountry($country) - ->setAge($age) - ->setGender('male'); + return $this->post(UrlBuilder::RESOURCE_CONVERT_TO_BUSINESS, $data, false, '/business/convert/'); } /** - * @param string|Registration $email - * @param string $password - * @param string $name - * @param string $country - * @param string $age - * @return Registration + * @param string $link + * @return array|bool */ - protected function getRegistrationForm($email, $password, $name, $country, $age) + public function confirmEmail($link) { - if ($email instanceof Registration) { - return $email; - } - - return $this->fillRegistrationForm( - $email, $password, $name, $country, $age - ); + return $this->get($link); } } diff --git a/tests/Bot/Providers/AuthTest.php b/tests/Bot/Providers/AuthTest.php index b801a84e..43e296b6 100644 --- a/tests/Bot/Providers/AuthTest.php +++ b/tests/Bot/Providers/AuthTest.php @@ -19,9 +19,9 @@ public function it_converts_simple_account_to_a_business_one() $provider->convertToBusiness('myBusinessName', 'http://example.com'); $request = [ - 'business_name' => 'myBusinessName', - 'website_url' => 'http://example.com', - 'account_type' => 'other', + 'business_name' => 'myBusinessName', + 'website_url' => 'http://example.com', + 'has_ads_credits' => '', ]; $this->assertWasPostRequest(UrlBuilder::RESOURCE_CONVERT_TO_BUSINESS, $request); diff --git a/tests/Bot/Providers/RegistrationTest.php b/tests/Bot/Providers/RegistrationTest.php index 45e49244..1c3714b8 100644 --- a/tests/Bot/Providers/RegistrationTest.php +++ b/tests/Bot/Providers/RegistrationTest.php @@ -25,13 +25,17 @@ public function it_creates_a_form_from_plain_params() /** @test */ public function it_converts_account_to_business_type_when_registering_a_business_account() { + $this->request + ->shouldReceive('isLoggedIn') + ->andReturn(true); + $provider = $this->getProvider(); $provider->registerBusiness('johndoe@example.com', 'secret', 'johnDoe'); $this->assertWasPostRequest(UrlBuilder::RESOURCE_CONVERT_TO_BUSINESS, [ - 'business_name' => 'johnDoe', - 'website_url' => '', - 'account_type' => 'other', + 'business_name' => 'johnDoe', + 'website_url' => '', + 'has_ads_credits' => '', ]); }