UserEncoder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace TreeHouse\BaseApiBundle\Security;
4
5
use Symfony\Component\Security\Core\Util\StringUtils;
6
7
class UserEncoder
8
{
9
    const HASH_DELIMITER = ':';
10
11
    /**
12
     * @var string
13
     */
14
    protected $secret;
15
16
    /**
17
     * @param string $secret
18
     */
19 8
    public function __construct($secret)
20
    {
21 8
        $this->secret = $secret;
22 8
    }
23
24
    /**
25
     * @param string $hash
26
     *
27
     * @return array
28
     */
29 2
    public function decodeHash($hash)
30
    {
31 2
        return explode(self::HASH_DELIMITER, base64_decode($hash));
32
    }
33
34
    /**
35
     * @param array $parts
36
     *
37
     * @return string
38
     */
39 4
    public function encodeHash(array $parts)
40
    {
41 4
        return base64_encode(implode(self::HASH_DELIMITER, $parts));
42
    }
43
44
    /**
45
     * @param string $username
46
     * @param string $password
47
     *
48
     * @return string
49
     */
50 4
    public function generateHash($username, $password)
51
    {
52 4
        return sha1($username . $password . $this->secret);
53
    }
54
55
    /**
56
     * @see StringUtils::equals
57
     */
58 2
    public function compareHashes($hash1, $hash2)
59
    {
60 2
        return StringUtils::equals($hash1, $hash2);
61
    }
62
63
    /**
64
     * @param string $username
65
     * @param string $password
66
     *
67
     * @return string
68
     */
69 2
    public function generateTokenValue($username, $password)
70
    {
71 2
        return $this->encodeHash([
72 2
            base64_encode($username),
73 2
            $this->generateHash($username, $password),
74 1
        ]);
75
    }
76
}
77