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\JobLogCommand; |
10
|
|
|
use ApiClients\Client\Travis\Resource\LogLineInterface; |
11
|
|
|
use ApiClients\Foundation\Hydrator\Hydrator; |
12
|
|
|
use React\Promise\PromiseInterface; |
13
|
|
|
use Rx\Observable; |
14
|
|
|
use Rx\ObserverInterface; |
15
|
|
|
use function React\Promise\resolve; |
16
|
|
|
|
17
|
|
|
final class JobLogHandler |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var SharedAppClientService |
21
|
|
|
*/ |
22
|
|
|
private $pusher; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Hydrator |
26
|
|
|
*/ |
27
|
|
|
private $hydrator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* JobLogHandler constructor. |
31
|
|
|
* @param SharedAppClientService $pusher |
32
|
|
|
* @param Hydrator $hydrator |
33
|
|
|
*/ |
34
|
|
|
public function __construct(SharedAppClientService $pusher, Hydrator $hydrator) |
35
|
|
|
{ |
36
|
|
|
$this->pusher = $pusher; |
37
|
|
|
$this->hydrator = $hydrator; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Fetch the given repository and hydrate it. |
42
|
|
|
* |
43
|
|
|
* @param JobLogCommand $command |
44
|
|
|
* @return PromiseInterface |
45
|
|
|
*/ |
46
|
|
|
public function handle(JobLogCommand $command): PromiseInterface |
47
|
|
|
{ |
48
|
|
|
return $this->pusher->share( |
49
|
|
|
ApiSettings::PUSHER_KEY |
50
|
|
|
)->then(function (PusherAsyncClient $pusher) use ($command) { |
51
|
|
|
return resolve(Observable::create(function ( |
52
|
|
|
ObserverInterface $observer |
53
|
|
|
) use ( |
54
|
|
|
$pusher, |
55
|
|
|
$command |
56
|
|
|
) { |
57
|
|
|
$subscription = $pusher->channel('job-' . (string)$command->getId())->filter(function (Event $event) { |
|
|
|
|
58
|
|
|
return $event->getEvent() === 'job:log'; |
59
|
|
|
})->map(function (Event $event) { |
60
|
|
|
return $this->hydrator->hydrate(LogLineInterface::HYDRATE_CLASS, $event->getData()); |
61
|
|
|
})->subscribe( |
62
|
|
|
function (LogLineInterface $line) use ($observer, &$subscription) { |
63
|
|
|
$observer->onNext($line); |
64
|
|
|
|
65
|
|
|
if ($line->final()) { |
66
|
|
|
$subscription->dispose(); |
67
|
|
|
} |
68
|
|
|
}, |
69
|
|
|
function ($error) use ($observer) { |
70
|
|
|
$observer->onError($error); |
71
|
|
|
}, |
72
|
|
|
function () use ($observer) { |
73
|
|
|
$observer->onComplete(); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
); |
76
|
|
|
})); |
77
|
|
|
}); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.