PasswordManager::addPassword()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
namespace Nubs\PwMan;
3
4
use Exception;
5
6
/**
7
 * Manage the collection of passwords.
8
 */
9
class PasswordManager
10
{
11
    /** @type array<array> The passwords. */
12
    private $_passwords;
13
14
    /**
15
     * Initialize the password manager.
16
     *
17
     * @api
18
     * @param array<array> The passwords.
19
     */
20
    public function __construct(array $passwords)
21
    {
22
        $this->_passwords = $passwords;
23
    }
24
25
    /**
26
     * Find all the passwords that match the given application name.
27
     *
28
     * The application name is used as a regex.
29
     *
30
     * @api
31
     * @param string $application The application name.
32
     * @return array<array> The passwords for the applications that match the
33
     *     application.
34
     */
35
    public function matchingApplication($application)
36
    {
37
        $result = [];
38
        foreach ($this->_passwords as $key => $value) {
39
            if (preg_match("/{$application}/i", $key)) {
40
                $result[$key] = $value;
41
            }
42
        }
43
44
        return $result;
45
    }
46
47
    /**
48
     * Add the new application to the password list.
49
     *
50
     * @api
51
     * @param string $name The unique application name.
52
     * @param array $newApplication The application information.
53
     * @return void
54
     */
55
    public function addPassword($name, array $newApplication)
56
    {
57
        if (isset($this->_passwords[$name])) {
58
            throw new Exception("Password already exists for {$name}");
59
        }
60
61
        $this->_passwords[$name] = $newApplication;
62
    }
63
64
    /**
65
     * Remove the given password by unique name.
66
     *
67
     * @api
68
     * @param string $name The unique application name.
69
     * @return void
70
     */
71
    public function removePassword($name)
72
    {
73
        unset($this->_passwords[$name]);
74
    }
75
76
    /**
77
     * Replace one set of passwords with a new set.
78
     *
79
     * Removes all the passwords in $old, and adds all the passwords in $new.
80
     *
81
     * @api
82
     * @param array $old The old passwords to replace.
83
     * @param array $new The new passwords to use instead.
84
     * @return void
85
     */
86
    public function replacePasswords(array $old, array $new)
87
    {
88
        array_map([$this, 'removePassword'], array_keys($old));
89
90
        foreach ($new as $name => $spec) {
91
            $this->addPassword($name, $spec);
92
        }
93
94
        $this->sortPasswords();
95
    }
96
97
    /**
98
     * Get the passwords.
99
     *
100
     * @api
101
     * @return array<array> The passwords.
102
     */
103
    public function getPasswords()
104
    {
105
        return $this->_passwords;
106
    }
107
108
    /**
109
     * Sort the passwords.
110
     *
111
     * @api
112
     * @return void
113
     */
114
    public function sortPasswords()
115
    {
116
        ksort($this->_passwords);
117
    }
118
}
119