|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Travis\CommandBus\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use ApiClients\Client\Travis\CommandBus\Command\RepositoryCommand; |
|
6
|
|
|
use ApiClients\Client\Travis\Exception\RepositoryDoesNotExist; |
|
7
|
|
|
use ApiClients\Client\Travis\Resource\RepositoryInterface; |
|
8
|
|
|
use ApiClients\Foundation\Resource\EmptyResourceInterface; |
|
9
|
|
|
use ApiClients\Tools\Services\Client\FetchAndHydrateService; |
|
10
|
|
|
use Clue\React\Buzz\Message\ResponseException; |
|
11
|
|
|
use React\Promise\PromiseInterface; |
|
12
|
|
|
use Throwable; |
|
13
|
|
|
use function React\Promise\reject; |
|
14
|
|
|
use function React\Promise\resolve; |
|
15
|
|
|
use function WyriHaximus\React\futureFunctionPromise; |
|
16
|
|
|
|
|
17
|
|
|
final class RepositoryHandler |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var FetchAndHydrateService |
|
21
|
|
|
*/ |
|
22
|
|
|
private $service; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param FetchAndHydrateService $service |
|
26
|
|
|
*/ |
|
27
|
3 |
|
public function __construct(FetchAndHydrateService $service) |
|
28
|
|
|
{ |
|
29
|
3 |
|
$this->service = $service; |
|
30
|
3 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Fetch the given repository and hydrate it |
|
34
|
|
|
* |
|
35
|
|
|
* @param RepositoryCommand $command |
|
36
|
|
|
* @return PromiseInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
public function handle(RepositoryCommand $command): PromiseInterface |
|
39
|
|
|
{ |
|
40
|
3 |
|
return $this->service->handle( |
|
41
|
3 |
|
'repos/' . $command->getRepository(), |
|
42
|
3 |
|
'repo', |
|
43
|
3 |
|
RepositoryInterface::HYDRATE_CLASS |
|
44
|
|
|
)->then(function (RepositoryInterface $repository) use ($command) { |
|
45
|
2 |
|
if ($repository instanceof EmptyResourceInterface) { |
|
46
|
1 |
|
return reject(RepositoryDoesNotExist::create($command->getRepository())); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
return resolve($repository); |
|
50
|
|
|
}, function (Throwable $throwable) use ($command) { |
|
51
|
1 |
|
if (!($throwable instanceof ResponseException)) { |
|
52
|
|
|
return reject($throwable); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
if ($throwable->getCode() !== 404) { |
|
56
|
|
|
return reject($throwable); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
return reject(RepositoryDoesNotExist::create($command->getRepository())); |
|
60
|
3 |
|
}); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|