Completed
Push — master ( 3f431b...deda3e )
by Sergey
04:18 queued 01:59
created

User::isLoggedIn()   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
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use LogicException;
6
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
7
use seregazhuk\PinterestBot\Api\Traits\UploadsImages;
8
9
class User extends Provider
10
{
11
    use UploadsImages;
12
13
    /**
14
     * @var array
15
     */
16
    protected $loginRequiredFor = [
17
        'profile',
18
        'convertToBusiness',
19
        'changePassword',
20
        'isBanned',
21
        'deactivate',
22
        'getUserName'
23
    ];
24
25
    const REGISTRATION_COMPLETE_EXPERIENCE_ID = '11:10105';
26
    const ACCOUNT_TYPE_OTHER = 'other';
27
28
    /**
29
     * Updates or returns user profile info. Gets associative array as a param. Available keys of array are:
30
     * 'last_name', 'first_name', 'username', 'about', 'location' and 'website_url'.
31
     * You can also change user avatar by passing 'profile_image'.
32
     *
33
     * @param array $userInfo If empty returns current user profile.
34
     *
35
     * @return bool|array
36
     */
37
    public function profile($userInfo = [])
38
    {
39
        if(empty($userInfo)) {
40
            return $this->execGetRequest([], UrlBuilder::RESOURCE_GET_USER_SETTINGS);
41
        }
42
43
        if (isset($userInfo['profile_image'])) {
44
            $userInfo['profile_image_url'] = $this->upload($userInfo['profile_image']);
45
        }
46
47
        return $this->execPostRequest($userInfo, UrlBuilder::RESOURCE_UPDATE_USER_SETTINGS);
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 = "UK", $age = 18)
62
    {
63
        $data = [
64
            "age"        => $age,
65
            "email"      => $email,
66
            "password"   => $password,
67
            "country"    => $country,
68
            "first_name" => $name,
69
            "container"  => "home_page"
70
        ];
71
72
        return $this->makeRegisterCall($data);
73
    }
74
75
    /**
76
     * Register a new business account.
77
     *
78
     * @param string $email
79
     * @param string $password
80
     * @param string $businessName
81
     * @param string $website
82
     * @return bool|mixed
83
     */
84
    public function registerBusiness($email, $password, $businessName, $website = '')
85
    {
86
        $data = [
87
            "email"         => $email,
88
            "password"      => $password,
89
            "website_url"   => $website,
90
            "business_name" => $businessName,
91
            "account_type"  => self::ACCOUNT_TYPE_OTHER,
92
        ];
93
94
        return $this->makeRegisterCall($data);
95
    }
96
97
    /**
98
     * Convert your account to a business one.
99
     *
100
     * @param string $businessName
101
     * @param string $websiteUrl
102
     *
103
     * @return bool
104
     */
105
    public function convertToBusiness($businessName, $websiteUrl = '')
106
    {
107
        $data = [
108
            'business_name' => $businessName,
109
            'website_url'   => $websiteUrl,
110
            'account_type'  => self::ACCOUNT_TYPE_OTHER,
111
        ];
112
113
        return $this->execPostRequest($data, UrlBuilder::RESOURCE_CONVERT_TO_BUSINESS);
114
    }
115
116
    /**
117
     * Login as pinner.
118
     *
119
     * @param string $username
120
     * @param string $password
121
     *
122
     * @param bool $autoLogin
123
     * @return bool
124
     */
125
    public function login($username, $password, $autoLogin = true)
126
    {
127
        if ($this->request->isLoggedIn()) return true;
128
129
        $this->checkCredentials($username, $password);
130
131
        // Trying to load previously saved cookies from last login
132
        // session for this username.
133
        if($autoLogin && $this->request->autoLogin($username)) {
134
            return true;
135
        }
136
137
        return $this->processLogin($username, $password);
138
    }
139
140
    public function logout()
141
    {
142
        $this->request->logout();
143
    }
144
145
    /**
146
     * @return bool
147
     */
148
    public function isLoggedIn()
149
    {
150
        return $this->request->isLoggedIn();
151
    }
152
153
    /**
154
     * Checks if current user is banned
155
     *
156
     * @return bool
157
     */
158
    public function isBanned()
159
    {
160
        $profile = $this->profile();
161
162
       return isset($profile['is_write_banned']) ?
163
           (bool)$profile['is_write_banned'] :
164
           false;
165
    }
166
167
    /**
168
     * Returns current user username
169
     *
170
     * @return string
171
     */
172
    public function getUserName()
173
    {
174
        $profile = $this->profile();
175
176
        return isset($profile['username']) ? $profile['username'] : '';
177
    }
178
179
    /**
180
     * @param string $oldPassword
181
     * @param string $newPassword
182
     * @return bool
183
     */
184
    public function changePassword($oldPassword, $newPassword)
185
    {
186
        $request = [
187
            'old_password'         => $oldPassword,
188
            'new_password'         => $newPassword,
189
            'new_password_confirm' => $newPassword,
190
        ];
191
192
        return $this->execPostRequest($request, UrlBuilder::RESOURCE_CHANGE_PASSWORD);
193
    }
194
195
    /**
196
     * Deactivates your account.
197
     *
198
     * @param string $reason
199
     * @param string $explanation
200
     * @return bool
201
     */
202
    public function deactivate($reason = 'other', $explanation = '')
203
    {
204
        $profile = $this->profile();
205
206
        if(!isset($profile['id'])) return false;
207
208
        $request = [
209
            'user_id'     => $profile['id'],
210
            'reason'      => $reason,
211
            'explanation' => $explanation,
212
        ];
213
214
        return $this->execPostRequest($request, UrlBuilder::RESOURCE_DEACTIVATE_ACCOUNT);
215
    }
216
217
    /**
218
     * Validates password and login.
219
     *
220
     * @param string $username
221
     * @param string $password
222
     */
223
    protected function checkCredentials($username, $password)
224
    {
225
        if (!$username || !$password) {
226
            throw new LogicException('You must set username and password to login.');
227
        }
228
    }
229
230
    /**
231
     * @return bool
232
     */
233
    protected function completeRegistration()
234
    {
235
        $this->request->setTokenFromCookies();
236
237
        return $this->execPostRequest(
238
                ['placed_experience_id' => self::REGISTRATION_COMPLETE_EXPERIENCE_ID],
239
                UrlBuilder::RESOURCE_REGISTRATION_COMPLETE
240
            );
241
    }
242
243
    /**
244
     * @param array $data
245
     * @return bool|mixed
246
     */
247
    protected function makeRegisterCall($data)
248
    {
249
        $this->visitMainPage();
250
        $this->request->setTokenFromCookies();
251
252
        if (!$this->execPostRequest($data, UrlBuilder::RESOURCE_CREATE_REGISTER)) {
253
            return false;
254
        }
255
256
        return $this->completeRegistration();
257
    }
258
259
    /**
260
     * @param string $username
261
     * @param string $password
262
     * @return bool
263
     */
264
    protected function processLogin($username, $password)
265
    {
266
        $this->request->clearToken();
267
268
        $credentials = [
269
            'username_or_email' => $username,
270
            'password'          => $password,
271
        ];
272
273
        $response = $this->execPostRequest($credentials, UrlBuilder::RESOURCE_LOGIN, true);
274
        if ($response->hasErrors()) {
275
            return false;
276
        }
277
278
        $this->request->login();
279
280
        return true;
281
    }
282
283
284
    public function visitMainPage()
285
    {
286
        $this->execGetRequest([], '');
287
    }
288
}
289