Completed
Pull Request — master (#229)
by Sergey
02:32
created

Auth::confirmEmail()   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
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use seregazhuk\PinterestBot\Api\Response;
6
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
7
use seregazhuk\PinterestBot\Api\Traits\SendsRegisterActions;
8
9
class Auth extends Provider
10
{
11
    use SendsRegisterActions;
12
13
    /**
14
     * @var array
15
     */
16
    protected $loginRequiredFor = [
17
        'logout'
18
    ];
19
20
    const REGISTRATION_COMPLETE_EXPERIENCE_ID = '11:10105';
21
    const ACCOUNT_TYPE_OTHER = 'other';
22
23
    /**
24
     * Login as pinner.
25
     *
26
     * @param string $username
27
     * @param string $password
28
     * @param bool $autoLogin
29
     * @return bool
30
     */
31
    public function login($username, $password, $autoLogin = true)
32
    {
33
        if ($this->request->isLoggedIn()) return true;
34
35
        $this->checkCredentials($username, $password);
36
37
        // Trying to load previously saved cookies from last login session for this username.
38
        // Then grab user profile info to check, if cookies are ok. If an empty response
39
        // was returned, then send login request.
40
        if($autoLogin && $this->processAutoLogin($username)) {
41
            return true;
42
        }
43
44
        return $this->processLogin($username, $password);
45
    }
46
47
    public function logout()
48
    {
49
        $this->request->logout();
50
    }
51
52
    /**
53
     * Register a new user.
54
     *
55
     * @param string $email
56
     * @param string $password
57
     * @param string $name
58
     * @param string $country
59
     * @param int $age
60
     *
61
     * @return bool
62
     */
63
    public function register($email, $password, $name, $country = 'GB', $age = 18)
64
    {
65
        $data = [
66
            "age"        => $age,
67
            "email"      => $email,
68
            "password"   => $password,
69
            "country"    => $country,
70
            "first_name" => $name,
71
            "gender"     => "male",
72
            "container"  => 'home_page',
73
        ];
74
75
        return $this->makeRegisterCall($data);
76
    }
77
78
    /**
79
     * Register a new business account.
80
     *
81
     * @param string $email
82
     * @param string $password
83
     * @param string $businessName
84
     * @param string $website
85
     * @return bool|mixed
86
     */
87
    public function registerBusiness($email, $password, $businessName, $website = '')
88
    {
89
        $data = [
90
            "email"         => $email,
91
            "password"      => $password,
92
            "website_url"   => $website,
93
            "account_type"  => self::ACCOUNT_TYPE_OTHER,
94
            "business_name" => $businessName,
95
        ];
96
97
        return $this->makeBusinessRegisterCall($data);
98
    }
99
100
    /**
101
     * @return bool
102
     */
103
    public function isLoggedIn()
104
    {
105
        return $this->request->isLoggedIn();
106
    }
107
108
    /**
109
     * @param string $link
110
     * @return array|bool
111
     */
112
    public function confirmEmail($link)
113
    {
114
        return $this->visitPage($link);
115
    }
116
117
    /**
118
     * Validates password and login.
119
     *
120
     * @param string $username
121
     * @param string $password
122
     */
123
    protected function checkCredentials($username, $password)
124
    {
125
        if (!$username || !$password) {
126
            throw new \LogicException('You must set username and password to login.');
127
        }
128
    }
129
130
    /**
131
     * @return bool|Response
132
     */
133
    protected function sendEmailVerificationAction()
134
    {
135
        $actions = [
136
            ['name' => 'unauth.signup_step_1.completed']
137
        ];
138
139
        return $this->sendRegisterActionRequest($actions);
140
    }
141
142
    /**
143
     * @return bool
144
     */
145
    protected function completeRegistration()
146
    {
147
        return $this->execPostRequest(
148
            ['placed_experience_id' => self::REGISTRATION_COMPLETE_EXPERIENCE_ID],
149
            UrlBuilder::RESOURCE_REGISTRATION_COMPLETE
150
        );
151
    }
152
153
    /**
154
     * @param array $data
155
     * @return bool|mixed
156
     */
157 View Code Duplication
    protected function makeRegisterCall($data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
    {
159
        $this->visitPage();
160
161
        if(!$this->sendEmailVerificationAction()) return false;
162
163
        if(!$this->execPostRequest($data, UrlBuilder::RESOURCE_CREATE_REGISTER)) return false;
164
165
        if(!$this->sendPlainRegistrationActions()) return false;
166
167
        return $this->completeRegistration();
168
    }
169
170
    /**
171
     * @param array $data
172
     * @return bool|mixed
173
     */
174 View Code Duplication
    protected function makeBusinessRegisterCall($data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
    {
176
        $this->visitPage('business/create/');
177
178
        if(!$this->sendBusinessRegistrationInitActions()) return false;
179
180
        if(!$this->execPostRequest($data, UrlBuilder::RESOURCE_CREATE_REGISTER)) return false;
181
182
        if(!$this->sendBusinessRegistrationFinishActions()) return false;
183
184
        return $this->completeRegistration();
185
    }
186
187
    /**
188
     * @param string $username
189
     * @param string $password
190
     * @return bool
191
     */
192
    protected function processLogin($username, $password)
193
    {
194
        $this->request->clearToken();
195
        $this->request->getHttpClient()->removeCookies();
196
197
        $credentials = [
198
            'username_or_email' => $username,
199
            'password'          => $password,
200
        ];
201
202
        $response = $this->execPostRequest($credentials, UrlBuilder::RESOURCE_LOGIN, true);
203
204
        if (!$response->isOk()) return false;
205
206
        $this->request->login();
207
208
        return true;
209
    }
210
211
    /**
212
     * @param string $username
213
     * @return bool
214
     */
215
    protected function processAutoLogin($username)
216
    {
217
        return $this->request->autoLogin($username) && $this->getProfile();
218
    }
219
220
    /**
221
     * @return array
222
     */
223
    protected function getProfile()
224
    {
225
        return $this->execGetRequest([], UrlBuilder::RESOURCE_GET_USER_SETTINGS);
226
    }
227
228
    /**
229
     * @return bool
230
     */
231
    protected function sendRegisterActions()
232
    {
233
        $actions = [
234
            ["name" => "multi_step_step_2_complete"],
235
            ["name" => "signup_home_page"],
236
            ["name" => "signup_referrer.other"],
237
            ["name" => "signup_referrer_module.unauth_home_react_page"],
238
            ["name" => "unauth.signup_step_2.completed"],
239
            ["name" => "setting_new_window_location"],
240
        ];
241
242
        if(!$this->sendRegisterActionRequest($actions)) return false;
243
244
        if(!$this->sendRegisterActionRequest()) return false;
245
246
        return true;
247
    }
248
}