|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HelePartnerSyncApi; |
|
4
|
|
|
|
|
5
|
|
|
use Exception as BaseException; |
|
6
|
|
|
use HelePartnerSyncApi\Method\Method; |
|
7
|
|
|
use HelePartnerSyncApi\Request\RequestFactory; |
|
8
|
|
|
use HelePartnerSyncApi\Response\ErrorResponse; |
|
9
|
|
|
use HelePartnerSyncApi\Response\SuccessResponse; |
|
10
|
|
|
use Throwable; |
|
11
|
|
|
|
|
12
|
|
|
class Client |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
const VERSION = '1.0.0'; |
|
16
|
|
|
|
|
17
|
|
|
const HEADER_SIGNATURE = 'X-Hele-Signature'; |
|
18
|
|
|
const HEADER_SIGNATURE_ALGORITHM = 'X-Hele-Signature-Algorithm'; |
|
19
|
|
|
|
|
20
|
|
|
const SIGNATURE_ALGORITHM = 'sha1'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
private $secret; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Method[] |
|
29
|
|
|
*/ |
|
30
|
|
|
private $methods; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var RequestFactory |
|
34
|
|
|
*/ |
|
35
|
|
|
private $requestFactory; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $secret |
|
39
|
|
|
* @param RequestFactory $requestFactory |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct($secret, RequestFactory $requestFactory) |
|
42
|
|
|
{ |
|
43
|
|
|
Validator::checkString($secret); |
|
44
|
|
|
|
|
45
|
|
|
$this->secret = $secret; |
|
46
|
|
|
$this->requestFactory = $requestFactory; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function registerMethod(Method $method) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->methods[$method->getName()] = $method; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return SuccessResponse|ErrorResponse |
|
56
|
|
|
*/ |
|
57
|
|
|
public function run() |
|
58
|
|
|
{ |
|
59
|
|
|
try { |
|
60
|
|
|
$request = $this->requestFactory->createRequest(); |
|
61
|
|
|
|
|
62
|
|
|
if ($request->getExpectedVersion() !== self::VERSION) { |
|
63
|
|
|
throw new ClientException(sprintf('Server expected version %s, but current version is %s', $request->getExpectedVersion(), self::VERSION)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$method = $this->getMethod($request->getMethod()); |
|
67
|
|
|
|
|
68
|
|
|
$responseData = $method->call($request); |
|
69
|
|
|
|
|
70
|
|
|
} catch (BaseException $e) { |
|
71
|
|
|
return new ErrorResponse($this->secret, $e); |
|
72
|
|
|
|
|
73
|
|
|
} catch (Throwable $e) { |
|
74
|
|
|
return new ErrorResponse($this->secret, $e); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return new SuccessResponse( |
|
78
|
|
|
$this->secret, |
|
79
|
|
|
$responseData |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $method |
|
85
|
|
|
* @return Method |
|
86
|
|
|
* @throws ClientException |
|
87
|
|
|
*/ |
|
88
|
|
|
private function getMethod($method) |
|
89
|
|
|
{ |
|
90
|
|
|
if (!isset($this->methods[$method])) { |
|
91
|
|
|
throw new ClientException(sprintf('Method %s was not registered!', $method)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $this->methods[$method]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|