Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
KExport
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
2 / 2
8
100.00% covered (success)
100.00%
1 / 1
 getType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 parse
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2
3declare(strict_types=1);
4
5namespace App\Parser\Type;
6
7use JsonException;
8use UnexpectedValueException;
9use App\Entity\Waypoint;
10use App\Parser\AbstractParser;
11
12class KExport extends AbstractParser
13{
14    protected function getType(): string
15    {
16        return 'kexport';
17    }
18
19    /**
20     * @param array<string> $data
21     *
22     * @return Waypoint[]
23     */
24    public function parse(array $data): array
25    {
26        $waypoints = [];
27        try {
28            $items = json_decode(
29                $data[$this->getType()],
30                true,
31                512,
32                JSON_THROW_ON_ERROR
33            );
34        } catch (JsonException) {
35            throw new UnexpectedValueException(
36                'Invalid KExport JSON data'
37            );
38        }
39
40        /** @var array<array{guid?: string, title?: string, lat: float, lng: float, image?: string}> $items */
41        foreach ($items as $item) {
42            if (!$guid = $item['guid'] ?? '') {
43                continue;
44            }
45
46            if (!$title = $item['title'] ?? '') {
47                continue;
48            }
49
50            $lat = $item['lat'];
51            $lon = $item['lng'];
52            $image = $item['image'] ?? '';
53
54            if (isset($data['importImages']) && $data['importImages']) {
55                $this->wayPointHelper->checkImage($guid, $image);
56            }
57
58            $waypoints[] = $this->createWayPoint($guid, $lat, $lon, $title, $image);
59        }
60
61        return $waypoints;
62    }
63}