MapDefinitionsService   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 3 1
A __construct() 0 3 1
A all() 0 12 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-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\Services;
16
17
use Fisharebest\Webtrees\Registry;
18
use Fisharebest\Webtrees\Services\ModuleService;
19
use Illuminate\Support\Collection;
20
use MyArtJaub\Webtrees\Contracts\GeoDispersion\MapDefinitionInterface;
21
use MyArtJaub\Webtrees\Contracts\GeoDispersion\ModuleMapDefinitionProviderInterface;
22
23
/**
24
 * Service for accessing map definitions .
25
 */
26
class MapDefinitionsService
27
{
28
    private ModuleService $module_service;
29
30
    /**
31
     * Constructor for MapDefinitionsService
32
     *
33
     * @param ModuleService $module_service
34
     */
35
    public function __construct(ModuleService $module_service)
36
    {
37
        $this->module_service = $module_service;
38
    }
39
40
    /**
41
     * Find a map definition by ID.
42
     *
43
     * @param string $id
44
     * @return MapDefinitionInterface|NULL
45
     */
46
    public function find(string $id): ?MapDefinitionInterface
47
    {
48
        return $this->all()->get($id);
49
    }
50
51
    /**
52
     * Get all map definitions available.
53
     *
54
     * {@internal The list is generated based on the modules exposing ModuleMapDefinitionProviderInterface,
55
     * and the result is cached}
56
     *
57
     * @param bool $include_disabled
58
     * @return Collection<string, MapDefinitionInterface>
59
     */
60
    public function all(bool $include_disabled = false): Collection
61
    {
62
        return Registry::cache()->array()->remember(
63
            'maj-geodisp-maps-all',
64
            function () use ($include_disabled): Collection {
65
                /** @var Collection<string, MapDefinitionInterface> $map_definitions */
66
                $map_definitions = $this->module_service
67
                    ->findByInterface(ModuleMapDefinitionProviderInterface::class, $include_disabled)
68
                    ->flatMap(fn(ModuleMapDefinitionProviderInterface $module) => $module->listMapDefinition())
69
                    ->mapWithKeys(fn(MapDefinitionInterface $map) => [ $map->id() => $map ]);
70
71
                return $map_definitions;
72
            }
73
        );
74
    }
75
}
76