Completed
Pull Request — master (#204)
by Sergey
03:18
created

Auth   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 171
c 0
b 0
f 0
wmc 17
lcom 1
cbo 2
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A login() 0 14 4
A logout() 0 4 1
A register() 0 13 1
A registerBusiness() 0 12 1
A isLoggedIn() 0 4 1
A checkCredentials() 0 6 3
A completeRegistration() 0 9 1
A makeRegisterCall() 0 11 2
A processLogin() 0 17 2
A visitMainPage() 0 4 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use seregazhuk\PinterestBot\Api\Response;
6
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
7
8
class Auth extends Provider
9
{
10
11
    /**
12
     * @var array
13
     */
14
    protected $loginRequiredFor = [];
15
16
    const REGISTRATION_COMPLETE_EXPERIENCE_ID = '11:10105';
17
    const ACCOUNT_TYPE_OTHER = 'other';
18
19
    /**
20
     * Login as pinner.
21
     *
22
     * @param string $username
23
     * @param string $password
24
     *
25
     * @param bool $autoLogin
26
     * @return bool
27
     */
28
    public function login($username, $password, $autoLogin = true)
29
    {
30
        if ($this->request->isLoggedIn()) return true;
31
32
        $this->checkCredentials($username, $password);
33
34
        // Trying to load previously saved cookies from last login
35
        // session for this username.
36
        if($autoLogin && $this->request->autoLogin($username)) {
37
            return true;
38
        }
39
40
        return $this->processLogin($username, $password);
41
    }
42
43
    /**
44
     * If $removeCookies is set, cookie file will be removed
45
     * from the file system.
46
     *
47
     * @param bool $removeCookies
48
     */
49
    public function logout($removeCookies = false)
50
    {
51
        $this->request->logout($removeCookies);
52
    }
53
54
    /**
55
     * Register a new user.
56
     *
57
     * @param string $email
58
     * @param string $password
59
     * @param string $name
60
     * @param string $country
61
     * @param int $age
62
     *
63
     * @return bool
64
     */
65
    public function register($email, $password, $name, $country = 'GB', $age = 18)
66
    {
67
        $data = [
68
            "age"        => $age,
69
            "email"      => $email,
70
            "password"   => $password,
71
            "country"    => $country,
72
            "first_name" => $name,
73
            "container"  => 'home_page',
74
        ];
75
76
        return $this->makeRegisterCall($data);
77
    }
78
79
    /**
80
     * Register a new business account.
81
     *
82
     * @param string $email
83
     * @param string $password
84
     * @param string $businessName
85
     * @param string $website
86
     * @return bool|mixed
87
     */
88
    public function registerBusiness($email, $password, $businessName, $website = '')
89
    {
90
        $data = [
91
            "email"         => $email,
92
            "password"      => $password,
93
            "website_url"   => $website,
94
            "business_name" => $businessName,
95
            "account_type"  => self::ACCOUNT_TYPE_OTHER,
96
        ];
97
98
        return $this->makeRegisterCall($data);
99
    }
100
101
    /**
102
     * @return bool
103
     */
104
    public function isLoggedIn()
105
    {
106
        return $this->request->isLoggedIn();
107
    }
108
109
    /**
110
     * Validates password and login.
111
     *
112
     * @param string $username
113
     * @param string $password
114
     */
115
    protected function checkCredentials($username, $password)
116
    {
117
        if (!$username || !$password) {
118
            throw new \LogicException('You must set username and password to login.');
119
        }
120
    }
121
122
    /**
123
     * @return bool
124
     */
125
    protected function completeRegistration()
126
    {
127
        $this->request->setTokenFromCookies();
128
129
        return $this->execPostRequest(
130
            ['placed_experience_id' => self::REGISTRATION_COMPLETE_EXPERIENCE_ID],
131
            UrlBuilder::RESOURCE_REGISTRATION_COMPLETE
132
        );
133
    }
134
135
    /**
136
     * @param array $data
137
     * @return bool|mixed
138
     */
139
    protected function makeRegisterCall($data)
140
    {
141
        $this->visitMainPage();
142
        $this->request->setTokenFromCookies();
143
144
        if (!$this->execPostRequest($data, UrlBuilder::RESOURCE_CREATE_REGISTER)) {
145
            return false;
146
        }
147
148
        return $this->completeRegistration();
149
    }
150
151
    /**
152
     * @param string $username
153
     * @param string $password
154
     * @return bool
155
     */
156
    protected function processLogin($username, $password)
157
    {
158
        $this->request->clearToken();
159
160
        $credentials = [
161
            'username_or_email' => $username,
162
            'password'          => $password,
163
        ];
164
165
        $response = $this->execPostRequest($credentials, UrlBuilder::RESOURCE_LOGIN, true);
166
167
        if (!$response->isOk()) return false;
168
169
        $this->request->login();
170
171
        return true;
172
    }
173
174
    public function visitMainPage()
175
    {
176
        $this->execGetRequest([], '');
177
    }
178
}