Completed
Push — master ( acee74...12df02 )
by Sergey
06:23 queued 03:08
created

Auth::completeRegistration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
6
7
class Auth extends Provider
8
{
9
10
    /**
11
     * @var array
12
     */
13
    protected $loginRequiredFor = [];
14
15
    const REGISTRATION_COMPLETE_EXPERIENCE_ID = '11:10105';
16
    const ACCOUNT_TYPE_OTHER = 'other';
17
18
    /**
19
     * Login as pinner.
20
     *
21
     * @param string $username
22
     * @param string $password
23
     * @param bool $autoLogin
24
     * @return bool
25
     */
26
    public function login($username, $password, $autoLogin = true)
27
    {
28
        if ($this->request->isLoggedIn()) return true;
29
30
        $this->checkCredentials($username, $password);
31
32
        // Trying to load previously saved cookies from last login session for this username.
33
        // Then grab user profile info to check, if cookies are ok. If an empty response
34
        // was returned, then send login request.
35
        if($autoLogin && $this->processAutoLogin($username)) {
36
            return true;
37
        }
38
39
        return $this->processLogin($username, $password);
40
    }
41
42
    /**
43
     * If $removeCookies is set, cookie file will be removed
44
     * from the file system.
45
     *
46
     * @param bool $removeCookies
47
     */
48
    public function logout($removeCookies = false)
49
    {
50
        $this->request->logout($removeCookies);
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
            "container"  => 'home_page',
73
        ];
74
75
        return $this->makeRegisterCall($data);
76
    }
77
78
    /**
79
     * Register a new business account.
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
        $data = [
90
            "email"         => $email,
91
            "password"      => $password,
92
            "website_url"   => $website,
93
            "business_name" => $businessName,
94
            "account_type"  => self::ACCOUNT_TYPE_OTHER,
95
        ];
96
97
        return $this->makeRegisterCall($data);
98
    }
99
100
    /**
101
     * @return bool
102
     */
103
    public function isLoggedIn()
104
    {
105
        return $this->request->isLoggedIn();
106
    }
107
108
    /**
109
     * Validates password and login.
110
     *
111
     * @param string $username
112
     * @param string $password
113
     */
114
    protected function checkCredentials($username, $password)
115
    {
116
        if (!$username || !$password) {
117
            throw new \LogicException('You must set username and password to login.');
118
        }
119
    }
120
121
    /**
122
     * @return bool
123
     */
124
    protected function completeRegistration()
125
    {
126
        $this->request->setTokenFromCookies();
127
128
        return $this->execPostRequest(
129
            ['placed_experience_id' => self::REGISTRATION_COMPLETE_EXPERIENCE_ID],
130
            UrlBuilder::RESOURCE_REGISTRATION_COMPLETE
131
        );
132
    }
133
134
    /**
135
     * @param array $data
136
     * @return bool|mixed
137
     */
138
    protected function makeRegisterCall($data)
139
    {
140
        $this->visitMainPage();
141
        $this->request->setTokenFromCookies();
142
143
        if (!$this->execPostRequest($data, UrlBuilder::RESOURCE_CREATE_REGISTER)) {
144
            return false;
145
        }
146
147
        return $this->completeRegistration();
148
    }
149
150
    /**
151
     * @param string $username
152
     * @param string $password
153
     * @return bool
154
     */
155
    protected function processLogin($username, $password)
156
    {
157
        $this->request->clearToken();
158
        $this->request->getHttpClient()->removeCookies();
159
160
        $credentials = [
161
            'username_or_email' => $username,
162
            'password'          => $password,
163
        ];
164
165
        $response = $this->execPostRequest($credentials, UrlBuilder::RESOURCE_LOGIN, true);
166
167
        if (!$response->isOk()) return false;
168
169
        $this->request->login();
170
171
        return true;
172
    }
173
174
    /**
175
     * @param string $username
176
     * @return bool
177
     */
178
    protected function processAutoLogin($username)
179
    {
180
        return $this->request->autoLogin($username) && $this->getProfile();
181
    }
182
183
    public function visitMainPage()
184
    {
185
        $this->execGetRequest([], '');
186
    }
187
188
    /**
189
     * @return array
190
     */
191
    protected function getProfile()
192
    {
193
        return $this->execGetRequest([], UrlBuilder::RESOURCE_GET_USER_SETTINGS);
194
    }
195
}