Completed
Push — master ( 6551be...435af8 )
by Mario
09:41
created

UltravioletIndexDenormalizer::denormalizeSingle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 14
rs 9.7998
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\GeographicCoordinates;
9
use Marek\OpenWeatherMap\API\Value\Response\UltravioletIndex\AggregatedUltravioletIndex;
10
use Marek\OpenWeatherMap\API\Value\Response\UltravioletIndex\UltravioletIndex;
11
12
class UltravioletIndexDenormalizer extends AbstractDenormalizer
13
{
14
    public function denormalize(array $data, APIResponse $response): APIResponse
15
    {
16
        if ($response instanceof AggregatedUltravioletIndex) {
17
            return $this->denormalizeMultiple($data, $response);
18
        }
19
20
        if (!$response instanceof  UltravioletIndex) {
21
            return $response;
22
        }
23
24
        return $this->denormalizeSingle($data, $response);
25
    }
26
27
    protected function denormalizeSingle(array $data, UltravioletIndex $index): UltravioletIndex
28
    {
29
        $index->value = $this->getData('value', $data);
30
        $index->date = $this->getDateTimeFromTimestamp('date', $data);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getDateTimeFromTimestamp('date', $data) of type object<DateTimeImmutable> is incompatible with the declared type object<DateTime> of property $date.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
31
        $index->isoDate = $this->getValue('date_iso', $data, \DateTimeInterface::class);
32
33
        $location = new GeographicCoordinates();
34
        $location->latitude = $this->getData('lat', $data);
35
        $location->longitude = $this->getData('lon', $data);
36
37
        $index->location = $location;
0 ignored issues
show
Documentation Bug introduced by
It seems like $location of type object<Marek\OpenWeather...\GeographicCoordinates> is incompatible with the declared type object<Marek\OpenWeather...alue\Response\Location> of property $location.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
39
        return $index;
40
    }
41
42
    protected function denormalizeMultiple($data, AggregatedUltravioletIndex $response): AggregatedUltravioletIndex
43
    {
44
        $indexes = [];
45
        foreach ($data as $datum) {
46
            $indexes[] = $this->denormalizeSingle($datum, new UltravioletIndex());
47
        }
48
49
        $response->count = count($indexes);
50
        $response->indexes = $indexes;
51
52
        return $response;
53
    }
54
}
55