Completed
Pull Request — master (#308)
by Sergey
06:18
created

Password::parseCurrentUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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