Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
GetRateHandler
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 handle
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace App\Currency\Application\Query;
6
7use App\Currency\Application\ExchangeRateProvider;
8use App\Shared\Domain\ValueObject\Currency;
9use App\Shared\Domain\ValueObject\IsoDate;
10use DateTimeImmutable;
11
12class GetRateHandler
13{
14    public function __construct(
15        private readonly ExchangeRateProvider $rates,
16    ) {
17    }
18
19    /**
20     * @return array{from: string, to: string, rate: float, date: string}
21     */
22    public function handle(GetRateQuery $query): array
23    {
24        $from = new Currency($query->from);
25        $to = new Currency($query->to);
26        $on = $query->on !== null ? IsoDate::parse($query->on) : new DateTimeImmutable('today');
27
28        return [
29            'from' => $from->code(),
30            'to' => $to->code(),
31            'rate' => $this->rates->rate($from, $to, $on),
32            'date' => $on->format('Y-m-d'),
33        ];
34    }
35}