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