|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Phpactor\LanguageServer\Core\Server\Client; |
| 4 | + |
| 5 | +use Amp\Promise; |
| 6 | +use InvalidArgumentException; |
| 7 | +use Phpactor\LanguageServer\Core\Rpc\ResponseMessage; |
| 8 | +use Phpactor\LanguageServer\Core\Server\RpcClient; |
| 9 | +use Phpactor\LanguageServer\WorkDoneProgress\WorkDoneToken; |
| 10 | + |
| 11 | +final class WorkDoneProgressClient |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var RpcClient |
| 15 | + */ |
| 16 | + private $client; |
| 17 | + |
| 18 | + public function __construct(RpcClient $client) |
| 19 | + { |
| 20 | + $this->client = $client; |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * @return Promise<ResponseMessage> |
| 25 | + */ |
| 26 | + public function create(WorkDoneToken $token): Promise |
| 27 | + { |
| 28 | + return \Amp\call(function () use ($token) { |
| 29 | + return yield $this->client->request('window/workDoneProgress/create', [ |
| 30 | + 'token' => (string) $token, |
| 31 | + ]); |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + public function begin( |
| 36 | + WorkDoneToken $token, |
| 37 | + string $title, |
| 38 | + ?string $message = null, |
| 39 | + ?int $percentage = null, |
| 40 | + ?bool $cancellable = null |
| 41 | + ): void { |
| 42 | + self::assertIsValidPercentage($percentage); |
| 43 | + |
| 44 | + $this->notify($token, [ |
| 45 | + 'kind' => 'begin', |
| 46 | + 'title' => $title, |
| 47 | + 'message' => $message, |
| 48 | + 'percentage' => $percentage, |
| 49 | + 'cancellable' => $cancellable, |
| 50 | + ]); |
| 51 | + } |
| 52 | + |
| 53 | + public function report( |
| 54 | + WorkDoneToken $token, |
| 55 | + ?string $message = null, |
| 56 | + ?int $percentage = null, |
| 57 | + ?bool $cancellable = null |
| 58 | + ): void { |
| 59 | + self::assertIsValidPercentage($percentage); |
| 60 | + |
| 61 | + $this->notify($token, [ |
| 62 | + 'kind' => 'report', |
| 63 | + 'message' => $message, |
| 64 | + 'percentage' => $percentage, |
| 65 | + 'cancellable' => $cancellable, |
| 66 | + ]); |
| 67 | + } |
| 68 | + |
| 69 | + public function end(WorkDoneToken $token, ?string $message = null): void |
| 70 | + { |
| 71 | + $this->notify($token, [ |
| 72 | + 'kind' => 'end', |
| 73 | + 'message' => $message, |
| 74 | + ]); |
| 75 | + } |
| 76 | + |
| 77 | + private static function assertIsValidPercentage(?int $percentage): void |
| 78 | + { |
| 79 | + if (!(null === $percentage || 0 <= $percentage && $percentage <= 100)) { |
| 80 | + throw new InvalidArgumentException( |
| 81 | + 'The percentage must be an integer comprised between 0 and 100.', |
| 82 | + ); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private function notify(WorkDoneToken $token, array $value): void |
| 87 | + { |
| 88 | + assert(in_array($value['kind'], ['begin', 'report', 'end'])); |
| 89 | + |
| 90 | + $this->client->notification('$/progress', [ |
| 91 | + 'token' => (string) $token, |
| 92 | + 'value' => $value, |
| 93 | + ]); |
| 94 | + } |
| 95 | +} |
0 commit comments