Completed
Push — master ( adc0bd...a52ed1 )
by Sergey
05:25 queued 02:40
created

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