Completed
Push — master ( 72430f...dafba8 )
by Jonathan
04:56
created

Signature::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 4
cp 0.75
rs 10
cc 1
nc 1
nop 1
crap 1.0156
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 24
    public function __construct($signKey)
18
    {
19 24
        $this->signKey = $signKey;
20 24
    }
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 10
    public function addSignature($path, array $params)
29
    {
30 10
        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 8
    public function validateRequest($path, array $params)
40
    {
41 8
        if (!isset($params['s'])) {
42 2
            throw new SignatureException('Signature is missing.');
43
        }
44
45 6
        if ($params['s'] !== $this->generateSignature($path, $params)) {
46 2
            throw new SignatureException('Signature is not valid.');
47
        }
48 4
    }
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 18
    public function generateSignature($path, array $params)
57
    {
58 18
        unset($params['s']);
59 18
        ksort($params);
60
61 18
        return md5($this->signKey.':'.ltrim($path, '/').'?'.http_build_query($params));
62
    }
63
}
64