1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Travis\CommandBus\Handler; |
4
|
|
|
|
5
|
|
|
use ApiClients\Client\Pusher\Event; |
6
|
|
|
use ApiClients\Client\Pusher\Service\SharedAppClientService; |
7
|
|
|
use ApiClients\Client\Travis\ApiSettings; |
8
|
|
|
use ApiClients\Client\Travis\AsyncClient; |
9
|
|
|
use ApiClients\Client\Pusher\AsyncClient as PusherAsyncClient; |
10
|
|
|
use ApiClients\Client\Travis\CommandBus\Command\JobCommand; |
11
|
|
|
use ApiClients\Client\Travis\CommandBus\Command\JobLogCommand; |
12
|
|
|
use ApiClients\Client\Travis\CommandBus\Command\RepositoryEventsCommand; |
13
|
|
|
use ApiClients\Client\Travis\Resource\LogLineInterface; |
14
|
|
|
use ApiClients\Client\Travis\Resource\RepositoryInterface; |
15
|
|
|
use ApiClients\Foundation\Hydrator\Hydrator; |
16
|
|
|
use React\Promise\PromiseInterface; |
17
|
|
|
use function ApiClients\Tools\Rx\unwrapObservableFromPromise; |
18
|
|
|
use function React\Promise\resolve; |
19
|
|
|
use Rx\React\Promise; |
20
|
|
|
use function WyriHaximus\React\futureFunctionPromise; |
21
|
|
|
|
22
|
|
|
final class RepositoryEventsHandler |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var SharedAppClientService |
26
|
|
|
*/ |
27
|
|
|
private $pusher; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Hydrator |
31
|
|
|
*/ |
32
|
|
|
private $hydrator; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* JobLogHandler constructor. |
36
|
|
|
* @param SharedAppClientService $pusher |
37
|
|
|
* @param Hydrator $hydrator |
38
|
|
|
*/ |
39
|
|
|
public function __construct(SharedAppClientService $pusher, Hydrator $hydrator) |
40
|
|
|
{ |
41
|
|
|
$this->pusher = $pusher; |
42
|
|
|
$this->hydrator = $hydrator; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Fetch the given repository and hydrate it |
47
|
|
|
* |
48
|
|
|
* @param RepositoryEventsCommand $command |
49
|
|
|
* @return PromiseInterface |
50
|
|
|
*/ |
51
|
|
|
public function handle(RepositoryEventsCommand $command): PromiseInterface |
52
|
|
|
{ |
53
|
|
|
return $this->pusher->share( |
54
|
|
|
ApiSettings::PUSHER_KEY |
55
|
|
|
)->then(function (PusherAsyncClient $pusher) use ($command) { |
56
|
|
|
return resolve( |
57
|
|
|
$pusher->channel('repo-' . (string)$command->getId())->filter(function (Event $event) { |
58
|
|
|
return in_array($event->getEvent(), [ |
59
|
|
|
'build:created', |
60
|
|
|
'build:started', |
61
|
|
|
'build:finished', |
62
|
|
|
]); |
63
|
|
|
})->filter(function (Event $event) { |
64
|
|
|
return isset($event->getData()['repository']); |
65
|
|
|
})->map(function (Event $event) { |
66
|
|
|
return $this->hydrator->hydrate( |
67
|
|
|
RepositoryInterface::HYDRATE_CLASS, |
68
|
|
|
$event->getData()['repository'] |
69
|
|
|
); |
70
|
|
|
}) |
71
|
|
|
); |
72
|
|
|
}); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|