Passed
Push — master ( d5d6b0...312b1e )
by Sergey
03:09
created

User::getCountries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
        'invite',
20
        'profile',
21
        'username',
22
        'isBanned',
23
        'deactivate',
24
        'sessionsHistory',
25
        'convertToBusiness',
26
    ];
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|Profile
36
     */
37
    public function profile($userInfo = null)
38
    {
39
        // If we call method without params, return current user profile data.
40
        if(empty($userInfo)) return $this->getProfile();
41
42
        return $this->updateProfile($userInfo);
43
    }
44
45
    /**
46
     * Checks if current user is banned
47
     *
48
     * @return bool
49
     */
50
    public function isBanned()
51
    {
52
        $profile = $this->profile();
53
54
       return isset($profile['is_write_banned']) ?
55
           (bool)$profile['is_write_banned'] :
56
           false;
57
    }
58
59
    /**
60
     * Returns current user username
61
     *
62
     * @return string
63
     */
64
    public function username()
65
    {
66
        $profile = $this->profile();
67
68
        return isset($profile['username']) ? $profile['username'] : '';
69
    }
70
71
    /**
72
     * Deactivates your account.
73
     *
74
     * @param string $reason
75
     * @param string $explanation
76
     * @return bool
77
     */
78
    public function deactivate($reason = 'other', $explanation = '')
79
    {
80
        $profile = $this->profile();
81
82
        if(!isset($profile['id'])) return false;
83
84
        $request = [
85
            'user_id'     => $profile['id'],
86
            'reason'      => $reason,
87
            'explanation' => $explanation,
88
        ];
89
90
        return $this->post($request, UrlBuilder::RESOURCE_DEACTIVATE_ACCOUNT);
91
    }
92
93
    /**
94
     * Send invite to email
95
     * @param string $email
96
     * @return bool|Response
97
     */
98
    public function invite($email)
99
    {
100
        $data = [
101
            'email' => $email,
102
            'type'  => 'email',
103
        ];
104
105
        return $this->post($data, UrlBuilder::RESOURCE_INVITE);
106
    }
107
108
    /**
109
     * Remove things you’ve recently searched for from search suggestions.
110
     * @return bool|Response
111
     */
112
    public function clearSearchHistory()
113
    {
114
        return $this->post([], UrlBuilder::RESOURCE_CLEAR_SEARCH_HISTORY);
115
    }
116
117
    /**
118
     * Simply makes GET request to some url.
119
     * @param string $url
120
     * @return array|bool
121
     */
122
    public function visitPage($url = '')
123
    {
124
        return $this->get([], $url);
125
    }
126
127
    /**
128
     * Get your account sessions history
129
     * @return array
130
     */
131
    public function sessionsHistory()
132
    {
133
        return $this->get([], UrlBuilder::RESOURCE_SESSIONS_HISTORY);
134
    }
135
136
    /**
137
     * Get list of available locales
138
     * @return array
139
     */
140
    public function getLocales()
141
    {
142
        return $this->get([], UrlBuilder::RESOURCE_AVAILABLE_LOCALES);
143
    }
144
145
    /**
146
     * Get list of available countries
147
     * @return array
148
     */
149
    public function getCountries()
150
    {
151
        return $this->get([], UrlBuilder::RESOURCE_AVAILABLE_COUNTRIES);
152
    }
153
154
    /**
155
     * Get list of available account types
156
     * @return array
157
     */
158
    public function getAccountTypes()
159
    {
160
        return $this->get([], UrlBuilder::RESOURCE_AVAILABLE_ACCOUNT_TYPES);
161
    }
162
163
    /**
164
     * @return array
165
     */
166
    protected function getProfile()
167
    {
168
        return $this->get([], UrlBuilder::RESOURCE_GET_USER_SETTINGS);
169
    }
170
171
    /**
172
     * @param array|Profile $userInfo
173
     * @return bool|Response
174
     */
175
    protected function updateProfile($userInfo)
176
    {
177
        // If we have a form object, convert it to array
178
        if ($userInfo instanceof Profile) {
179
            $userInfo = $userInfo->toArray();
180
        }
181
182
        if (isset($userInfo['profile_image'])) {
183
            $userInfo['profile_image_url'] = $this->upload($userInfo['profile_image']);
184
        }
185
186
        return $this->post($userInfo, UrlBuilder::RESOURCE_UPDATE_USER_SETTINGS);
187
    }
188
}
189