Completed
Pull Request — master (#732)
by 12345
03:41
created

Signer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 37
wmc 5
c 1
b 0
f 0
lcom 1
cbo 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A check() 0 4 1
A sign() 0 10 3
1
<?php
2
3
namespace Liip\ImagineBundle\Imagine\Cache;
4
5
class Signer implements SignerInterface
6
{
7
    /**
8
     * @var string
9
     */
10
    private $secret;
11
12
    /**
13
     * @param string $secret
14
     */
15
    public function __construct($secret)
16
    {
17
        $this->secret = $secret;
18
    }
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function sign($path, array $runtimeConfig = null)
24
    {
25
        if ($runtimeConfig) {
26
            array_walk_recursive($runtimeConfig, function (&$value) {
27
                $value = (string) $value;
28
            });
29
        }
30
31
        return substr(preg_replace('/[^a-zA-Z0-9-_]/', '', base64_encode(hash_hmac('sha256', ltrim($path, '/').(null === $runtimeConfig ?: serialize($runtimeConfig)), $this->secret, true))), 0, 8);
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function check($hash, $path, array $runtimeConfig = null)
38
    {
39
        return $hash === $this->sign($path, $runtimeConfig);
40
    }
41
}
42