Passed
Branch feature/2.1-geodispersion-dev (1d61a8)
by Jonathan
61:21
created

FilteredTopPlaceMapperConfig   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 57
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A jsonSerializeConfig() 0 9 1
A jsonDeserialize() 0 23 5
A __construct() 0 3 1
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) 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\PlaceMappers\Config;
16
17
use Fisharebest\Webtrees\Place;
18
use Fisharebest\Webtrees\Services\TreeService;
19
use MyArtJaub\Webtrees\Common\GeoDispersion\Config\GenericPlaceMapperConfig;
20
use RuntimeException;
21
22
/**
23
 * Place Mapper configuration for mappers filtering on Top Places.
24
 */
25
class FilteredTopPlaceMapperConfig extends GenericPlaceMapperConfig
26
{
27
    private TreeService $tree_service;
28
29
    /**
30
     * FilteredTopPlaceMapperConfig
31
     *
32
     * @param TreeService $tree_service
33
     */
34
    public function __construct(TreeService $tree_service)
35
    {
36
        $this->tree_service = $tree_service;
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     * @see \MyArtJaub\Webtrees\Common\GeoDispersion\Config\GenericPlaceMapperConfig::jsonSerializeConfig()
42
     */
43
    public function jsonSerializeConfig()
44
    {
45
        return [
46
            'topPlaces' => collect($this->get('topPlaces', []))
47
                ->filter(
48
                    /** @psalm-suppress MissingClosureParamType */
49
                    fn($item): bool => $item instanceof Place
50
                )->map(fn(Place $place): array => [ $place->tree()->id(), $place->gedcomName() ])
51
                ->toArray()
52
        ];
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     * @see \MyArtJaub\Webtrees\Common\GeoDispersion\Config\GenericPlaceMapperConfig::jsonDeserialize()
58
     */
59
    public function jsonDeserialize($config): self
60
    {
61
        if (is_string($config)) {
62
            return $this->jsonDeserialize(json_decode($config));
63
        }
64
        if (is_array($config)) {
65
            $this->setConfig([
66
                'topPlaces' => collect($config['topPlaces'] ?? [])
67
                    ->filter(
68
                        /** @psalm-suppress MissingClosureParamType */
69
                        fn($item): bool => is_array($item) && count($item) == 2
70
                    )->map(function (array $item): ?Place {
71
                        try {
72
                            return new Place($item[1], $this->tree_service->find($item[0]));
73
                        } catch (RuntimeException $ex) {
74
                            return null;
75
                        }
76
                    })
77
                    ->filter()
78
                    ->toArray()
79
                ]);
80
        }
81
        return $this;
82
    }
83
}
84