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

SecurityUtility   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 45
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generateToken() 0 4 1
A setHashAlgorithm() 0 11 2
A getHashAlgorithm() 0 8 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