GmapsUrlSigner::sign()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace BenTools\GmapsApiSigner;
4
5
final class GmapsUrlSigner
6
{
7
    /**
8
     * @var string
9
     */
10
    private $secretCode;
11
12
    /**
13
     * GmapsUrlSigner constructor.
14
     * @param string $apiKey
0 ignored issues
show
Bug introduced by
There is no parameter named $apiKey. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
15
     * @param string $secretCode
16
     */
17
    public function __construct(string $secretCode)
18
    {
19
        $this->secretCode = $secretCode;
20
    }
21
22
    /**
23
     * @param string $url
24
     * @return string
25
     */
26
    public function sign(string $url): string
27
    {
28
        $segments = parse_url($url);
29
        $urlPartToSign = $segments['path'].'?'.$segments['query'];
30
        // Decode the private key into its binary format
31
        $decodedKey = base64_decode(str_replace(array('-', '_'), array('+', '/'), $this->secretCode));
32
        // Create a signature using the private key and the URL-encoded
33
        // string using HMAC SHA1. This signature will be binary.
34
        $signature = hash_hmac('sha1', $urlPartToSign, $decodedKey, true);
35
        $encodedSignature = str_replace(array('+', '/'), array('-', '_'), base64_encode($signature));
36
        return sprintf('%s&signature=%s', $url, $encodedSignature);
37
    }
38
39
    /**
40
     * Use as a callable
41
     *
42
     * @param string $url
43
     * @return string
44
     */
45
    public function __invoke(string $url): string
46
    {
47
        return $this->sign($url);
48
    }
49
}
50