Completed
Push — master ( 9da044...c40fa9 )
by Sergey
03:27 queued 01:08
created

Auth   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 5
dl 0
loc 179
rs 10
c 0
b 0
f 0

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