CreateSourceAction   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 62 8
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 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 <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\I18N;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\I18N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Fisharebest\Webtrees\Registry;
24
use Fisharebest\Webtrees\Validator;
25
use Psr\Http\Message\ResponseInterface;
26
use Psr\Http\Message\ServerRequestInterface;
27
use Psr\Http\Server\RequestHandlerInterface;
28
29
/**
30
 * Process a form to create a new source.
31
 */
32
class CreateSourceAction implements RequestHandlerInterface
33
{
34
    /**
35
     * @param ServerRequestInterface $request
36
     *
37
     * @return ResponseInterface
38
     */
39
    public function handle(ServerRequestInterface $request): ResponseInterface
40
    {
41
        $tree         = Validator::attributes($request)->tree();
42
        $title        = Validator::parsedBody($request)->isNotEmpty()->string('source-title');
43
        $abbreviation = Validator::parsedBody($request)->string('source-abbreviation');
44
        $author       = Validator::parsedBody($request)->string('source-author');
45
        $publication  = Validator::parsedBody($request)->string('source-publication');
46
        $repository   = Validator::parsedBody($request)->isXref()->string('source-repository', '');
47
        $call_number  = Validator::parsedBody($request)->string('source-call-number');
48
        $text         = Validator::parsedBody($request)->string('source-text');
49
        $restriction  = Validator::parsedBody($request)->string('restriction');
50
51
        $title        = Registry::elementFactory()->make('SOUR:TITL')->canonical($title);
52
        $abbreviation = Registry::elementFactory()->make('SOUR:ABBR')->canonical($abbreviation);
53
        $author       = Registry::elementFactory()->make('SOUR:AUTH')->canonical($author);
54
        $publication  = Registry::elementFactory()->make('SOUR:PUBL')->canonical($publication);
55
        $repository   = Registry::elementFactory()->make('SOUR:REPO')->canonical($repository);
56
        $call_number  = Registry::elementFactory()->make('SOUR:REPO:CALN')->canonical($call_number);
57
        $text         = Registry::elementFactory()->make('SOUR:TEXT')->canonical($text);
58
        $restriction  = Registry::elementFactory()->make('SOUR:RESN')->canonical($restriction);
59
60
        $gedcom = "0 @@ SOUR\n1 TITL " . strtr($title, ["\n" => "\n2 CONT "]);
61
62
        if ($abbreviation !== '') {
63
            $gedcom .= "\n1 ABBR " . strtr($abbreviation, ["\n" => "\n2 CONT "]);
64
        }
65
66
        if ($author !== '') {
67
            $gedcom .= "\n1 AUTH " . strtr($author, ["\n" => "\n2 CONT "]);
68
        }
69
70
        if ($publication !== '') {
71
            $gedcom .= "\n1 PUBL " . strtr($publication, ["\n" => "\n2 CONT "]);
72
        }
73
74
        if ($text !== '') {
75
            $gedcom .= "\n1 TEXT " . strtr($text, ["\n" => "\n2 CONT "]);
76
        }
77
78
        if ($repository !== '') {
79
            $gedcom .= "\n1 REPO @" . $repository . '@';
80
81
            if ($call_number !== '') {
82
                $gedcom .= "\n2 CALN " . strtr($call_number, ["\n" => "\n3 CONT "]);
83
            }
84
        }
85
86
        if ($restriction !== '') {
87
            $gedcom .= "\n1 RESN " . strtr($restriction, ["\n" => "\n2 CONT "]);
88
        }
89
90
        $record = $tree->createRecord($gedcom);
91
92
        // value and text are for autocomplete
93
        // html is for interactive modals
94
        return response([
95
            'value' => '@' . $record->xref() . '@',
96
            'text'  => view('selects/source', ['source' => $record]),
97
            'html'  => view('modals/record-created', [
98
                'title' => I18N::translate('The source has been created'),
99
                'name'  => $record->fullName(),
100
                'url'   => $record->url(),
101
            ]),
102
        ]);
103
    }
104
}
105