Completed
Pull Request — master (#168)
by Sergey
03:10
created

User   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 256
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 24
lcom 1
cbo 3
dl 0
loc 256
rs 10
c 2
b 0
f 2

14 Methods

Rating   Name   Duplication   Size   Complexity  
A profile() 0 12 3
A register() 0 13 1
A registerBusiness() 0 12 1
A convertToBusiness() 0 10 1
A login() 0 21 3
A logout() 0 4 1
A isLoggedIn() 0 4 1
A isBanned() 0 8 2
A getUserName() 0 6 2
A changePassword() 0 10 1
A deactivate() 0 14 2
A checkCredentials() 0 6 3
A completeRegistration() 0 9 1
A makeRegisterCall() 0 11 2
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
     * @return bool
123
     */
124
    public function login($username, $password)
125
    {
126
        if ($this->request->isLoggedIn()) return true;
127
128
        $this->checkCredentials($username, $password);
129
        $this->request->clearToken();
130
131
        $credentials = [
132
            'username_or_email' => $username,
133
            'password'          => $password,
134
        ];
135
136
        $response = $this->execPostRequest($credentials, UrlBuilder::RESOURCE_LOGIN, true);
137
        if ($response->hasErrors()) {
138
            return false;
139
        }
140
141
        $this->request->login();
142
143
        return true;
144
    }
145
146
    public function logout()
147
    {
148
        $this->request->logout();
149
    }
150
151
    /**
152
     * @return bool
153
     */
154
    public function isLoggedIn()
155
    {
156
        return $this->request->isLoggedIn();
157
    }
158
159
    /**
160
     * Checks if current user is banned
161
     *
162
     * @return bool
163
     */
164
    public function isBanned()
165
    {
166
        $profile = $this->profile();
167
168
       return isset($profile['is_write_banned']) ?
169
           (bool)$profile['is_write_banned'] :
170
           false;
171
    }
172
173
    /**
174
     * Returns current user username
175
     *
176
     * @return string
177
     */
178
    public function getUserName()
179
    {
180
        $profile = $this->profile();
181
182
        return isset($profile['username']) ? $profile['username'] : '';
183
    }
184
185
    /**
186
     * @param string $oldPassword
187
     * @param string $newPassword
188
     * @return bool
189
     */
190
    public function changePassword($oldPassword, $newPassword)
191
    {
192
        $request = [
193
            'old_password'         => $oldPassword,
194
            'new_password'         => $newPassword,
195
            'new_password_confirm' => $newPassword,
196
        ];
197
198
        return $this->execPostRequest($request, UrlBuilder::RESOURCE_CHANGE_PASSWORD);
199
    }
200
201
    /**
202
     * Deactivates your account.
203
     *
204
     * @param string $reason
205
     * @param string $explanation
206
     * @return bool
207
     */
208
    public function deactivate($reason = 'other', $explanation = '')
209
    {
210
        $profile = $this->profile();
211
212
        if(!isset($profile['id'])) return false;
213
214
        $request = [
215
            'user_id'     => $profile['id'],
216
            'reason'      => $reason,
217
            'explanation' => $explanation,
218
        ];
219
220
        return $this->execPostRequest($request, UrlBuilder::RESOURCE_DEACTIVATE_ACCOUNT);
221
    }
222
223
    /**
224
     * Validates password and login.
225
     *
226
     * @param string $username
227
     * @param string $password
228
     */
229
    protected function checkCredentials($username, $password)
230
    {
231
        if (!$username || !$password) {
232
            throw new LogicException('You must set username and password to login.');
233
        }
234
    }
235
236
    /**
237
     * @return bool
238
     */
239
    protected function completeRegistration()
240
    {
241
        $this->request->setTokenFromCookies();
242
243
        return $this->execPostRequest(
244
                ['placed_experience_id' => self::REGISTRATION_COMPLETE_EXPERIENCE_ID],
245
                UrlBuilder::RESOURCE_REGISTRATION_COMPLETE
246
            );
247
    }
248
249
    /**
250
     * @param array $data
251
     * @return bool|mixed
252
     */
253
    protected function makeRegisterCall($data)
254
    {
255
        $this->execGetRequest([], '');
256
        $this->request->setTokenFromCookies();
257
258
        if (!$this->execPostRequest($data, UrlBuilder::RESOURCE_CREATE_REGISTER)) {
259
            return false;
260
        }
261
262
        return $this->completeRegistration();
263
    }
264
}
265