PwnedPasswords   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setPassword() 0 4 1
A __construct() 0 3 1
A get() 0 15 3
1
<?php
2
3
namespace Ridvanbaluyos\Pwned;
4
5
use \Ridvanbaluyos\Pwned\Pwned as Pwned;
6
/**
7
 * Getting all data classes in the system
8
 * https://haveibeenpwned.com/API/v2#PwnedPasswords
9
 *
10
 * @package    Pwned
11
 * @author     Ridvan Baluyos <[email protected]>
12
 * @link       https://github.com/ridvanbaluyos/haveibeenpwned
13
 * @license    MIT
14
 */
15
class PwnedPasswords extends Pwned
16
{
17
    private $password;
18
19
    /**
20
     * PwnedPasswords constructor.
21
     */
22
    public function __construct()
23
    {
24
        parent::__construct();
25
    }
26
27
    /**
28
     * This function gets all pawned passwords.
29
     *
30
     * @return array
31
     */
32
    public function get(): array
33
    {
34
        // No password has been set, return empty array.
35
        if (!isset($this->password)) return [];
36
37
        $url = $this->endpoint . 'pwnedpassword/' . $this->password;
38
        $pwnedPasswords = $this->requestGet($url);
39
40
        if (empty($pwnedPasswords)) {
41
            return [
42
                'code' => 200,
43
                'message' =>'Oh no — pwned! This password has previously appeared in a data breach and should never be used. If you\'ve ever used it anywhere before, change it immediately!'
44
            ];
45
        }
46
        return $pwnedPasswords;
47
    }
48
49
    /**
50
     * Sets the password.
51
     *
52
     * @param string $password
53
     * @return $this
54
     */
55
    public function setPassword(string $password)
56
    {
57
        $this->password = $password;
58
        return $this;
59
    }
60
}