Completed
Pull Request — master (#308)
by Sergey
06:18
created

Auth::getProfile()   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 0
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\ResolvesCurrentUser;
9
use seregazhuk\PinterestBot\Api\Traits\SendsRegisterActions;
10
11
class Auth extends Provider
12
{
13
    use SendsRegisterActions, ResolvesCurrentUser;
14
15
    /**
16
     * @var array
17
     */
18
    protected $loginRequiredFor = [
19
        'logout',
20
        'convertToBusiness',
21
    ];
22
23
    const REGISTRATION_COMPLETE_EXPERIENCE_ID = '11:10105';
24
25
    /**
26
     * Login as pinner.
27
     *
28
     * @param string $username
29
     * @param string $password
30
     * @param bool $autoLogin
31
     * @return bool
32
     */
33
    public function login($username, $password, $autoLogin = true)
34
    {
35
        if ($this->isLoggedIn()) return true;
36
37
        $this->checkCredentials($username, $password);
38
39
        // Trying to load previously saved cookies from last login session for this username.
40
        // Then grab user profile info to check, if cookies are ok. If an empty response
41
        // was returned, then send login request.
42
        if ($autoLogin && $this->processAutoLogin($username)) {
43
            return true;
44
        }
45
46
        return $this->processLogin($username, $password);
47
    }
48
49
    public function logout()
50
    {
51
        $this->request->logout();
52
    }
53
54
    /**
55
     * Register a new user.
56
     *
57
     * @param string|Registration $email
58
     * @param string $password
59
     * @param string $name
60
     * @param string $country @deprecated
61
     * @param int $age @deprecated
62
     *
63
     * @return bool
64
     */
65
    public function register($email, $password = null, $name = null, $country = 'GB', $age = 18)
66
    {
67
        $registrationForm = $this->getRegistrationForm($email, $password, $name, $country, $age);
68
69
        return $this->makeRegisterCall($registrationForm);
70
    }
71
72
    /**
73
     * Register a new business account. At first we register a basic type account.
74
     * Then convert it to a business one. This is done to receive a confirmation
75
     * email after registration.
76
     *
77
     * @param string|Registration $registrationForm
78
     * @param string $password
79
     * @param string $name
80
     * @param string $website
81
     * @return bool|mixed
82
     */
83
    public function registerBusiness($registrationForm, $password, $name, $website = '')
84
    {
85
        $registration = $this->register($registrationForm, $password, $name);
86
87
        if (!$registration) return false;
88
89
        $website = ($registrationForm instanceof Registration) ?
90
            $registrationForm->getSite() : $website;
91
92
        return $this->convertToBusiness($name, $website);
93
    }
94
95
    /**
96
     * Convert your account to a business one.
97
     *
98
     * @param string $businessName
99
     * @param string $websiteUrl
100
     * @return bool
101
     */
102
    public function convertToBusiness($businessName, $websiteUrl = '')
103
    {
104
        $data = [
105
            'business_name' => $businessName,
106
            'website_url'   => $websiteUrl,
107
            'account_type'  => 'other',
108
        ];
109
110
        return $this->post(UrlBuilder::RESOURCE_CONVERT_TO_BUSINESS, $data);
111
    }
112
113
    /**
114
     * @param string $link
115
     * @return array|bool
116
     */
117
    public function confirmEmail($link)
118
    {
119
        return $this->get($link);
120
    }
121
122
    /**
123
     * Validates password and login.
124
     *
125
     * @param string $username
126
     * @param string $password
127
     */
128
    protected function checkCredentials($username, $password)
129
    {
130
        if (!$username || !$password) {
131
            throw new \LogicException('You must set username and password to login.');
132
        }
133
    }
134
135
    /**
136
     * @return bool
137
     */
138
    protected function completeRegistration()
139
    {
140
        return $this->post(
141
            UrlBuilder::RESOURCE_REGISTRATION_COMPLETE,
142
            ['placed_experience_id' => self::REGISTRATION_COMPLETE_EXPERIENCE_ID]
143
        );
144
    }
145
146
    /**
147
     * @param Registration $registrationForm
148
     * @return bool|mixed
149
     */
150
    protected function makeRegisterCall(Registration $registrationForm)
151
    {
152
        if (!$this->sendEmailVerificationAction()) return false;
153
154
        if (!$this->post(UrlBuilder::RESOURCE_CREATE_REGISTER, $registrationForm->toArray())) 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->loadCookiesFor($username);
169
170
        $credentials = [
171
            'username_or_email' => $username,
172
            'password'          => $password,
173
        ];
174
175
        $this->post(UrlBuilder::RESOURCE_LOGIN, $credentials);
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->resolveCurrentUserId();
191
    }
192
193
    /**
194
     * @param string $registrationForm
195
     * @param string $password
196
     * @param string $name
197
     * @param string $country
198
     * @param string $age
199
     * @return Registration
200
     */
201
    protected function fillRegistrationForm($registrationForm, $password, $name, $country, $age)
202
    {
203
        return (new Registration($registrationForm, $password, $name))
204
            ->setCountry($country)
205
            ->setAge($age)
206
            ->setGender("male");
207
    }
208
209
    /**
210
     * @param string|Registration $email
211
     * @param string $password
212
     * @param string $name
213
     * @param string $country
214
     * @param string $age
215
     * @return Registration
216
     */
217
    protected function getRegistrationForm($email, $password, $name, $country, $age)
218
    {
219
        if ($email instanceof Registration) return $email;
220
221
        return $this->fillRegistrationForm(
222
            $email, $password, $name, $country, $age
223
        );
224
    }
225
}
226