1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\AppVeyor\CommandBus\Handler; |
4
|
|
|
|
5
|
|
|
use ApiClients\Client\AppVeyor\CommandBus\Command\AddProjectCommand; |
6
|
|
|
use ApiClients\Client\AppVeyor\Resource\ProjectInterface; |
7
|
|
|
use ApiClients\Foundation\Hydrator\Hydrator; |
8
|
|
|
use ApiClients\Foundation\Transport\Service\RequestService; |
9
|
|
|
use ApiClients\Middleware\Json\JsonStream; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
use React\Promise\PromiseInterface; |
12
|
|
|
use RingCentral\Psr7\Request; |
13
|
|
|
|
14
|
|
|
final class AddProjectHandler |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var RequestService |
18
|
|
|
*/ |
19
|
|
|
private $requestService; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Hydrator |
23
|
|
|
*/ |
24
|
|
|
private $hydrator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param RequestService $requestService |
28
|
|
|
* @param Hydrator $hydrator |
29
|
|
|
*/ |
30
|
1 |
|
public function __construct(RequestService $requestService, Hydrator $hydrator) |
31
|
|
|
{ |
32
|
1 |
|
$this->requestService = $requestService; |
33
|
1 |
|
$this->hydrator = $hydrator; |
34
|
1 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param AddProjectCommand $command |
38
|
|
|
* @return PromiseInterface |
39
|
|
|
*/ |
40
|
1 |
|
public function handle(AddProjectCommand $command): PromiseInterface |
41
|
|
|
{ |
42
|
1 |
|
return $this->requestService->request( |
43
|
1 |
|
new Request( |
44
|
1 |
|
'POST', |
45
|
1 |
|
'projects', |
46
|
1 |
|
[], |
47
|
1 |
|
new JsonStream([ |
48
|
1 |
|
'repositoryProvider' => $command->getProvider(), |
49
|
1 |
|
'repositoryName' => $command->getRepository(), |
50
|
|
|
]) |
51
|
|
|
) |
52
|
1 |
|
)->then(function (ResponseInterface $response) { |
53
|
1 |
|
return $this->hydrator->hydrate( |
54
|
1 |
|
ProjectInterface::HYDRATE_CLASS, |
55
|
1 |
|
$response->getBody()->getParsedContents() |
|
|
|
|
56
|
|
|
); |
57
|
1 |
|
}); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: