Completed
Push — master ( 2d0d9b...2037bb )
by Sergey
05:22 queued 02:31
created

Auth   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

14 Methods

Rating   Name   Duplication   Size   Complexity  
A login() 0 15 4
A logout() 0 4 1
A register() 0 6 1
A registerBusiness() 0 11 3
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 17 2
A processAutoLogin() 0 4 2
A getProfile() 0 4 1
A fillRegistrationForm() 0 7 1
A getRegistrationForm() 0 8 2
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
6
use seregazhuk\PinterestBot\Api\Forms\Registration;
7
use seregazhuk\PinterestBot\Api\Providers\Core\Provider;
8
use seregazhuk\PinterestBot\Api\Traits\SendsRegisterActions;
9
10
class Auth extends Provider
11
{
12
    use SendsRegisterActions;
13
14
    /**
15
     * @var array
16
     */
17
    protected $loginRequiredFor = [
18
        'logout'
19
    ];
20
21
    const REGISTRATION_COMPLETE_EXPERIENCE_ID = '11:10105';
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->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|Registration $email
56
     * @param string $password
57
     * @param string $name
58
     * @param string $country @deprecated
59
     * @param int $age @deprecated
60
     *
61
     * @return bool
62
     */
63
    public function register($email, $password = null, $name = null, $country = 'GB', $age = 18)
64
    {
65
        $registrationForm = $this->getRegistrationForm($email, $password, $name, $country, $age);
0 ignored issues
show
Bug introduced by
It seems like $email defined by parameter $email on line 63 can also be of type object<seregazhuk\Pinter...Api\Forms\Registration>; however, seregazhuk\PinterestBot\...::getRegistrationForm() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
66
67
        return $this->makeRegisterCall($registrationForm);
68
    }
69
70
    /**
71
     * Register a new business account. At first we register a basic type account.
72
     * Then convert it to a business one. This is done to receive a confirmation
73
     * email after registration.
74
     *
75
     * @param string|Registration $registrationForm
76
     * @param string $password
77
     * @param string $name
78
     * @param string $website
79
     * @return bool|mixed
80
     */
81
    public function registerBusiness($registrationForm, $password, $name, $website = '')
82
    {
83
        $registration = $this->register($registrationForm, $password, $name);
84
85
        if(!$registration) return false;
86
87
        $website = ($registrationForm instanceof Registration) ?
88
            $registrationForm->getSite() : $website;
89
90
        return $this->convertToBusiness($name, $website);
91
    }
92
93
    /**
94
     * Convert your account to a business one.
95
     *
96
     * @param string $businessName
97
     * @param string $websiteUrl
98
     * @return bool
99
     */
100
    public function convertToBusiness($businessName, $websiteUrl = '')
101
    {
102
        $data = [
103
            'business_name' => $businessName,
104
            'website_url'   => $websiteUrl,
105
            'account_type'  => 'other',
106
        ];
107
108
        return $this->post($data, UrlBuilder::RESOURCE_CONVERT_TO_BUSINESS);
109
    }
110
111
    /**
112
     * @param string $link
113
     * @return array|bool
114
     */
115
    public function confirmEmail($link)
116
    {
117
        return $this->visitPage($link);
118
    }
119
120
    /**
121
     * Validates password and login.
122
     *
123
     * @param string $username
124
     * @param string $password
125
     */
126
    protected function checkCredentials($username, $password)
127
    {
128
        if (!$username || !$password) {
129
            throw new \LogicException('You must set username and password to login.');
130
        }
131
    }
132
133
    /**
134
     * @return bool
135
     */
136
    protected function completeRegistration()
137
    {
138
        return $this->post(
139
            ['placed_experience_id' => self::REGISTRATION_COMPLETE_EXPERIENCE_ID],
140
            UrlBuilder::RESOURCE_REGISTRATION_COMPLETE
141
        );
142
    }
143
144
    /**
145
     * @param Registration $registrationForm
146
     * @return bool|mixed
147
     */
148
    protected function makeRegisterCall(Registration $registrationForm)
149
    {
150
        $this->visitPage();
151
152
        if(!$this->sendEmailVerificationAction()) return false;
153
154
        if(!$this->post($registrationForm->toArray(), UrlBuilder::RESOURCE_CREATE_REGISTER)) return false;
155
156
        if(!$this->sendRegistrationActions()) return false;
157
158
        return $this->completeRegistration();
159
    }
160
161
    /**
162
     * @param string $username
163
     * @param string $password
164
     * @return bool
165
     */
166
    protected function processLogin($username, $password)
167
    {
168
        $this->request->dropCookies();
169
170
        $credentials = [
171
            'username_or_email' => $username,
172
            'password'          => $password,
173
        ];
174
175
        $this->post($credentials, UrlBuilder::RESOURCE_LOGIN);
176
177
        if ($this->response->isEmpty()) return false;
178
179
        $this->request->login();
180
181
        return true;
182
    }
183
184
    /**
185
     * @param string $username
186
     * @return bool
187
     */
188
    protected function processAutoLogin($username)
189
    {
190
        return $this->request->autoLogin($username) && $this->getProfile();
191
    }
192
193
    /**
194
     * @return array
195
     */
196
    protected function getProfile()
197
    {
198
        return $this->get([], UrlBuilder::RESOURCE_GET_USER_SETTINGS);
199
    }
200
201
    /**
202
     * @param $registrationForm
203
     * @param $password
204
     * @param $name
205
     * @param $country
206
     * @param $age
207
     * @return Registration
208
     */
209
    protected function fillRegistrationForm($registrationForm, $password, $name, $country, $age)
210
    {
211
        return (new Registration($registrationForm, $password, $name))
212
            ->setCountry($country)
213
            ->setAge($age)
214
            ->setGender("male");
215
    }
216
217
    /**
218
     * @param string $email
219
     * @param string $password
220
     * @param string $name
221
     * @param string $country
222
     * @param string $age
223
     * @return Registration
224
     */
225
    protected function getRegistrationForm($email, $password, $name, $country, $age)
226
    {
227
        if($email instanceof Registration) return $email;
228
229
        return $this->fillRegistrationForm(
230
            $email, $password, $name, $country, $age
231
        );
232
    }
233
}