SecurityUtility   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 49
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
use Botonomous\BotonomousException;
6
7
/**
8
 * Class SecurityUtility.
9
 */
10
class SecurityUtility
11
{
12
    const DEFAULT_HASH_ALGORITHM = 'sha1';
13
14
    private $hashAlgorithm;
15
16
    /**
17
     * generate a token.
18
     *
19
     * @throws \Exception
20
     *
21
     * @return string
22
     */
23 3
    public function generateToken(): string
24
    {
25 3
        return hash($this->getHashAlgorithm(), uniqid(mt_rand(), true));
26
    }
27
28
    /**
29
     * @param string $hashAlgorithm
30
     *
31
     * @throws \Exception
32
     */
33 4
    public function setHashAlgorithm(string $hashAlgorithm)
34
    {
35
        /*
36
         * check if hashing algorithm is valid
37
         */
38 4
        if (!in_array($hashAlgorithm, hash_algos(), true)) {
39 1
            throw new BotonomousException('Hash algorithm is not valid');
40
        }
41
42 4
        $this->hashAlgorithm = $hashAlgorithm;
43 4
    }
44
45
    /**
46
     * @throws \Exception
47
     *
48
     * @return string
49
     */
50 4
    public function getHashAlgorithm(): string
51
    {
52 4
        if (empty($this->hashAlgorithm)) {
53 4
            $this->setHashAlgorithm(self::DEFAULT_HASH_ALGORITHM);
54
        }
55
56 4
        return $this->hashAlgorithm;
57
    }
58
}
59