Completed
Pull Request — master (#126)
by Sergey
07:37
created

User::convertToBusiness()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 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
    public function isLoggedIn()
138
    {
139
        return $this->request->isLoggedIn();
140
    }
141
142
    /**
143
     * Validates password and login.
144
     *
145
     * @param string $username
146
     * @param string $password
147
     */
148
    protected function checkCredentials($username, $password)
149
    {
150
        if (!$username || !$password) {
151
            throw new \LogicException('You must set username and password to login.');
152
        }
153
    }
154
155
    protected function completeRegistration()
156
    {
157
        $this->request->setTokenFromCookies();
158
159
        return $this->execPostRequest(
160
            ['placed_experience_id' => self::REGISTRATION_COMPLETE_EXPERIENCE_ID], UrlHelper::RESOURCE_REGISTRATION_COMPLETE
161
        );
162
    }
163
164
    /**
165
     * @param array $data
166
     * @return bool|mixed
167
     * @throws AuthException
168
     */
169
    protected function makeRegisterCall($data)
170
    {
171
        $this->execGetRequest([], '');
172
        $this->request->setTokenFromCookies();
173
174
        if (!$this->execPostRequest($data, UrlHelper::RESOURCE_CREATE_REGISTER)) {
175
            return false;
176
        }
177
178
        return $this->completeRegistration();
179
    }
180
}
181