Passed
Push — master ( ff83e3...5f4a5c )
by Ehsan
03:54
created

SecurityUtility::getHashAlgorithm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Botonomous\utility;
4
5
/**
6
 * Class SecurityUtility.
7
 */
8
class SecurityUtility
9
{
10
    const DEFAULT_HASH_ALGORITHM = 'sha1';
11
12
    private $hashAlgorithm;
13
14
    /**
15
     * generate a token.
16
     *
17
     * @return string
18
     */
19 3
    public function generateToken()
20
    {
21 3
        return hash($this->getHashAlgorithm(), uniqid(mt_rand(), true));
22
    }
23
24
    /**
25
     * @param $hashAlgorithm
26
     *
27
     * @throws \Exception
28
     */
29 4
    public function setHashAlgorithm($hashAlgorithm)
30
    {
31
        /*
32
         * check if hashing algorithm is valid
33
         */
34 4
        if (!in_array($hashAlgorithm, hash_algos(), true)) {
35 1
            throw new \Exception('Hash algorithm is not valid');
36
        }
37
38 4
        $this->hashAlgorithm = $hashAlgorithm;
39 4
    }
40
41
    /**
42
     * @return string
43
     */
44 4
    public function getHashAlgorithm()
45
    {
46 4
        if (empty($this->hashAlgorithm)) {
47 4
            $this->setHashAlgorithm(self::DEFAULT_HASH_ALGORITHM);
48
        }
49
50 4
        return $this->hashAlgorithm;
51
    }
52
}
53