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

PasswordHasherCollection::count()   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 Traversable;
19
20
/**
21
 * Password hashing class that use weak hashing algorithms. This class is
22
 * intended only to be used with legacy databases where passwords have
23
 * not been migrated to a stronger algorithm yet.
24
 */
25
class PasswordHasherCollection implements PasswordHasherCollectionInterface
26
{
27
    /**
28
     * List of Hashers
29
     *
30
     * @var array
31
     */
32
    protected $hashers = [];
33
34
    /**
35
     * Constructor
36
     *
37
     * @param iterable $hashers An iterable of password hashers
38
     */
39 4
    public function __construct(iterable $hashers = [])
40
    {
41 4
        foreach ($hashers as $hasher) {
42 4
            $this->add($hasher);
43
        }
44 4
    }
45
46
    /**
47
     * Adds a password hasher to the collection
48
     *
49
     * @param \Phauthentic\PasswordHasher\PasswordHasherInterface $hasher Hasher
50
     * @return void
51
     */
52 4
    public function add(PasswordHasherInterface $hasher): void
53
    {
54 4
        $this->hashers[] = $hasher;
55 4
    }
56
57
    /**
58
     * Retrieve an external iterator
59
     *
60
     * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
61
     * @return Traversable An instance of an object implementing <b>Iterator</b> or
62
     * <b>Traversable</b>
63
     */
64 2
    public function getIterator(): Traversable
65
    {
66 2
        return new ArrayIterator($this->hashers);
67
    }
68
69
    /**
70
     * Whether a offset exists
71
     *
72
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
73
     * @param mixed $offset <p>
74
     * An offset to check for.
75
     * </p>
76
     * @return bool true on success or false on failure.
77
     * </p>
78
     * <p>
79
     * The return value will be casted to boolean if non-boolean was returned.
80
     */
81
    public function offsetExists($offset)
82
    {
83
        return isset($this->hashers[$offset]);
84
    }
85
86
    /**
87
     * Offset to retrieve
88
     *
89
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
90
     * @param mixed $offset <p>
91
     * The offset to retrieve.
92
     * </p>
93
     * @return mixed Can return all value types.
94
     */
95 2
    public function offsetGet($offset)
96
    {
97 2
        if (isset($this->hashers[$offset])) {
98 2
            return $this->hashers[$offset];
99
        }
100
    }
101
102
    /**
103
     * Offset to set
104
     *
105
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
106
     * @param mixed $offset <p>
107
     * The offset to assign the value to.
108
     * </p>
109
     * @param mixed $value <p>
110
     * The value to set.
111
     * </p>
112
     * @return void
113
     */
114
    public function offsetSet($offset, $value)
115
    {
116
        $this->hashers[$offset] = $value;
117
    }
118
119
    /**
120
     * Offset to unset
121
     *
122
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
123
     * @param mixed $offset <p>
124
     * The offset to unset.
125
     * </p>
126
     * @return void
127
     */
128
    public function offsetUnset($offset)
129
    {
130
        unset($this->hashers[$offset]);
131
    }
132
133
    /**
134
     * @inheritDoc
135
     */
136 4
    public function count(): int
137
    {
138 4
        return count($this->hashers);
139
    }
140
}
141