|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Crummy\Phlack\Bridge\Symfony\HttpKernel; |
|
4
|
|
|
|
|
5
|
|
|
use Crummy\Phlack\Bot\Mainframe\Adapter\AbstractAdapter; |
|
6
|
|
|
use Crummy\Phlack\Bot\Mainframe\Mainframe; |
|
7
|
|
|
use Crummy\Phlack\Bridge\Symfony\HttpFoundation\RequestConverter; |
|
8
|
|
|
use Crummy\Phlack\WebHook\Reply\Reply; |
|
9
|
|
|
use Crummy\Phlack\WebHook\SlashCommand; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
12
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
|
13
|
|
|
|
|
14
|
|
|
class MainframeKernel extends AbstractAdapter implements HttpKernelInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var \Crummy\Phlack\Bridge\Symfony\HttpFoundation\RequestConverter */ |
|
17
|
|
|
protected $converter; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param Mainframe $mainframe |
|
21
|
|
|
* @param RequestConverter $converter |
|
22
|
|
|
*/ |
|
23
|
2 |
|
public function __construct(Mainframe $mainframe = null, RequestConverter $converter = null) |
|
24
|
|
|
{ |
|
25
|
2 |
|
$mainframe = $mainframe ?: new Mainframe(); |
|
26
|
2 |
|
$converter = $converter ?: new RequestConverter(); |
|
27
|
2 |
|
parent::__construct($mainframe, $converter); |
|
28
|
2 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Mediates Request handling between HttpKernelInterface and the Mainframe. |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true) |
|
35
|
|
|
{ |
|
36
|
1 |
|
$command = $this->converter->convert($request); |
|
37
|
1 |
|
$packet = $this->mainframe->execute($command); |
|
38
|
1 |
|
$content = $packet['output']; |
|
39
|
|
|
|
|
40
|
1 |
|
if ($command instanceof SlashCommand && $content instanceof Reply) { |
|
41
|
|
|
$content = $content->get('text'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
return new Response($content); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|