Completed
Push — master ( 339f50...f1a35d )
by Sergey
05:08 queued 01:52
created

User   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 4
Bugs 2 Features 2
Metric Value
wmc 19
c 4
b 2
f 2
lcom 1
cbo 4
dl 0
loc 210
rs 10

12 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 6 1
A getUserName() 0 4 1
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\Exceptions\AuthFailed;
8
use seregazhuk\PinterestBot\Api\Traits\UploadsImages;
9
10
class User extends Provider
11
{
12
    use UploadsImages;
13
14
    /**
15
     * @var array
16
     */
17
    protected $loginRequiredFor = ['profile', 'convertToBusiness'];
18
19
    const REGISTRATION_COMPLETE_EXPERIENCE_ID = '11:10105';
20
    const ACCOUNT_TYPE_OTHER = 'other';
21
22
    /**
23
     * Updates or returns user profile info. Gets associative array as a param. Available keys of array are:
24
     * 'last_name', 'first_name', 'username', 'about', 'location' and 'website_url'.
25
     * You can also change user avatar by passing 'profile_image'.
26
     *
27
     * @param array $userInfo If empty returns current user profile.
28
     *
29
     * @return bool|array
30
     */
31
    public function profile($userInfo = [])
32
    {
33
        if(empty($userInfo)) {
34
            return $this->execGetRequest([], UrlBuilder::RESOURCE_GET_USER_SETTINGS);
35
        }
36
37
        if (isset($userInfo['profile_image'])) {
38
            $userInfo['profile_image_url'] = $this->upload($userInfo['profile_image']);
39
        }
40
41
        return $this->execPostRequest($userInfo, UrlBuilder::RESOURCE_UPDATE_USER_SETTINGS);
42
    }
43
44
    /**
45
     * Register a new user.
46
     *
47
     * @param string $email
48
     * @param string $password
49
     * @param string $name
50
     * @param string $country
51
     * @param int $age
52
     *
53
     * @return bool
54
     */
55
    public function register($email, $password, $name, $country = "UK", $age = 18)
56
    {
57
        $data = [
58
            "age"        => $age,
59
            "email"      => $email,
60
            "password"   => $password,
61
            "country"    => $country,
62
            "first_name" => $name,
63
            "container"  => "home_page"
64
        ];
65
66
        return $this->makeRegisterCall($data);
67
    }
68
69
    /**
70
     * Register a new business account.
71
     *
72
     * @param string $email
73
     * @param string $password
74
     * @param string $businessName
75
     * @param string $website
76
     * @return bool|mixed
77
     */
78
    public function registerBusiness($email, $password, $businessName, $website = '')
79
    {
80
        $data = [
81
            "email"         => $email,
82
            "password"      => $password,
83
            "website_url"   => $website,
84
            "business_name" => $businessName,
85
            "account_type"  => self::ACCOUNT_TYPE_OTHER,
86
        ];
87
88
        return $this->makeRegisterCall($data);
89
    }
90
91
    /**
92
     * Convert your account to a business one.
93
     *
94
     * @param string $businessName
95
     * @param string $websiteUrl
96
     *
97
     * @return bool
98
     */
99
    public function convertToBusiness($businessName, $websiteUrl = '')
100
    {
101
        $data = [
102
            'business_name' => $businessName,
103
            'website_url'   => $websiteUrl,
104
            'account_type'  => self::ACCOUNT_TYPE_OTHER,
105
        ];
106
107
        return $this->execPostRequest($data, UrlBuilder::RESOURCE_CONVERT_TO_BUSINESS);
108
    }
109
110
    /**
111
     * Login as pinner.
112
     *
113
     * @param string $username
114
     * @param string $password
115
     *
116
     * @throws AuthFailed
117
     *
118
     * @return bool
119
     */
120
    public function login($username, $password)
121
    {
122
        if ($this->request->isLoggedIn()) return true;
123
124
        $this->checkCredentials($username, $password);
125
        $this->request->clearToken();
126
127
        $credentials = [
128
            'username_or_email' => $username,
129
            'password'          => $password,
130
        ];
131
132
        $response = $this->execPostRequest($credentials, UrlBuilder::RESOURCE_LOGIN, true);
133
        if ($response->hasErrors()) {
134
            throw new AuthFailed($response->getLastError()['message']);
135
        }
136
137
        $this->request->login();
138
139
        return true;
140
    }
141
142
    public function logout()
143
    {
144
        $this->request->logout();
145
    }
146
147
    /**
148
     * @return bool
149
     */
150
    public function isLoggedIn()
151
    {
152
        return $this->request->isLoggedIn();
153
    }
154
155
    /**
156
     * Checks if current user is banned
157
     *
158
     * @return bool
159
     */
160
    public function isBanned()
161
    {
162
        $profile = $this->profile();
163
164
        return (bool)$profile['is_write_banned'];
165
    }
166
167
    /**
168
     * Returns current user username
169
     *
170
     * @return string
171
     */
172
    public function getUserName()
173
    {
174
        return $this->profile()['username'];
175
    }
176
177
    /**
178
     * Validates password and login.
179
     *
180
     * @param string $username
181
     * @param string $password
182
     */
183
    protected function checkCredentials($username, $password)
184
    {
185
        if (!$username || !$password) {
186
            throw new LogicException('You must set username and password to login.');
187
        }
188
    }
189
190
    /**
191
     * @return bool
192
     */
193
    protected function completeRegistration()
194
    {
195
        $this->request->setTokenFromCookies();
196
197
        return $this->execPostRequest(
198
                ['placed_experience_id' => self::REGISTRATION_COMPLETE_EXPERIENCE_ID],
199
                UrlBuilder::RESOURCE_REGISTRATION_COMPLETE
200
            );
201
    }
202
203
    /**
204
     * @param array $data
205
     * @return bool|mixed
206
     * @throws AuthFailed
207
     */
208
    protected function makeRegisterCall($data)
209
    {
210
        $this->execGetRequest([], '');
211
        $this->request->setTokenFromCookies();
212
213
        if (!$this->execPostRequest($data, UrlBuilder::RESOURCE_CREATE_REGISTER)) {
214
            return false;
215
        }
216
217
        return $this->completeRegistration();
218
    }
219
}
220