Completed
Push — master ( 7d378c...9da044 )
by Sergey
03:36 queued 01:11
created

Auth::sendEmailVerificationAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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