Completed
Push — master ( 38cba2...37650a )
by Sergey
11:24 queued 07:53
created

User   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 11
Bugs 4 Features 5
Metric Value
wmc 16
c 11
b 4
f 5
lcom 1
cbo 6
dl 0
loc 174
rs 10

10 Methods

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