|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the mingyoung/dingtalk. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 张铭阳 <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace EasyDingTalk\Kernel; |
|
13
|
|
|
|
|
14
|
|
|
use EasyDingTalk\Kernel\Exceptions\InvalidArgumentException; |
|
15
|
|
|
use EasyDingTalk\Kernel\Exceptions\RuntimeException; |
|
16
|
|
|
use function EasyDingTalk\tap; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
18
|
|
|
|
|
19
|
|
|
class Server |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var \EasyDingTalk\Application |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $app; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $handlers = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param \EasyDingTalk\Application $app |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct($app) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->app = $app; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Handle the request. |
|
41
|
|
|
* |
|
42
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
43
|
|
|
*/ |
|
44
|
|
|
public function serve() |
|
45
|
|
|
{ |
|
46
|
|
|
foreach ($this->handlers as $handler) { |
|
47
|
|
|
$handler->__invoke($this->getPayload()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$this->app['logger']->debug('Request received: ', [ |
|
51
|
|
|
'method' => $this->app['request']->getMethod(), |
|
52
|
|
|
'uri' => $this->app['request']->getUri(), |
|
53
|
|
|
'content' => $this->app['request']->getContent(), |
|
54
|
|
|
]); |
|
55
|
|
|
|
|
56
|
|
|
return tap(new Response( |
|
57
|
|
|
$this->app['encryptor']->encrypt('success'), 200, ['Content-Type' => 'application/json'] |
|
58
|
|
|
), function ($response) { |
|
59
|
|
|
$this->app['logger']->debug('Response created:', ['content' => $response->getContent()]); |
|
60
|
|
|
}); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Push handler. |
|
65
|
|
|
* |
|
66
|
|
|
* @param \Closure|string|object $handler |
|
67
|
|
|
* |
|
68
|
|
|
* @return void |
|
69
|
|
|
* |
|
70
|
|
|
* @throws \EasyDingTalk\Kernel\Exceptions\InvalidArgumentException |
|
71
|
|
|
*/ |
|
72
|
|
|
public function push($handler) |
|
73
|
|
|
{ |
|
74
|
|
|
if (is_string($handler)) { |
|
75
|
|
|
$handler = function ($payload) use ($handler) { |
|
76
|
|
|
return (new $handler($this->app))->__invoke($payload); |
|
77
|
|
|
}; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (!is_callable($handler)) { |
|
81
|
|
|
throw new InvalidArgumentException('Invalid handler'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
array_push($this->handlers, $handler); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get request payload. |
|
89
|
|
|
* |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getPayload() |
|
93
|
|
|
{ |
|
94
|
|
|
$payload = json_decode($this->app['request']->getContent(), true); |
|
95
|
|
|
|
|
96
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) { |
|
97
|
|
|
throw new RuntimeException('No payload received'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$result = $this->app['encryptor']->decrypt( |
|
101
|
|
|
$payload['encrypt'], $this->app['request'], $this->app['request']->get('signature'), $this->app['request']->get('nonce'), $this->app['request']->get('timestamp') |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
|
|
return json_decode($result, true); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|