Completed
Branch master (23259a)
by Sergey
04:17 queued 48s
created

Password   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A change() 0 9 1
B reset() 0 33 3
A parseCurrentUrl() 0 5 1
A sendResetLink() 0 5 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
6
use seregazhuk\PinterestBot\Api\Providers\Core\Provider;
7
8
class Password extends Provider
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $loginRequiredFor = [
14
        'change',
15
    ];
16
17
    /**
18
     * Ask for password reset link in email
19
     *
20
     * @param string $user Username or user mail
21
     * @return bool
22
     */
23
    public function sendResetLink($user)
24
    {
25
        return $this->post(
26
            UrlBuilder::RESOURCE_RESET_PASSWORD_SEND_LINK,
27
            ['username_or_email' => $user]
28
        );
29
    }
30
31
    /**
32
     * Set a new password by link from reset password email
33
     *
34
     * @param string $link
35
     * @param string $newPassword
36
     * @return bool
37
     */
38
    public function reset($link, $newPassword)
39
    {
40
        // Visit link to get current reset token, username and token expiration
41
        $this->get($link);
42
        $this->request->dropCookies();
43
44
        $urlData = $this->parseCurrentUrl();
45
46
        $isValidUrlData = isset($urlData['query'], $urlData['path']);
47
        if (!$isValidUrlData) {
48
            return false;
49
        }
50
51
        $username = trim(str_replace('/pw/', '', $urlData['path']), '/');
52
53
        $query = [];
54
55
        parse_str($urlData['query'], $query);
56
57
        $isValidQuery = isset($query['e'], $query['t']);
58
        if (!$isValidQuery) {
59
            return false;
60
        }
61
62
        $request = [
63
            'username'             => $username,
64
            'new_password'         => $newPassword,
65
            'new_password_confirm' => $newPassword,
66
            'token'                => $query['t'],
67
            'expiration'           => $query['e'],
68
        ];
69
70
        return $this->post(UrlBuilder::RESOURCE_RESET_PASSWORD_UPDATE, $request);
71
    }
72
73
    /**
74
     * @param string $oldPassword
75
     * @param string $newPassword
76
     * @return bool
77
     */
78
    public function change($oldPassword, $newPassword)
79
    {
80
        $request = [
81
            'old_password'         => $oldPassword,
82
            'new_password'         => $newPassword,
83
            'new_password_confirm' => $newPassword,
84
        ];
85
86
        return $this->post(UrlBuilder::RESOURCE_CHANGE_PASSWORD, $request);
87
    }
88
89
    /**
90
     * @return mixed
91
     */
92
    protected function parseCurrentUrl()
93
    {
94
        $url = $this->request->getCurrentUrl();
95
96
        return parse_url($url);
97
    }
98
}
99