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

Signer::sign()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 2
nop 2
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