Test Failed
Push — master ( 2a75d8...e4b8e4 )
by Julien
12:49
created

Hash   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 49
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hash() 0 14 1
A checkHash() 0 23 3
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc\Model;
13
14
use Phalcon\Di\DiInterface;
15
use Phalcon\Security;
16
use Zemit\Config\ConfigInterface;
17
18
trait Hash
19
{
20
    abstract public function getDI(): DiInterface;
21
    
22
    /**
23
     * Hash string
24
     */
25
    public function hash(string $string, ?string $salt = null, ?string $workFactor = null): string
26
    {
27
        $config = $this->getDI()->get('config');
28
        assert($config instanceof ConfigInterface);
29
        
30
        $security = $this->getDI()->get('security');
31
        assert($security instanceof Security);
32
        
33
        // Get salt & workFactor
34
        $salt ??= $config->path('security.salt') ?? '';
35
        $workFactor ??= $config->path('security.workFactor') ?? 10;
36
        
37
        // return salted hash
38
        return $security->hash($salt . $string, $workFactor);
0 ignored issues
show
Bug introduced by
It seems like $workFactor can also be of type string; however, parameter $workFactor of Phalcon\Security::hash() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
        return $security->hash($salt . $string, /** @scrutinizer ignore-type */ $workFactor);
Loading history...
39
    }
40
    
41
    /**
42
     * Check hash
43
     */
44
    public function checkHash(?string $hash = null, ?string $string = null, int $maxPassLength = 0): bool
45
    {
46
        // hash empty, not valid
47
        if (empty($hash)) {
48
            return false;
49
        }
50
        
51
        // string empty not valid
52
        if (empty($string)) {
53
            return false;
54
        }
55
        
56
        $config = $this->getDI()->get('config');
57
        assert($config instanceof ConfigInterface);
58
        
59
        $security = $this->getDI()->get('security');
60
        assert($security instanceof Security);
61
        
62
        // Get salt
63
        $salt = $config->path('security.salt') ?? '';
64
        
65
        // check hash
66
        return $security->checkHash($salt . $string, $hash, $maxPassLength);
67
    }
68
}
69