HourForecastDenormalizer::denormalize()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 6
nop 2
dl 0
loc 32
rs 9.0968
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Marek\OpenWeatherMap\Denormalizer;
6
7
use Marek\OpenWeatherMap\API\Value\Response\APIResponse;
8
use Marek\OpenWeatherMap\API\Value\Response\Clouds;
9
use Marek\OpenWeatherMap\API\Value\Response\HourForecast\AggregatedHourForecast;
10
use Marek\OpenWeatherMap\API\Value\Response\HourForecast\HourForecast;
11
use Marek\OpenWeatherMap\API\Value\Response\Main;
12
use Marek\OpenWeatherMap\API\Value\Response\Rain;
13
use Marek\OpenWeatherMap\API\Value\Response\Snow;
14
use Marek\OpenWeatherMap\API\Value\Response\Sys;
15
use Marek\OpenWeatherMap\API\Value\Response\WeatherValue;
16
use Marek\OpenWeatherMap\API\Value\Response\Wind;
17
18
class HourForecastDenormalizer extends AbstractDenormalizer
19
{
20
    public function denormalize(array $data, APIResponse $response): APIResponse
21
    {
22
        if (!$response instanceof AggregatedHourForecast) {
23
            return $response;
24
        }
25
26
        $hourForecasts = [];
27
        foreach ($data['list'] as $item) {
28
            $innerWeather = [];
29
            foreach ($item['weather'] as $w) {
30
                $innerWeather[] = $this->denormalizer->denormalize($w, WeatherValue::class);
31
            }
32
33
            $hourForecast = new HourForecast();
34
            $hourForecast->rain = $this->getValue('rain', $item, Rain::class);
0 ignored issues
show
Bug introduced by
The property rain does not seem to exist in Marek\OpenWeatherMap\API...urForecast\HourForecast.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
35
            $hourForecast->snow = $this->getValue('snow', $item, Snow::class);
0 ignored issues
show
Bug introduced by
The property snow does not seem to exist in Marek\OpenWeatherMap\API...urForecast\HourForecast.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
36
            $hourForecast->wind = $this->getValue('wind', $item, Wind::class);
37
            $hourForecast->clouds = $this->getValue('clouds', $item, Clouds::class);
0 ignored issues
show
Bug introduced by
The property clouds does not seem to exist in Marek\OpenWeatherMap\API...urForecast\HourForecast.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
38
            $hourForecast->main = $this->getValue('main', $item, Main::class);
0 ignored issues
show
Bug introduced by
The property main does not seem to exist in Marek\OpenWeatherMap\API...urForecast\HourForecast.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
39
            $hourForecast->weather = $innerWeather;
0 ignored issues
show
Bug introduced by
The property weather does not seem to exist in Marek\OpenWeatherMap\API...urForecast\HourForecast.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
40
            $hourForecast->sys = $this->getValue('sys', $item, Sys::class);
0 ignored issues
show
Bug introduced by
The property sys does not seem to exist in Marek\OpenWeatherMap\API...urForecast\HourForecast.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
41
            $hourForecast->dt = $this->getDateTimeFromTimestamp('dt', $item);
0 ignored issues
show
Bug introduced by
The property dt does not seem to exist in Marek\OpenWeatherMap\API...urForecast\HourForecast.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
42
            $hourForecast->dt_txt = empty($data['dt_txt']) ? null : \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $data['dt_txt']);
0 ignored issues
show
Bug introduced by
The property dt_txt does not seem to exist in Marek\OpenWeatherMap\API...urForecast\HourForecast.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
43
44
            $hourForecasts[] = $hourForecast;
45
        }
46
47
        $response->count = $this->getData('cnt', $data);
48
        $response->list = $hourForecasts;
49
50
        return $response;
51
    }
52
}
53