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

Signer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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