Passed
Push — master ( 46e795...e02900 )
by Florian
03:56 queued 02:04
created

AbstractPasswordHasher   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 16
c 0
b 0
f 0
dl 0
loc 63
ccs 15
cts 15
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setSalt() 0 7 1
A checkSaltPositionArgument() 0 8 2
A needsRehash() 0 3 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 InvalidArgumentException;
18
19
/**
20
 * Abstract password hashing class
21
 */
22
abstract class AbstractPasswordHasher implements PasswordHasherInterface
23
{
24
    const SALT_BEFORE = 'before';
25
    const SALT_AFTER = 'after';
26
27
    /**
28
     * Salt
29
     *
30
     * @return void
31
     */
32
    protected $salt = '';
33
34
    /**
35
     * Position of the salt
36
     *
37
     * @var string
38
     */
39
    protected $saltPosition;
40
41
    /**
42
     * Sets the salt if any was used
43
     *
44
     * @return $this
45
     */
46 3
    public function setSalt(string $salt, string $position = self::SALT_AFTER): self
47
    {
48 3
        $this->checkSaltPositionArgument($position);
49 2
        $this->salt = $salt;
50 2
        $this->saltPosition = $position;
51
52 2
        return $this;
53
    }
54
55
    /**
56
     * Checks the salt position argument
57
     *
58
     * @return void
59
     */
60 3
    protected function checkSaltPositionArgument($position): void
61
    {
62 3
        if (!in_array($position, [self::SALT_BEFORE, self::SALT_AFTER])) {
63 1
            throw new InvalidArgumentException(sprintf(
64 1
                '`%s` is an invalud argument, it has to be `` or ``',
65 1
                $position,
66 1
                self::SALT_BEFORE,
67 1
                self::SALT_AFTER
68
            ));
69
        }
70 2
    }
71
72
    /**
73
     * Returns true if the password need to be rehashed, due to the password being
74
     * created with anything else than the passwords generated by this class.
75
     *
76
     * Returns true by default since the only implementation users should rely
77
     * on is the one provided by default in php 5.5+ or any compatible library
78
     *
79
     * @param string $password The password to verify
80
     * @return bool
81
     */
82 1
    public function needsRehash(string $password): bool
83
    {
84 1
        return password_needs_rehash($password, PASSWORD_DEFAULT);
85
    }
86
}
87