|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Github\CommandBus\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use ApiClients\Client\Github\CommandBus\Command\RenderMarkdownCommand; |
|
6
|
|
|
use ApiClients\Foundation\Transport\Service\RequestService; |
|
7
|
|
|
use Clue\React\Buzz\Message\ReadableBodyStream; |
|
8
|
|
|
use React\EventLoop\LoopInterface; |
|
9
|
|
|
use React\Promise\PromiseInterface; |
|
10
|
|
|
use React\Stream\ThroughStream; |
|
11
|
|
|
use RingCentral\Psr7\Request; |
|
12
|
|
|
use WyriHaximus\React\Stream\Json\JsonStream; |
|
13
|
|
|
|
|
14
|
|
|
final class RenderMarkdownHandler |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var RequestService |
|
18
|
|
|
*/ |
|
19
|
|
|
private $requestService; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var LoopInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $loop; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param RequestService $requestService |
|
28
|
|
|
* @param LoopInterface $loop |
|
29
|
|
|
*/ |
|
30
|
2 |
|
public function __construct(RequestService $requestService, LoopInterface $loop) |
|
31
|
|
|
{ |
|
32
|
2 |
|
$this->requestService = $requestService; |
|
33
|
2 |
|
$this->loop = $loop; |
|
34
|
2 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param RenderMarkdownCommand $command |
|
38
|
|
|
* @return PromiseInterface |
|
39
|
|
|
*/ |
|
40
|
2 |
|
public function handle(RenderMarkdownCommand $command): PromiseInterface |
|
41
|
|
|
{ |
|
42
|
2 |
|
$stream = new JsonStream(); |
|
43
|
|
|
|
|
44
|
2 |
|
$this->loop->futureTick(function () use ($stream, $command) { |
|
45
|
2 |
|
$markdownStream = new ThroughStream(); |
|
46
|
2 |
|
$stream->write('text', $markdownStream); |
|
47
|
2 |
|
if ($command->getMode() !== '') { |
|
48
|
1 |
|
$stream->write('mode', $command->getMode()); |
|
49
|
|
|
} |
|
50
|
2 |
|
if ($command->getContext() !== '') { |
|
51
|
1 |
|
$stream->write('context', $command->getContext()); |
|
52
|
|
|
} |
|
53
|
2 |
|
$stream->end(); |
|
54
|
|
|
|
|
55
|
2 |
|
$this->loop->futureTick(function () use ($markdownStream, $command) { |
|
56
|
2 |
|
$command->getStream()->pipe($markdownStream); |
|
57
|
2 |
|
}); |
|
58
|
2 |
|
}); |
|
59
|
|
|
|
|
60
|
2 |
|
return $this->requestService->request( |
|
61
|
2 |
|
new Request( |
|
62
|
2 |
|
'POST', |
|
63
|
2 |
|
'/markdown', |
|
64
|
2 |
|
[], |
|
65
|
2 |
|
new ReadableBodyStream($stream) |
|
66
|
|
|
) |
|
67
|
2 |
|
)->then(function ($markdown) { |
|
68
|
2 |
|
return $markdown->getBody(); |
|
69
|
2 |
|
}); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|