Completed
Push — master ( 2d0d9b...2037bb )
by Sergey
05:22 queued 02:31
created

User::isBanned()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 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
        'convertToBusiness',
25
    ];
26
27
    /**
28
     * Updates or returns user profile info. Gets associative array as a param. Available keys of array are:
29
     * 'last_name', 'first_name', 'username', 'about', 'location' and 'website_url'.
30
     * You can also change user avatar by passing 'profile_image'.
31
     *
32
     * @param array $userInfo If empty returns current user profile.
33
     *
34
     * @return bool|array|Profile
35
     */
36
    public function profile($userInfo = null)
37
    {
38
        // If we call method without params, return current user profile data.
39
        if(empty($userInfo)) return $this->getProfile();
40
41
        return $this->updateProfile($userInfo);
42
    }
43
44
    /**
45
     * Checks if current user is banned
46
     *
47
     * @return bool
48
     */
49
    public function isBanned()
50
    {
51
        $profile = $this->profile();
52
53
       return isset($profile['is_write_banned']) ?
54
           (bool)$profile['is_write_banned'] :
55
           false;
56
    }
57
58
    /**
59
     * Returns current user username
60
     *
61
     * @return string
62
     */
63
    public function username()
64
    {
65
        $profile = $this->profile();
66
67
        return isset($profile['username']) ? $profile['username'] : '';
68
    }
69
70
    /**
71
     * Deactivates your account.
72
     *
73
     * @param string $reason
74
     * @param string $explanation
75
     * @return bool
76
     */
77
    public function deactivate($reason = 'other', $explanation = '')
78
    {
79
        $profile = $this->profile();
80
81
        if(!isset($profile['id'])) return false;
82
83
        $request = [
84
            'user_id'     => $profile['id'],
85
            'reason'      => $reason,
86
            'explanation' => $explanation,
87
        ];
88
89
        return $this->post($request, UrlBuilder::RESOURCE_DEACTIVATE_ACCOUNT);
90
    }
91
92
    /**
93
     * Send invite to email
94
     * @param string $email
95
     * @return bool|Response
96
     */
97
    public function invite($email)
98
    {
99
        $data = [
100
            'email' => $email,
101
            'type'  => 'email',
102
        ];
103
104
        return $this->post($data, UrlBuilder::RESOURCE_INVITE);
105
    }
106
107
    /**
108
     * Remove things you’ve recently searched for from search suggestions.
109
     * @return bool|Response
110
     */
111
    public function clearSearchHistory()
112
    {
113
        return $this->post([], UrlBuilder::RESOURCE_CLEAR_SEARCH_HISTORY);
114
    }
115
116
    /**
117
     * @return array|bool|Response
118
     */
119
    protected function getProfile()
120
    {
121
        return $this->get([], UrlBuilder::RESOURCE_GET_USER_SETTINGS);
122
    }
123
124
    /**
125
     * @param array|Profile $userInfo
126
     * @return bool|Response
127
     */
128
    protected function updateProfile($userInfo)
129
    {
130
        // If we have a form object, convert it to array
131
        if ($userInfo instanceof Profile) {
132
            $userInfo = $userInfo->toArray();
133
        }
134
135
        if (isset($userInfo['profile_image'])) {
136
            $userInfo['profile_image_url'] = $this->upload($userInfo['profile_image']);
137
        }
138
139
        return $this->post($userInfo, UrlBuilder::RESOURCE_UPDATE_USER_SETTINGS);
140
    }
141
}
142