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

Password::sendPasswordResetLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 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 Password extends Provider
9
{
10
    /**
11
     * Ask for password reset link in email
12
     *
13
     * @param string $user Username or user mail
14
     * @return bool
15
     */
16
    public function sendPasswordResetLink($user)
17
    {
18
        $request = ['username_or_email' => $user];
19
20
        return $this->execPostRequest($request, UrlBuilder::RESOURCE_RESET_PASSWORD_SEND_LINK);
21
    }
22
23
    /**
24
     * Set a new password by link from reset password email
25
     *
26
     * @param string $link
27
     * @param string $newPassword
28
     * @return bool|Response
29
     */
30
    public function resetPassword($link, $newPassword)
31
    {
32
        // Visit link to get current reset token, username and token expiration
33
        $this->execGetRequest([], $link);
34
        $this->request->clearToken();
35
36
        $passwordResetUrl = $this->request->getHttpClient()->getCurrentUrl();
37
38
        $urlData = parse_url($passwordResetUrl);
39
        $username = trim(str_replace('/pw/', '', $urlData['path']), '/');
40
41
        $query = [];
42
        parse_str($urlData['query'], $query);
43
44
45
        return $this->execPostRequest([
46
            'username'             => $username,
47
            'new_password'         => $newPassword,
48
            'new_password_confirm' => $newPassword,
49
            'token'                => $query['t'],
50
            'expiration'           => $query['e'],
51
        ], UrlBuilder::RESOURCE_RESET_PASSWORD_UPDATE, true);
52
    }
53
54
    /**
55
     * @param string $oldPassword
56
     * @param string $newPassword
57
     * @return bool
58
     */
59
    public function changePassword($oldPassword, $newPassword)
60
    {
61
        $request = [
62
            'old_password'         => $oldPassword,
63
            'new_password'         => $newPassword,
64
            'new_password_confirm' => $newPassword,
65
        ];
66
67
        return $this->execPostRequest($request, UrlBuilder::RESOURCE_CHANGE_PASSWORD);
68
    }
69
}