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

MapDefinitionsService   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 42
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 3 1
A __construct() 0 3 1
A all() 0 8 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\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
            fn() => $this->module_service
65
                ->findByInterface(ModuleMapDefinitionProviderInterface::class, $include_disabled)
66
                ->flatMap(fn(ModuleMapDefinitionProviderInterface $module) => $module->listMapDefinition())
67
                ->mapWithKeys(fn(MapDefinitionInterface $map) => [ $map->id() => $map ])
68
        );
69
    }
70
}
71