Completed
Pull Request — master (#236)
by Sergey
06:25
created

Auth   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 23
lcom 1
cbo 4
dl 0
loc 197
rs 10
c 1
b 0
f 1

12 Methods

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