Completed
Push — master ( 37fe77...5f884d )
by Jonathan
03:10
created

Signature   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 65.22%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 59
ccs 15
cts 23
cp 0.6522
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addSignature() 0 4 1
A validateRequest() 0 10 3
A generateSignature() 0 7 1
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