Passed
Pull Request — master (#2)
by Florian
03:36
created

PasswordHasherCollection::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
5
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE.txt
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
12
 * @link          http://cakephp.org CakePHP(tm) Project
13
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
14
 */
15
namespace Phauthentic\PasswordHasher;
16
17
use ArrayIterator;
18
use RuntimeException;
19
use Traversable;
20
21
/**
22
 * Password hashing class that use weak hashing algorithms. This class is
23
 * intended only to be used with legacy databases where passwords have
24
 * not been migrated to a stronger algorithm yet.
25
 */
26
class PasswordHasherCollection implements PasswordHasherCollectionInterface
27
{
28
    /**
29
     * List of Hashers
30
     *
31
     * @var array
32
     */
33
    protected $hashers = [];
34
35
    /**
36
     * Constructor
37
     *
38
     * @param iterable $hashers An iterable of password hashers
39
     */
40 8
    public function __construct(iterable $hashers = [])
41
    {
42 8
        foreach ($hashers as $hasher) {
43 4
            $this->add($hasher);
44
        }
45 8
    }
46
47
    /**
48
     * Adds a password hasher to the collection
49
     *
50
     * @param \Phauthentic\PasswordHasher\PasswordHasherInterface $hasher Hasher
51
     * @return void
52
     */
53 5
    public function add(PasswordHasherInterface $hasher): void
54
    {
55 5
        $this->hashers[] = $hasher;
56 5
    }
57
58
    /**
59
     * Retrieve an external iterator
60
     *
61
     * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
62
     * @return Traversable An instance of an object implementing <b>Iterator</b> or
63
     * <b>Traversable</b>
64
     */
65 2
    public function getIterator(): Traversable
66
    {
67 2
        return new ArrayIterator($this->hashers);
68
    }
69
70
    /**
71
     * Whether a offset exists
72
     *
73
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
74
     * @param mixed $offset <p>
75
     * An offset to check for.
76
     * </p>
77
     * @return bool true on success or false on failure.
78
     * </p>
79
     * <p>
80
     * The return value will be casted to boolean if non-boolean was returned.
81
     */
82 2
    public function offsetExists($offset)
83
    {
84 2
        return isset($this->hashers[$offset]);
85
    }
86
87
    /**
88
     * Offset to retrieve
89
     *
90
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
91
     * @param mixed $offset <p>
92
     * The offset to retrieve.
93
     * </p>
94
     * @return mixed Can return all value types.
95
     */
96 3
    public function offsetGet($offset)
97
    {
98 3
        if (isset($this->hashers[$offset])) {
99 3
            return $this->hashers[$offset];
100
        }
101
    }
102
103
    /**
104
     * Offset to set
105
     *
106
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
107
     * @param mixed $offset <p>
108
     * The offset to assign the value to.
109
     * </p>
110
     * @param mixed $value <p>
111
     * The value to set.
112
     * </p>
113
     * @return void
114
     */
115 1
    public function offsetSet($offset, $value)
116
    {
117 1
        throw new RuntimeException('Use add()');
118
    }
119
120
    /**
121
     * Offset to unset
122
     *
123
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
124
     * @param mixed $offset <p>
125
     * The offset to unset.
126
     * </p>
127
     * @return void
128
     */
129
    public function offsetUnset($offset)
130
    {
131
        unset($this->hashers[$offset]);
132
    }
133
134
    /**
135
     * @inheritDoc
136
     */
137 6
    public function count(): int
138
    {
139 6
        return count($this->hashers);
140
    }
141
}
142