Passed
Branch feature/2.1-geodispersion-dev (38d49e)
by Jonathan
04:17
created

MapFeaturePropertyData   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 41
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 22 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('map-properties-' . $map_id, function () use ($map_id) {
54
            $map = $this->map_definition_service->find($map_id);
55
            if ($map === null) {
56
                throw new HttpNotFoundException(I18N::translate('The map could not be found.'));
57
            }
58
59
            $features = [];
60
            collect($map->features())
61
                ->map(fn(Feature $feature): ?stdClass => $feature->getProperties())
62
                ->filter()
63
                ->map(fn(stdClass $properties) => array_keys(get_object_vars($properties)))
64
                ->each(function (array $properties) use (&$features) {
65
                    $features = count($features) === 0 ? $properties : array_intersect($features, $properties);
66
                });
67
68
            usort($features, I18N::comparator());
69
            return  $features;
70
        }, 86400000));
71
    }
72
}
73