Completed
Pull Request — master (#226)
by Sergey
02:25
created

Auth::visitMainPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use seregazhuk\PinterestBot\Api\Response;
6
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
7
8
class Auth extends Provider
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $loginRequiredFor = [
14
        'logout'
15
    ];
16
17
    const REGISTRATION_COMPLETE_EXPERIENCE_ID = '11:10105';
18
    const ACCOUNT_TYPE_OTHER = 'other';
19
20
    /**
21
     * Login as pinner.
22
     *
23
     * @param string $username
24
     * @param string $password
25
     * @param bool $autoLogin
26
     * @return bool
27
     */
28
    public function login($username, $password, $autoLogin = true)
29
    {
30
        if ($this->request->isLoggedIn()) return true;
31
32
        $this->checkCredentials($username, $password);
33
34
        // Trying to load previously saved cookies from last login session for this username.
35
        // Then grab user profile info to check, if cookies are ok. If an empty response
36
        // was returned, then send login request.
37
        if($autoLogin && $this->processAutoLogin($username)) {
38
            return true;
39
        }
40
41
        return $this->processLogin($username, $password);
42
    }
43
44
    public function logout()
45
    {
46
        $this->request->logout();
47
    }
48
49
    /**
50
     * Register a new user.
51
     *
52
     * @param string $email
53
     * @param string $password
54
     * @param string $name
55
     * @param string $country
56
     * @param int $age
57
     *
58
     * @return bool
59
     */
60
    public function register($email, $password, $name, $country = 'GB', $age = 18)
61
    {
62
        $data = [
63
            "age"        => $age,
64
            "email"      => $email,
65
            "password"   => $password,
66
            "country"    => $country,
67
            "first_name" => $name,
68
            "gender"     => "male",
69
            "container"  => 'home_page',
70
        ];
71
72
        return $this->makeRegisterCall($data);
73
    }
74
75
    /**
76
     * Register a new business account.
77
     *
78
     * @param string $email
79
     * @param string $password
80
     * @param string $businessName
81
     * @param string $website
82
     * @return bool|mixed
83
     */
84
    public function registerBusiness($email, $password, $businessName, $website = '')
85
    {
86
        $data = [
87
            "email"         => $email,
88
            "password"      => $password,
89
            "website_url"   => $website,
90
            "business_name" => $businessName,
91
            "account_type"  => self::ACCOUNT_TYPE_OTHER,
92
        ];
93
94
        return $this->makeRegisterCall($data);
95
    }
96
97
    /**
98
     * @return bool
99
     */
100
    public function isLoggedIn()
101
    {
102
        return $this->request->isLoggedIn();
103
    }
104
105
    /**
106
     * @param string $link
107
     * @return array|bool
108
     */
109
    public function confirmEmail($link)
110
    {
111
        return $this->execGetRequest([], $link);
112
    }
113
114
    /**
115
     * Validates password and login.
116
     *
117
     * @param string $username
118
     * @param string $password
119
     */
120
    protected function checkCredentials($username, $password)
121
    {
122
        if (!$username || !$password) {
123
            throw new \LogicException('You must set username and password to login.');
124
        }
125
    }
126
127
    /**
128
     * @return bool|Response
129
     */
130
    protected function sendEmailVerificationRequest()
131
    {
132
        $actions = [
133
            ['name' => 'unauth.signup_step_1.completed']
134
        ];
135
136
        return $this->sendRegisterActionRequest($actions);
137
    }
138
139
    /**
140
     * @param array $actions
141
     * @return bool|Response
142
     */
143
    protected function sendRegisterActionRequest($actions = [])
144
    {
145
        return $this->execPostRequest(
146
            ['actions' => $actions],
147
            UrlBuilder::RESOURCE_UPDATE_REGISTRATION_TRACK,
148
            true
149
        );
150
    }
151
152
    /**
153
     * @return bool
154
     */
155
    protected function completeRegistration()
156
    {
157
        if(!$this->sendRegisterActions()) return false;
158
159
        return $this->execPostRequest(
160
            ['placed_experience_id' => self::REGISTRATION_COMPLETE_EXPERIENCE_ID],
161
            UrlBuilder::RESOURCE_REGISTRATION_COMPLETE
162
        );
163
    }
164
165
    /**
166
     * @param array $data
167
     * @return bool|mixed
168
     */
169
    protected function makeRegisterCall($data)
170
    {
171
        $this->visitMainPage();
172
        $this->request->setTokenFromCookies();
173
174
        if(!$this->sendEmailVerificationRequest()) return false;
175
176
        if(!$this->execPostRequest($data, UrlBuilder::RESOURCE_CREATE_REGISTER)) return false;
177
178
        return $this->completeRegistration();
179
    }
180
181
    /**
182
     * @param string $username
183
     * @param string $password
184
     * @return bool
185
     */
186
    protected function processLogin($username, $password)
187
    {
188
        $this->request->clearToken();
189
        $this->request->getHttpClient()->removeCookies();
190
191
        $credentials = [
192
            'username_or_email' => $username,
193
            'password'          => $password,
194
        ];
195
196
        $response = $this->execPostRequest($credentials, UrlBuilder::RESOURCE_LOGIN, true);
197
198
        if (!$response->isOk()) return false;
199
200
        $this->request->login();
201
202
        return true;
203
    }
204
205
    /**
206
     * @param string $username
207
     * @return bool
208
     */
209
    protected function processAutoLogin($username)
210
    {
211
        return $this->request->autoLogin($username) && $this->getProfile();
212
    }
213
214
    public function visitMainPage()
215
    {
216
        $this->execGetRequest([], '');
217
    }
218
219
    /**
220
     * @return array
221
     */
222
    protected function getProfile()
223
    {
224
        return $this->execGetRequest([], UrlBuilder::RESOURCE_GET_USER_SETTINGS);
225
    }
226
227
    /**
228
     * @return bool
229
     */
230
    protected function sendRegisterActions()
231
    {
232
        $actions = [
233
            ["name" => "multi_step_step_2_complete"],
234
            ["name" => "signup_home_page"],
235
            ["name" => "signup_referrer.other"],
236
            ["name" => "signup_referrer_module.unauth_home_react_page"],
237
            ["name" => "unauth.signup_step_2.completed"],
238
            ["name" => "setting_new_window_location"],
239
        ];
240
241
        if(!$this->sendRegisterActionRequest($actions)) return false;
242
243
        if(!$this->sendRegisterActionRequest()) return false;
244
245
        return true;
246
    }
247
}