Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
8 / 8
GetForecastQueryHandler
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
8 / 8
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 __invoke
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
6 / 6
1<?php
2
3namespace App\Infrastructure;
4
5use App\Application\ForecastResponse;
6use App\Application\GetForecastQuery;
7use App\Application\QueryHandler;
8use App\Domain\Weather\Forecast;
9use Symfony\Component\HttpFoundation\Request;
10use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer;
11use Symfony\Component\Serializer\SerializerInterface;
12use Symfony\Contracts\HttpClient\HttpClientInterface;
13
14class GetForecastQueryHandler implements QueryHandler
15{
16    public function __construct(
17        private HttpClientInterface $weatherClient,
18        private SerializerInterface $serializer,
19    ) {
20    }
21
22    public function __invoke(GetForecastQuery $query): ForecastResponse
23    {
24        $request = $this->weatherClient->request(Request::METHOD_GET, '/v1/forecast.json', [
25            'query' => [
26                'q' => (string) $query,
27                'days' => $query->getDays(),
28            ],
29        ]);
30
31        /** @var Forecast[] $forecasts */
32        $forecasts = $this->serializer->deserialize($request->getContent(), Forecast::class.'[]', 'json', [
33            UnwrappingDenormalizer::UNWRAP_PATH => '[forecast][forecastday]',
34        ]);
35
36        return new ForecastResponse($forecasts);
37    }
38}