Passed
Push — feature/code-analysis ( e964aa...4fe35d )
by Jonathan
14:33
created

SosaConfig   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B handle() 0 44 6
1
<?php
2
3
/**
4
 * webtrees-lib: MyArtJaub library for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees
7
 * @subpackage Sosa
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2009-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\Sosa\Http\RequestHandlers;
16
17
use Fisharebest\Webtrees\Auth;
18
use Fisharebest\Webtrees\DefaultUser;
19
use Fisharebest\Webtrees\I18N;
20
use Fisharebest\Webtrees\Tree;
21
use Fisharebest\Webtrees\Validator;
22
use Fisharebest\Webtrees\Http\ViewResponseTrait;
23
use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
24
use Fisharebest\Webtrees\Services\ModuleService;
25
use MyArtJaub\Webtrees\Module\Sosa\SosaModule;
26
use MyArtJaub\Webtrees\Module\Sosa\Services\SosaRecordsService;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
use Psr\Http\Server\RequestHandlerInterface;
30
31
/**
32
 * Request handler for configuring the Sosa de-cujus
33
 */
34
class SosaConfig implements RequestHandlerInterface
35
{
36
    use ViewResponseTrait;
37
38
    /**
39
     * @var SosaModule|null $module
40
     */
41
    private $module;
42
43
    /**
44
     * Constructor for SosaConfig Request Handler
45
     *
46
     * @param ModuleService $module_service
47
     */
48
    public function __construct(ModuleService $module_service)
49
    {
50
        $this->module = $module_service->findByInterface(SosaModule::class)->first();
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
56
     */
57
    public function handle(ServerRequestInterface $request): ResponseInterface
58
    {
59
        if ($this->module === null) {
60
            throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
61
        }
62
63
        $tree = Validator::attributes($request)->tree();
64
65
        $users_root = array();
66
        if (Auth::check()) {
67
            /** @var \Fisharebest\Webtrees\User $user */
68
            $user = Auth::user();
69
            $users_root[] = [
70
                'user'      => $user,
71
                'root_id'   => $tree->getUserPreference($user, 'MAJ_SOSA_ROOT_ID'),
72
                'max_gen'   => $tree->getUserPreference($user, 'MAJ_SOSA_MAX_GEN')
73
            ];
74
75
            if (Auth::isManager($tree)) {
76
                $default_user = new DefaultUser();
77
                $users_root[] = [
78
                    'user' => $default_user,
79
                    'root_id' => $tree->getUserPreference($default_user, 'MAJ_SOSA_ROOT_ID'),
80
                    'max_gen'   => $tree->getUserPreference($default_user, 'MAJ_SOSA_MAX_GEN')
81
                ];
82
            }
83
        }
84
85
        // Use the system max generations if not set
86
        $max_gen_system = app(SosaRecordsService::class)->maxSystemGenerations();
87
        foreach ($users_root as $key => $user_root) {
88
            $users_root[$key]['max_gen'] = is_numeric($user_root['max_gen']) ?
89
                (int) $user_root['max_gen'] :
90
                $max_gen_system;
91
        };
92
93
        return $this->viewResponse($this->module->name() . '::config-page', [
94
            'module_name'       =>  $this->module->name(),
95
            'title'             =>  I18N::translate('Sosa Configuration'),
96
            'tree'              =>  $tree,
97
            'user_id'           =>  Validator::attributes($request)->user(),
98
            'selected_user_id'  =>  Validator::queryParams($request)->integer('user_id', 0),
99
            'immediate_compute' =>  Validator::queryParams($request)->string('compute', '') == 'yes',
100
            'users_root'        =>  $users_root
101
        ]);
102
    }
103
}
104