MapFeaturePropertyData::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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-2022, 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\Validator;
21
use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
22
use MyArtJaub\Webtrees\Module\GeoDispersion\Services\MapDefinitionsService;
23
use Psr\Http\Message\ResponseInterface;
24
use Psr\Http\Message\ServerRequestInterface;
25
use Psr\Http\Server\RequestHandlerInterface;
26
use stdClass;
27
28
/**
29
 * Request handler for listing the feature properties of a map.
30
 */
31
class MapFeaturePropertyData implements RequestHandlerInterface
32
{
33
    private MapDefinitionsService $map_definition_service;
34
35
    /**
36
     * Constructor for MapFeaturePropertyData Request Handler
37
     *
38
     * @param MapDefinitionsService $map_definition_service
39
     */
40
    public function __construct(
41
        MapDefinitionsService $map_definition_service
42
    ) {
43
        $this->map_definition_service = $map_definition_service;
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
49
     */
50
    public function handle(ServerRequestInterface $request): ResponseInterface
51
    {
52
        $map_id = Validator::queryParams($request)->string(
53
            'map_id',
54
            Validator::attributes($request)->string('map_id', '')
55
        );
56
57
        return Registry::responseFactory()->response(Registry::cache()->file()->remember(
58
            'map-properties-' . $map_id,
59
            function () use ($map_id): array {
60
                $map = $this->map_definition_service->find($map_id);
61
                if ($map === null) {
62
                    throw new HttpNotFoundException(I18N::translate('The map could not be found.'));
63
                }
64
65
                $features = [];
66
                collect($map->features())
67
                    ->map(fn(Feature $feature): ?stdClass => $feature->getProperties())
68
                    ->filter()
69
                    ->map(fn(stdClass $properties): array => array_keys(get_object_vars($properties)))
70
                    ->each(function (array $properties) use (&$features): void {
71
                        $features = count($features) === 0 ? $properties : array_intersect($features, $properties);
72
                    });
73
74
                usort($features, I18N::comparator());
75
                return  $features;
76
            },
77
            86400000
78
        ));
79
    }
80
}
81