Signature::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace League\Glide\Signatures;
4
5
class Signature implements SignatureInterface
6
{
7
    /**
8
     * Secret key used to generate signature.
9
     * @var string
10
     */
11
    protected $signKey;
12
13
    /**
14
     * Create Signature instance.
15
     * @param string $signKey Secret key used to generate signature.
16
     */
17
    public function __construct($signKey)
18
    {
19
        $this->signKey = $signKey;
20
    }
21
22
    /**
23
     * Add an HTTP signature to manipulation parameters.
24
     * @param  string $path   The resource path.
25
     * @param  array  $params The manipulation parameters.
26
     * @return array  The updated manipulation parameters.
27
     */
28
    public function addSignature($path, array $params)
29
    {
30
        return array_merge($params, ['s' => $this->generateSignature($path, $params)]);
31
    }
32
33
    /**
34
     * Validate a request signature.
35
     * @param  string             $path   The resource path.
36
     * @param  array              $params The manipulation params.
37
     * @throws SignatureException
38
     */
39
    public function validateRequest($path, array $params)
40
    {
41
        if (!isset($params['s'])) {
42
            throw new SignatureException('Signature is missing.');
43
        }
44
45
        if ($params['s'] !== $this->generateSignature($path, $params)) {
46
            throw new SignatureException('Signature is not valid.');
47
        }
48
    }
49
50
    /**
51
     * Generate an HTTP signature.
52
     * @param  string $path   The resource path.
53
     * @param  array  $params The manipulation parameters.
54
     * @return string The generated HTTP signature.
55
     */
56
    public function generateSignature($path, array $params)
57
    {
58
        unset($params['s']);
59
        ksort($params);
60
61
        return md5($this->signKey.':'.ltrim($path, '/').'?'.http_build_query($params));
62
    }
63
}
64