Completed
Push — develop ( dd8217...f3d6a4 )
by Greg
16:04 queued 07:43
created

Source::rowMapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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;
21
22
use Closure;
23
use Fisharebest\Webtrees\Http\RequestHandlers\SourcePage;
24
use Illuminate\Database\Capsule\Manager as DB;
25
26
/**
27
 * A GEDCOM source (SOUR) object.
28
 */
29
class Source extends GedcomRecord
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\GedcomRecord 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...
30
{
31
    public const RECORD_TYPE = 'SOUR';
32
33
    protected const ROUTE_NAME  = SourcePage::class;
34
35
    /**
36
     * A closure which will create a record from a database row.
37
     *
38
     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Registry::sourceFactory()
39
     *
40
     * @param Tree $tree
41
     *
42
     * @return Closure
43
     */
44
    public static function rowMapper(Tree $tree): Closure
45
    {
46
        return Registry::sourceFactory()->mapper($tree);
47
    }
48
49
    /**
50
     * Get an instance of a source object. For single records,
51
     * we just receive the XREF. For bulk records (such as lists
52
     * and search results) we can receive the GEDCOM data as well.
53
     *
54
     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Registry::sourceFactory()
55
     *
56
     * @param string      $xref
57
     * @param Tree        $tree
58
     * @param string|null $gedcom
59
     *
60
     * @return Source|null
61
     */
62
    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?Source
63
    {
64
        return Registry::sourceFactory()->make($xref, $tree, $gedcom);
65
    }
66
67
    /**
68
     * Each object type may have its own special rules, and re-implement this function.
69
     *
70
     * @param int $access_level
71
     *
72
     * @return bool
73
     */
74
    protected function canShowByType(int $access_level): bool
75
    {
76
        // Hide sources if they are attached to private repositories ...
77
        preg_match_all('/\n1 REPO @(.+)@/', $this->gedcom, $matches);
78
        foreach ($matches[1] as $match) {
79
            $repo = Registry::repositoryFactory()->make($match, $this->tree);
80
            if ($repo && !$repo->canShow($access_level)) {
81
                return false;
82
            }
83
        }
84
85
        // ... otherwise apply default behavior
86
        return parent::canShowByType($access_level);
87
    }
88
89
    /**
90
     * Generate a private version of this record
91
     *
92
     * @param int $access_level
93
     *
94
     * @return string
95
     */
96
    protected function createPrivateGedcomRecord(int $access_level): string
97
    {
98
        return '0 @' . $this->xref . "@ SOUR\n1 TITL " . I18N::translate('Private');
99
    }
100
101
    /**
102
     * Extract names from the GEDCOM record.
103
     *
104
     * @return void
105
     */
106
    public function extractNames(): void
107
    {
108
        $this->extractNamesFromFacts(1, 'TITL', $this->facts(['TITL']));
109
    }
110
111
    /**
112
     * Lock the database row, to prevent concurrent edits.
113
     */
114
    public function lock(): void
115
    {
116
        DB::table('sources')
117
            ->where('s_file', '=', $this->tree->id())
118
            ->where('s_id', '=', $this->xref())
119
            ->lockForUpdate()
120
            ->get();
121
    }
122
}
123