-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcustom-transport.php
49 lines (39 loc) · 1.47 KB
/
custom-transport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
use MaciejSz\Nbp\Service\GoldRatesService;
use MaciejSz\Nbp\Shared\Infrastructure\Client\NbpWebClient;
use MaciejSz\Nbp\Shared\Infrastructure\Repository\NbpWebRepository;
use MaciejSz\Nbp\Shared\Infrastructure\Transport\Transport;
use MaciejSz\Nbp\Shared\Infrastructure\Transport\TransportFactory;
$customTransportFactory = new class implements TransportFactory {
public function make(string $baseUri): Transport
{
return new class implements Transport {
public function get(string $path): array
{
echo "Requesting resource: {$path}" . PHP_EOL;
$ch = curl_init();
$url = NbpWebClient::BASE_URL . $path;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
try {
$output = curl_exec($ch);
} finally {
curl_close($ch);
}
echo 'Request successful' . PHP_EOL;
return json_decode($output, true);
}
};
}
};
$client = NbpWebClient::new(transportFactory: $customTransportFactory);
$nbpRepository = NbpWebRepository::new($client);
$goldRates = GoldRatesService::new($nbpRepository);
$rate = $goldRates->fromDay('2022-01-03');
printf(
'Gold rate from %s is %F',
$rate->getDate()->format('Y-m-d'),
$rate->getValue()
);