1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HelePartnerSyncApi; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use HelePartnerSyncApi\Method\CancelReservation; |
7
|
|
|
use HelePartnerSyncApi\Method\CheckHealth; |
8
|
|
|
use HelePartnerSyncApi\Method\CreateReservation; |
9
|
|
|
use HelePartnerSyncApi\Method\GetSlots; |
10
|
|
|
use HelePartnerSyncApi\Request\DefaultRequestFactory; |
11
|
|
|
use HelePartnerSyncApi\Response\DefaultResponseFactory; |
12
|
|
|
|
13
|
|
|
class Application |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Client |
18
|
|
|
*/ |
19
|
|
|
private $client; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $secret |
23
|
|
|
*/ |
24
|
|
|
public function __construct($secret) |
25
|
|
|
{ |
26
|
|
|
$this->client = new Client( |
27
|
|
|
new DefaultRequestFactory($secret), |
28
|
|
|
new DefaultResponseFactory($secret) |
29
|
|
|
); |
30
|
|
|
$this->client->registerMethod(new CheckHealth()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Closure $callback function (DateTime $date, array $parameters) |
35
|
|
|
*/ |
36
|
|
|
public function onGetSlots(Closure $callback) |
37
|
|
|
{ |
38
|
|
|
$this->client->registerMethod(new GetSlots($callback)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Closure $callback function (DateTime $startDateTime, DateTime $endDateTime, int $quantity, array $parameters) |
43
|
|
|
*/ |
44
|
|
|
public function onCreateReservation(Closure $callback) |
45
|
|
|
{ |
46
|
|
|
$this->client->registerMethod(new CreateReservation($callback)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Closure $callback function (DateTime $startDateTime, DateTime $endDateTime, int $quantity, array $parameters) |
51
|
|
|
*/ |
52
|
|
|
public function onCancelReservation(Closure $callback) |
53
|
|
|
{ |
54
|
|
|
$this->client->registerMethod(new CancelReservation($callback)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function run() |
58
|
|
|
{ |
59
|
|
|
$this->client->run( |
60
|
|
|
$this->getHttpBody(), |
61
|
|
|
$this->getHttpHeaders() |
62
|
|
|
)->render(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
private function getHttpBody() |
69
|
|
|
{ |
70
|
|
|
return file_get_contents('php://input'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string[] |
75
|
|
|
*/ |
76
|
|
|
private function getHttpHeaders() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
if (function_exists('apache_request_headers')) { |
79
|
|
|
return apache_request_headers(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$headers = array(); |
83
|
|
|
foreach ($_SERVER as $k => $v) { |
84
|
|
|
if (strncmp($k, 'HTTP_', 5) == 0) { |
85
|
|
|
$k = substr($k, 5); |
86
|
|
|
} elseif (strncmp($k, 'CONTENT_', 8)) { |
87
|
|
|
continue; |
88
|
|
|
} |
89
|
|
|
$headers[strtr($k, '_', '-')] = $v; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $headers; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: