Lines 100.00% 18 / 18
Functions and Methods 100.00% 3 / 3
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
BannerMap 100.00% 18 / 18 100.00% 3 / 3 8 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 points 100.00% 7 / 7 100.00% 1 / 1 4
 point 100.00% 10 / 10 100.00% 1 / 1 3
1<?php
2
3declare(strict_types=1);
4
5namespace App\Bannergress;
6
7use App\ApiResource\Bannergress;
8use App\Entity\DoneBanner;
9use Symfony\Contracts\Translation\TranslatorInterface;
10
11/**
12 * The dots of the banners page's world map (banner_map_controller.js): where each banner with a
13 * start point starts, with its list (published, done, todo), title, link and a label.
14 */
15final readonly class BannerMap
16{
17    public function __construct(private TranslatorInterface $translator)
18    {
19    }
20
21    /**
22     * @param list<DoneBanner> $doneBanners
23     *
24     * @return list<array{lat: float, lng: float, list: string, title: string, url: string, label: string}>
25     */
26    public function points(Bannergress $bannergress, array $doneBanners): array
27    {
28        $points = [];
29        foreach (['published' => $bannergress->publishedBanners, 'todo' => $bannergress->todoBanners] as $list => $banners) {
30            foreach ($banners as $banner) {
31                $points[] = $this->point($list, $banner['startLatitude'] ?? null, $banner['startLongitude'] ?? null, $banner['title'], Bannergress::BANNER_BASE.\rawurlencode($banner['id']), $banner['missions']);
32            }
33        }
34
35        foreach ($doneBanners as $banner) {
36            $points[] = $this->point('done', $banner->startLatitude, $banner->startLongitude, $banner->title, $banner->getUrl(), $banner->missions);
37        }
38
39        return \array_values(\array_filter($points));
40    }
41
42    /**
43     * @return array{lat: float, lng: float, list: string, title: string, url: string, label: string}|null null without a start point
44     */
45    private function point(string $list, float|int|null $latitude, float|int|null $longitude, string $title, string $url, int $missions): ?array
46    {
47        if (null === $latitude || null === $longitude) {
48            return null;
49        }
50
51        return [
52            'lat' => (float) $latitude,
53            'lng' => (float) $longitude,
54            'list' => $list,
55            'title' => $title,
56            'url' => $url,
57            'label' => $this->translator->trans('bannergress.'.$list).' ยท '.$this->translator->trans('bannergress.missions', ['%count%' => $missions]),
58        ];
59    }
60}