Passed
Pull Request — master (#5)
by Florian
02:57
created

AbstractPasswordHasher::saltPassword()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
0 ignored issues
show
Coding Style introduced by
Header blocks must be separated by a single blank line
Loading history...
2
declare(strict_types=1);
0 ignored issues
show
Coding Style introduced by
Header blocks must be separated by a single blank line
Loading history...
3
/**
0 ignored issues
show
Coding Style introduced by
The file-level docblock must follow the opening PHP tag in the file header
Loading history...
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
 */
0 ignored issues
show
Coding Style introduced by
Header blocks must be separated by a single blank line
Loading history...
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';
0 ignored issues
show
Coding Style introduced by
Visibility must be declared on all constants if your project supports PHP 7.1 or later
Loading history...
25
    const SALT_AFTER = 'after';
0 ignored issues
show
Coding Style introduced by
Visibility must be declared on all constants if your project supports PHP 7.1 or later
Loading history...
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 8
    public function setSalt(string $salt, string $position = self::SALT_AFTER): self
47
    {
48 8
        $this->checkSaltPositionArgument($position);
49 6
        $this->salt = $salt;
50 6
        $this->saltPosition = $position;
51
52 6
        return $this;
53
    }
54
55
    /**
56
     * Checks the salt position argument
57
     *
58
     * @return void
59
     */
60 8
    protected function checkSaltPositionArgument($position): void
61
    {
62 8
        if (!in_array($position, [self::SALT_BEFORE, self::SALT_AFTER])) {
63 2
            throw new InvalidArgumentException(sprintf(
64 2
                '`%s` is an invalud argument, it has to be `` or ``',
65
                $position,
66 2
                self::SALT_BEFORE,
67 2
                self::SALT_AFTER
68
            ));
69
        }
70 6
    }
71
72
    /**
73
     * Adds the salt to a password
74
     *
75
     * @param string $password Password to salt
76
     * @return string Salted password
77
     */
78 22
    protected function saltPassword(string $password): string
79
    {
80 22
        if (empty($this->salt)) {
81 22
            return $password;
82
        }
83
84 4
        if ($this->saltPosition === self::SALT_BEFORE) {
85 2
            return $this->salt . $password;
86
        }
87
88 4
        return $password . $this->salt;
89
    }
90
91
    /**
92
     * Returns true if the password need to be rehashed, due to the password being
93
     * created with anything else than the passwords generated by this class.
94
     *
95
     * Returns true by default since the only implementation users should rely
96
     * on is the one provided by default in php 5.5+ or any compatible library
97
     *
98
     * @param string $password The password to verify
99
     * @return bool
100
     */
101 2
    public function needsRehash(string $password): bool
102
    {
103 2
        return password_needs_rehash($password, PASSWORD_DEFAULT);
104
    }
105
}
106