|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HelePartnerSyncApi\Request; |
|
4
|
|
|
|
|
5
|
|
|
use HelePartnerSyncApi\Validator; |
|
6
|
|
|
use HelePartnerSyncApi\ValidatorException; |
|
7
|
|
|
|
|
8
|
|
|
class Request |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
private $data; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $method; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $expectedVersion; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $jsonData |
|
28
|
|
|
* @param string $secret |
|
29
|
|
|
* @param string $signature |
|
30
|
|
|
* @param string $signatureAlgorithm |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct($jsonData, $secret, $signature, $signatureAlgorithm) |
|
33
|
|
|
{ |
|
34
|
|
|
$data = json_decode($jsonData, true); |
|
35
|
|
|
if (!is_array($data)) { |
|
36
|
|
|
throw new RequestException('Invalid JSON in request.'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if (!in_array($signatureAlgorithm, hash_algos(), true)) { |
|
40
|
|
|
throw new RequestException(sprintf('Unknown signature algorithm (%s) in request.', $signatureAlgorithm)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if (hash_hmac($signatureAlgorithm, $jsonData, $secret) !== $signature) { |
|
44
|
|
|
throw new RequestException('Signature in request is invalid.'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
try { |
|
48
|
|
|
Validator::checkStructure($data, array( |
|
49
|
|
|
'expectedVersion' => Validator::TYPE_STRING, |
|
50
|
|
|
'method' => Validator::TYPE_STRING, |
|
51
|
|
|
'data' => Validator::TYPE_ARRAY, |
|
52
|
|
|
)); |
|
53
|
|
|
|
|
54
|
|
|
} catch (ValidatorException $e) { |
|
55
|
|
|
throw new RequestException('Invalid request: ' . $e->getMessage(), $e); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$this->data = $data['data']; |
|
59
|
|
|
$this->method = $data['method']; |
|
60
|
|
|
$this->expectedVersion = $data['expectedVersion']; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getMethod() |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->method; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return array |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getData() |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->data; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getExpectedVersion() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->expectedVersion; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|