Passed
Push — master ( 63f369...ac5ee7 )
by Greg
05:52
created

Submission   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 87
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 9 2
A fetchGedcomRecord() 0 7 1
A extractNames() 0 7 1
A rowMapper() 0 7 1
A createPrivateGedcomRecord() 0 3 1
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2019 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;
21
22
use Closure;
23
use Exception;
24
use Fisharebest\Webtrees\Http\RequestHandlers\SubmissionPage;
25
use Illuminate\Database\Capsule\Manager as DB;
26
use stdClass;
27
28
/**
29
 * A GEDCOM submission (SUBN) object.
30
 * These records are only used when transferring data between two obsolete systems.
31
 * There is no need to create them - but we may encounter them in imported GEDCOM files.
32
 */
33
class Submission extends GedcomRecord
34
{
35
    public const RECORD_TYPE = 'SUBN';
36
37
    protected const ROUTE_NAME = SubmissionPage::class;
38
39
    /**
40
     * A closure which will create a record from a database row.
41
     *
42
     * @param Tree $tree
43
     *
44
     * @return Closure
45
     */
46
    public static function rowMapper(Tree $tree): Closure
47
    {
48
        return static function (stdClass $row) use ($tree): Submission {
49
            $submission = Submission::getInstance($row->o_id, $tree, $row->o_gedcom);
50
            assert($submission instanceof Submission);
51
52
            return $submission;
53
        };
54
    }
55
56
    /**
57
     * Get an instance of a submission object. For single records,
58
     * we just receive the XREF. For bulk records (such as lists
59
     * and search results) we can receive the GEDCOM data as well.
60
     *
61
     * @param string      $xref
62
     * @param Tree        $tree
63
     * @param string|null $gedcom
64
     *
65
     * @throws Exception
66
     *
67
     * @return Submission|null
68
     */
69
    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?Submission
70
    {
71
        $record = parent::getInstance($xref, $tree, $gedcom);
72
73
        if ($record instanceof self) {
74
            return $record;
75
        }
76
77
        return null;
78
    }
79
80
    /**
81
     * Fetch data from the database
82
     *
83
     * @param string $xref
84
     * @param int    $tree_id
85
     *
86
     * @return string|null
87
     */
88
    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
89
    {
90
        return DB::table('other')
91
            ->where('o_id', '=', $xref)
92
            ->where('o_file', '=', $tree_id)
93
            ->where('o_type', '=', self::RECORD_TYPE)
94
            ->value('o_gedcom');
95
    }
96
97
    /**
98
     * Generate a private version of this record
99
     *
100
     * @param int $access_level
101
     *
102
     * @return string
103
     */
104
    protected function createPrivateGedcomRecord(int $access_level): string
105
    {
106
        return '0 @' . $this->xref . "@ SUBM\n1 NAME " . I18N::translate('Private');
107
    }
108
    /**
109
     * Extract names from the GEDCOM record.
110
     *
111
     * @return void
112
     */
113
    public function extractNames(): void
114
    {
115
        $this->getAllNames[] = [
116
            'type'   => self::RECORD_TYPE,
117
            'sort'   => I18N::translate('Submission'),
118
            'full'   => I18N::translate('Submission'),
119
            'fullNN' => I18N::translate('Submission'),
120
        ];
121
    }
122
}
123