Completed
Push — master ( f4cd70...9f667f )
by Greg
13:48 queued 05:59
created

AbstractModuleComponentPage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2020 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\Auth;
23
use Fisharebest\Webtrees\Http\ViewResponseTrait;
24
use Fisharebest\Webtrees\Module\ModuleInterface;
25
use Fisharebest\Webtrees\Services\ModuleService;
26
use Fisharebest\Webtrees\Services\TreeService;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Server\RequestHandlerInterface;
29
30
/**
31
 * Show a list of modules.
32
 */
33
abstract class AbstractModuleComponentPage implements RequestHandlerInterface
34
{
35
    use ViewResponseTrait;
36
37
    /** @var ModuleService */
38
    private $module_service;
39
40
    /** @var TreeService */
41
    private $tree_service;
42
43
    /**
44
     * @param ModuleService $module_service
45
     * @param TreeService   $tree_service
46
     */
47
    public function __construct(ModuleService $module_service, TreeService $tree_service)
48
    {
49
        $this->module_service = $module_service;
50
        $this->tree_service   = $tree_service;
51
    }
52
53
    /**
54
     * @param string $interface
55
     * @param string $title
56
     * @param string $description
57
     *
58
     * @return ResponseInterface
59
     */
60
    protected function listComponents(string $interface, string $title, string $description): ResponseInterface
61
    {
62
        $this->layout = 'layouts/administration';
63
64
        $modules      = $this->module_service->findByInterface($interface, true, true);
65
        $uses_access  = $this->module_service->componentsWithAccess()->containsStrict($interface);
66
        $uses_sorting = $this->module_service->componentsWithOrder()->containsStrict($interface);
67
68
        $access_summary = $modules
69
            ->mapWithKeys(function (ModuleInterface $module) use ($interface): array {
70
                $access_levels = $this->tree_service->all()
71
                    ->map(static function ($tree) use ($interface, $module): int {
72
                        return $module->accessLevel($tree, $interface);
73
                    })
74
                    ->uniqueStrict()
75
                    ->values()
76
                    ->map(static function (int $level): string {
77
                        return Auth::accessLevelNames()[$level];
78
                    })
79
                    ->all();
80
81
                return [$module->name() => $access_levels];
82
            })
83
            ->all();
84
85
        return $this->viewResponse('admin/components', [
86
            'description'    => $description,
87
            'interface'      => $interface,
88
            'modules'        => $modules,
89
            'title'          => $title,
90
            'trees'          => $this->tree_service->all(),
91
            'uses_access'    => $uses_access,
92
            'uses_sorting'   => $uses_sorting,
93
            'access_summary' => $access_summary,
94
        ]);
95
    }
96
}
97