AirPollutionDenormalizer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 4
dl 0
loc 31
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A denormalize() 0 28 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Marek\OpenWeatherMap\Denormalizer;
6
7
use Marek\OpenWeatherMap\API\Value\Response\AirPollution\AirPollution;
8
use Marek\OpenWeatherMap\API\Value\Response\AirPollution\CarbonMonoxide;
9
use Marek\OpenWeatherMap\API\Value\Response\APIResponse;
10
use Marek\OpenWeatherMap\API\Value\Response\GeographicCoordinates;
11
12
class AirPollutionDenormalizer extends AbstractDenormalizer
13
{
14
    public function denormalize(array $data, APIResponse $response): APIResponse
15
    {
16
        if ($response instanceof CarbonMonoxide) {
17
            $response->time = $this->getValue('time', $data, \DateTimeInterface::class);
18
19
            $location = new GeographicCoordinates();
20
            $location->latitude = $this->getData('latitude', $data['location']);
21
            $location->longitude = $this->getData('longitude', $data['location']);
22
23
            $response->location = $location;
24
25
            $polutions = [];
26
            foreach ($data['data'] as $datum) {
27
                $pollution = new AirPollution();
28
                $pollution->precision = $datum['precision'];
29
                $pollution->pressure = $datum['pressure'];
30
                $pollution->value = $datum['value'];
31
32
                $polutions[] = $pollution;
33
            }
34
35
            $response->data = $polutions;
36
37
            return $response;
38
        }
39
40
        return $response;
41
    }
42
}
43