Passed
Branch main (f9aaf7)
by Jonathan
14:43
created

MapFeaturePropertyData   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 25 3
1
<?php
2
3
/**
4
 * webtrees-lib: MyArtJaub library for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees
7
 * @subpackage GeoDispersion
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2009-2021, Jonathan Jaubart
10
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
11
 */
12
13
declare(strict_types=1);
14
15
namespace MyArtJaub\Webtrees\Module\GeoDispersion\Http\RequestHandlers;
16
17
use Brick\Geo\IO\GeoJSON\Feature;
18
use Fisharebest\Webtrees\I18N;
19
use Fisharebest\Webtrees\Registry;
20
use Fisharebest\Webtrees\Exceptions\HttpNotFoundException;
21
use MyArtJaub\Webtrees\Module\GeoDispersion\Services\MapDefinitionsService;
22
use Psr\Http\Message\ResponseInterface;
23
use Psr\Http\Message\ServerRequestInterface;
24
use Psr\Http\Server\RequestHandlerInterface;
25
use stdClass;
26
27
/**
28
 * Request handler for listing the feature properties of a map.
29
 */
30
class MapFeaturePropertyData implements RequestHandlerInterface
31
{
32
    private MapDefinitionsService $map_definition_service;
33
34
    /**
35
     * Constructor for MapFeaturePropertyData Request Handler
36
     *
37
     * @param MapDefinitionsService $map_definition_service
38
     */
39
    public function __construct(
40
        MapDefinitionsService $map_definition_service
41
    ) {
42
        $this->map_definition_service = $map_definition_service;
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
48
     */
49
    public function handle(ServerRequestInterface $request): ResponseInterface
50
    {
51
        $map_id = $request->getQueryParams()['map_id'] ?? $request->getAttribute('map_id') ?? '';
52
53
        return response(Registry::cache()->file()->remember(
54
            'map-properties-' . $map_id,
55
            function () use ($map_id): array {
56
                $map = $this->map_definition_service->find($map_id);
57
                if ($map === null) {
58
                    throw new HttpNotFoundException(I18N::translate('The map could not be found.'));
59
                }
60
61
                $features = [];
62
                collect($map->features())
63
                    ->map(fn(Feature $feature): ?stdClass => $feature->getProperties())
64
                    ->filter()
65
                    ->map(fn(stdClass $properties): array => array_keys(get_object_vars($properties)))
66
                    ->each(function (array $properties) use (&$features): void {
67
                        $features = count($features) === 0 ? $properties : array_intersect($features, $properties);
68
                    });
69
70
                usort($features, I18N::comparator());
71
                return  $features;
72
            },
73
            86400000
74
        ));
75
    }
76
}
77