1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Travis\Service; |
4
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Hydrator\Hydrator; |
6
|
|
|
use ApiClients\Foundation\Service\ServiceInterface; |
7
|
|
|
use ApiClients\Foundation\Transport\Service\RequestService; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use React\Promise\CancellablePromiseInterface; |
10
|
|
|
use RingCentral\Psr7\Request; |
11
|
|
|
use Rx\Observable; |
12
|
|
|
use function React\Promise\resolve; |
13
|
|
|
use function WyriHaximus\React\futureFunctionPromise; |
14
|
|
|
|
15
|
|
|
class FetchAndHydrateService implements ServiceInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var RequestService |
19
|
|
|
*/ |
20
|
|
|
private $requestService; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Hydrator |
24
|
|
|
*/ |
25
|
|
|
private $hydrator; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param RequestService $requestService |
29
|
|
|
* @param Hydrator $hydrator |
30
|
|
|
*/ |
31
|
4 |
|
public function __construct(RequestService $requestService, Hydrator $hydrator) |
32
|
|
|
{ |
33
|
4 |
|
$this->requestService = $requestService; |
34
|
4 |
|
$this->hydrator = $hydrator; |
35
|
4 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string|null $path |
39
|
|
|
* @param string|null $index |
40
|
|
|
* @param string|null $hydrateClass |
41
|
|
|
* @return CancellablePromiseInterface |
42
|
|
|
*/ |
43
|
4 |
|
public function handle( |
44
|
|
|
string $path = null, |
45
|
|
|
string $index = null, |
46
|
|
|
string $hydrateClass = null |
47
|
|
|
): CancellablePromiseInterface { |
48
|
4 |
|
return $this->requestService->handle( |
49
|
4 |
|
new Request('GET', $path) |
50
|
4 |
|
)->then(function (ResponseInterface $response) use ($hydrateClass, $index) { |
51
|
4 |
|
return $this->hydrator->hydrate( |
52
|
|
|
$hydrateClass, |
53
|
4 |
|
$response->getBody()->getJson()[$index] |
|
|
|
|
54
|
|
|
); |
55
|
4 |
|
}); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
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: