Signature::generate()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 17
cts 17
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 18
nc 4
nop 1
crap 5
1
<?php
2
3
namespace Acquia\Network;
4
5
use Acquia\Rest\SignatureAbstract;
6
7
/**
8
 * Generates the HMAC hash used to sign Acquia Network requests.
9
 */
10
class Signature extends SignatureAbstract
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15 39
    public function generate($params = array())
16
    {
17 39
         if (!is_array($params)) {
18 3
             throw new \UnexpectedValueException('Expecting data to be an array.');
19
         }
20
21 36
         $time = $this->getRequestTime();
22 36
         $nonce = $this->generateNonce();
23 36
         $key = $this->getSecretKey();
24
25 36
         if (empty($params['rpc_version']) || $params['rpc_version'] < 2) {
26 30
              $string = $time . ':' . $nonce . ':' . $key . ':' . serialize($params);
27
28 30
              return base64_encode(
29 30
                  pack("H*", sha1((str_pad($key, 64, chr(0x00)) ^ (str_repeat(chr(0x5c), 64))) .
30 30
                  pack("H*", sha1((str_pad($key, 64, chr(0x00)) ^ (str_repeat(chr(0x36), 64))) .
31 30
                  $string)))));
32 6
        } elseif ($params['rpc_version'] == 2) {
33 3
              $string = $time . ':' . $nonce . ':' . json_encode($params);
34 3
              return sha1((str_pad($key, 64, chr(0x00)) ^ (str_repeat(chr(0x5c), 64))) . pack("H*", sha1((str_pad($key, 64, chr(0x00)) ^ (str_repeat(chr(0x36), 64))) . $string)));
35
        } else {
36 3
              $string = $time . ':' . $nonce;
37 3
              return sha1((str_pad($key, 64, chr(0x00)) ^ (str_repeat(chr(0x5c), 64))) . pack("H*", sha1((str_pad($key, 64, chr(0x00)) ^ (str_repeat(chr(0x36), 64))) . $string)));
38
        }
39
    }
40
}
41