SosaComputeAction   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B handle() 0 32 8
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 Fig\Http\Message\StatusCodeInterface;
18
use Fisharebest\Webtrees\Auth;
19
use Fisharebest\Webtrees\DefaultUser;
20
use Fisharebest\Webtrees\I18N;
21
use Fisharebest\Webtrees\Registry;
22
use Fisharebest\Webtrees\Validator;
23
use Fisharebest\Webtrees\Services\UserService;
24
use MyArtJaub\Webtrees\Module\Sosa\Services\SosaCalculatorService;
25
use Psr\Http\Message\ResponseInterface;
26
use Psr\Http\Message\ServerRequestInterface;
27
use Psr\Http\Server\RequestHandlerInterface;
28
29
/**
30
 * Request handler for computing the Sosa ancestors
31
 */
32
class SosaComputeAction implements RequestHandlerInterface
33
{
34
    private UserService $user_service;
35
36
    /**
37
     * Constructor for SosaConfigAction Request Handler
38
     *
39
     * @param UserService $user_service
40
     */
41
    public function __construct(UserService $user_service)
42
    {
43
        $this->user_service = $user_service;
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
49
     */
50
    public function handle(ServerRequestInterface $request): ResponseInterface
51
    {
52
        $tree = Validator::attributes($request)->tree();
53
54
        $user_id = Validator::parsedBody($request)->integer('user_id', Auth::id() ?? 0);
55
        $partial_from = Validator::parsedBody($request)->isXref()->string('partial_from', '');
56
57
        if (($user_id === -1 && Auth::isManager($tree)) || Auth::id() === $user_id) {
58
            $user = $user_id === -1 ? new DefaultUser() : $this->user_service->find($user_id);
59
60
            /** @var SosaCalculatorService $sosa_calc_service */
61
            $sosa_calc_service = app()->makeWith(SosaCalculatorService::class, [ 'tree' => $tree, 'user' => $user]);
62
63
            if (
64
                $partial_from !== '' &&
65
                ($sosa_from = Registry::individualFactory()->make($partial_from, $tree)) !== null
66
            ) {
67
                $res = $sosa_calc_service->computeFromIndividual($sosa_from);
68
            } else {
69
                $res = $sosa_calc_service->computeAll();
70
            }
71
72
            return $res ?
73
                Registry::responseFactory()->response() :
74
                Registry::responseFactory()->response(
75
                    I18N::translate('An error occurred while computing Sosa ancestors.'),
76
                    StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR
77
                );
78
        }
79
        return Registry::responseFactory()->response(
80
            I18N::translate('You do not have permission to modify the user.'),
81
            StatusCodeInterface::STATUS_FORBIDDEN
82
        );
83
    }
84
}
85