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
|
|
|
|
16
|
|
|
final class RepositoryHandler |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var FetchAndHydrateService |
20
|
|
|
*/ |
21
|
|
|
private $service; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param FetchAndHydrateService $service |
25
|
|
|
*/ |
26
|
3 |
|
public function __construct(FetchAndHydrateService $service) |
27
|
|
|
{ |
28
|
3 |
|
$this->service = $service; |
29
|
3 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Fetch the given repository and hydrate it. |
33
|
|
|
* |
34
|
|
|
* @param RepositoryCommand $command |
35
|
|
|
* @return PromiseInterface |
36
|
|
|
*/ |
37
|
3 |
|
public function handle(RepositoryCommand $command): PromiseInterface |
38
|
|
|
{ |
39
|
3 |
|
return $this->service->fetch( |
40
|
3 |
|
'repos/' . $command->getRepository(), |
41
|
3 |
|
'repo', |
42
|
3 |
|
RepositoryInterface::HYDRATE_CLASS |
43
|
2 |
|
)->then(function (RepositoryInterface $repository) use ($command) { |
44
|
2 |
|
if ($repository instanceof EmptyResourceInterface) { |
45
|
1 |
|
return reject(RepositoryDoesNotExist::create($command->getRepository())); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
return resolve($repository); |
49
|
|
|
}, function (Throwable $throwable) use ($command) { |
50
|
1 |
|
if (!($throwable instanceof ResponseException)) { |
51
|
|
|
return reject($throwable); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
if ($throwable->getCode() !== 404) { |
55
|
|
|
return reject($throwable); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
return reject(RepositoryDoesNotExist::create($command->getRepository())); |
59
|
3 |
|
}); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|