Passed
Branch main (f9aaf7)
by Jonathan
14:43
created

PlaceMapperService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A all() 0 13 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) 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\Services\ModuleService;
18
use Illuminate\Contracts\Container\BindingResolutionException;
19
use Illuminate\Support\Collection;
20
use MyArtJaub\Webtrees\Contracts\GeoDispersion\ModulePlaceMapperProviderInterface;
21
use MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface;
22
23
/**
24
 * Service for accessing available place mappers .
25
 */
26
class PlaceMapperService
27
{
28
    private ModuleService $module_service;
29
30
    /**
31
     * Constructor for PlaceMapperService
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
     * Get all place mappers available.
42
     *
43
     * {@internal The list is generated based on the modules exposing ModulePlaceMapperProviderInterface}
44
     *
45
     * @param bool $include_disabled
46
     * @return Collection
47
     */
48
    public function all(bool $include_disabled = false): Collection
49
    {
50
        return $this->module_service
51
            ->findByInterface(ModulePlaceMapperProviderInterface::class, $include_disabled)
52
            ->flatMap(fn(ModulePlaceMapperProviderInterface $module) => $module->listPlaceMappers())
53
            ->map(static function (string $mapper_class): ?PlaceMapperInterface {
54
                try {
55
                    $mapper = app($mapper_class);
56
                    return $mapper instanceof PlaceMapperInterface ? $mapper : null;
57
                } catch (BindingResolutionException $ex) {
58
                    return null;
59
                }
60
            })->filter();
61
        ;
62
    }
63
}
64