Completed
Push — master ( e60207...37fe77 )
by Jonathan
05:38
created

Signature   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 59
ccs 15
cts 15
cp 1
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 36
    public function __construct($signKey)
18
    {
19 36
        $this->signKey = $signKey;
20 36
    }
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 15
    public function addSignature($path, array $params)
29
    {
30 15
        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 12
    public function validateRequest($path, array $params)
40
    {
41 12
        if (!isset($params['s'])) {
42 3
            throw new SignatureException('Signature is missing.');
43
        }
44
45 9
        if ($params['s'] !== $this->generateSignature($path, $params)) {
46 3
            throw new SignatureException('Signature is not valid.');
47
        }
48 6
    }
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 27
    public function generateSignature($path, array $params)
57
    {
58 27
        unset($params['s']);
59 27
        ksort($params);
60
61 27
        return md5($this->signKey.':'.ltrim($path, '/').'?'.http_build_query($params));
62
    }
63
}
64