Completed
Pull Request — master (#296)
by Sergey
02:05
created

User   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

15 Methods

Rating   Name   Duplication   Size   Complexity  
A profile() 0 7 2
A isBanned() 0 8 2
A username() 0 4 1
A id() 0 4 1
A deactivate() 0 14 2
A invite() 0 9 1
A clearSearchHistory() 0 4 1
A visitPage() 0 4 1
A sessionsHistory() 0 4 1
A getLocales() 0 4 1
A getCountries() 0 4 1
A getAccountTypes() 0 4 1
A getProfile() 0 4 1
A updateProfile() 0 13 3
A getProfileData() 0 6 2
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use seregazhuk\PinterestBot\Api\Response;
6
use seregazhuk\PinterestBot\Api\Forms\Profile;
7
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
8
use seregazhuk\PinterestBot\Api\Traits\UploadsImages;
9
use seregazhuk\PinterestBot\Api\Providers\Core\Provider;
10
11
class User extends Provider
12
{
13
    use UploadsImages;
14
15
    /**
16
     * @var array
17
     */
18
    protected $loginRequiredFor = [
19
        'id',
20
        'invite',
21
        'profile',
22
        'username',
23
        'isBanned',
24
        'deactivate',
25
        'sessionsHistory',
26
        'convertToBusiness',
27
    ];
28
29
    /**
30
     * Updates or returns user profile info. Gets associative array as a param. Available keys of array are:
31
     * 'last_name', 'first_name', 'username', 'about', 'location' and 'website_url'.
32
     * You can also change user avatar by passing 'profile_image'.
33
     *
34
     * @param array $userInfo If empty returns current user profile.
35
     *
36
     * @return bool|array|Profile
37
     */
38
    public function profile($userInfo = null)
39
    {
40
        // If we call method without params, return current user profile data.
41
        if(empty($userInfo)) return $this->getProfile();
42
43
        return $this->updateProfile($userInfo);
44
    }
45
46
    /**
47
     * Checks if current user is banned
48
     *
49
     * @return bool
50
     */
51
    public function isBanned()
52
    {
53
        $profile = $this->profile();
54
55
       return isset($profile['is_write_banned']) ?
56
           (bool)$profile['is_write_banned'] :
57
           false;
58
    }
59
60
    /**
61
     * Returns current user username
62
     *
63
     * @return string
64
     */
65
    public function username()
66
    {
67
        return $this->getProfileData('username');
68
    }
69
70
    /**
71
     * Returns current user id
72
     *
73
     * @return string
74
     */
75
    public function id()
76
    {
77
        return $this->getProfileData('id');
78
    }
79
80
    /**
81
     * Deactivates your account.
82
     *
83
     * @param string $reason
84
     * @param string $explanation
85
     * @return bool
86
     */
87
    public function deactivate($reason = 'other', $explanation = '')
88
    {
89
        $profile = $this->profile();
90
91
        if(!isset($profile['id'])) return false;
92
93
        $request = [
94
            'user_id'     => $profile['id'],
95
            'reason'      => $reason,
96
            'explanation' => $explanation,
97
        ];
98
99
        return $this->post($request, UrlBuilder::RESOURCE_DEACTIVATE_ACCOUNT);
100
    }
101
102
    /**
103
     * Send invite to email
104
     * @param string $email
105
     * @return bool|Response
106
     */
107
    public function invite($email)
108
    {
109
        $data = [
110
            'email' => $email,
111
            'type'  => 'email',
112
        ];
113
114
        return $this->post($data, UrlBuilder::RESOURCE_INVITE);
115
    }
116
117
    /**
118
     * Remove things you’ve recently searched for from search suggestions.
119
     * @return bool|Response
120
     */
121
    public function clearSearchHistory()
122
    {
123
        return $this->post([], UrlBuilder::RESOURCE_CLEAR_SEARCH_HISTORY);
124
    }
125
126
    /**
127
     * Simply makes GET request to some url.
128
     * @param string $url
129
     * @return array|bool
130
     */
131
    public function visitPage($url = '')
132
    {
133
        return $this->get([], $url);
134
    }
135
136
    /**
137
     * Get your account sessions history
138
     * @return array
139
     */
140
    public function sessionsHistory()
141
    {
142
        return $this->get([], UrlBuilder::RESOURCE_SESSIONS_HISTORY);
143
    }
144
145
    /**
146
     * Get list of available locales
147
     * @return array
148
     */
149
    public function getLocales()
150
    {
151
        return $this->get([], UrlBuilder::RESOURCE_AVAILABLE_LOCALES);
152
    }
153
154
    /**
155
     * Get list of available countries
156
     * @return array
157
     */
158
    public function getCountries()
159
    {
160
        return $this->get([], UrlBuilder::RESOURCE_AVAILABLE_COUNTRIES);
161
    }
162
163
    /**
164
     * Get list of available account types
165
     * @return array
166
     */
167
    public function getAccountTypes()
168
    {
169
        return $this->get([], UrlBuilder::RESOURCE_AVAILABLE_ACCOUNT_TYPES);
170
    }
171
172
    /**
173
     * @return array
174
     */
175
    protected function getProfile()
176
    {
177
        return $this->get([], UrlBuilder::RESOURCE_GET_USER_SETTINGS);
178
    }
179
180
    /**
181
     * @param array|Profile $userInfo
182
     * @return bool|Response
183
     */
184
    protected function updateProfile($userInfo)
185
    {
186
        // If we have a form object, convert it to array
187
        if ($userInfo instanceof Profile) {
188
            $userInfo = $userInfo->toArray();
189
        }
190
191
        if (isset($userInfo['profile_image'])) {
192
            $userInfo['profile_image_url'] = $this->upload($userInfo['profile_image']);
193
        }
194
195
        return $this->post($userInfo, UrlBuilder::RESOURCE_UPDATE_USER_SETTINGS);
196
    }
197
198
    /**
199
     * @param string $key
200
     * @return mixed|string
201
     */
202
    protected function getProfileData($key)
203
    {
204
        $profile = $this->profile();
205
206
        return isset($profile[$key]) ? $profile[$key] : '';
207
    }
208
}
209