Passed
Push — master ( c9a927...3e5f5a )
by Greg
05:29
created

IndividualFactory::new()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 3
rs 10
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\Factories;
21
22
use Closure;
23
use Fisharebest\Webtrees\Contracts\IndividualFactoryInterface;
24
use Fisharebest\Webtrees\Individual;
25
use Fisharebest\Webtrees\Tree;
26
use Illuminate\Database\Capsule\Manager as DB;
27
use stdClass;
28
29
use function assert;
30
use function preg_match;
31
32
/**
33
 * Make a Individual object.
34
 */
35
class IndividualFactory extends AbstractGedcomRecordFactory implements IndividualFactoryInterface
36
{
37
    private const TYPE_CHECK_REGEX = '/^0 @[^@]+@ ' . Individual::RECORD_TYPE . '/';
38
39
    /**
40
     * Create an individual.
41
     *
42
     * @param string      $xref
43
     * @param Tree        $tree
44
     * @param string|null $gedcom
45
     *
46
     * @return Individual|null
47
     */
48
    public function make(string $xref, Tree $tree, string $gedcom = null): ?Individual
49
    {
50
        return $this->cache->remember(__CLASS__ . $xref . '@' . $tree->id(), function () use ($xref, $tree, $gedcom) {
51
            $gedcom  = $gedcom ?? $this->gedcom($xref, $tree);
52
            $pending = $this->pendingChanges($tree)->get($xref);
53
54
            if ($gedcom === null && ($pending === null || !preg_match(self::TYPE_CHECK_REGEX, $pending))) {
55
                return null;
56
            }
57
            $xref = $this->extractXref($gedcom ?? $pending, $xref);
58
59
            return new Individual($xref, $gedcom ?? '', $pending, $tree);
60
        });
61
    }
62
63
    /**
64
     * Create an individual from a row in the database.
65
     *
66
     * @param Tree $tree
67
     *
68
     * @return Closure
69
     */
70
    public function mapper(Tree $tree): Closure
71
    {
72
        return function (stdClass $row) use ($tree): Individual {
73
            $individual = $this->make($row->i_id, $tree, $row->i_gedcom);
74
            assert($individual instanceof Individual);
75
76
            return $individual;
77
        };
78
    }
79
80
    /**
81
     * Create an individual from raw GEDCOM data.
82
     *
83
     * @param string      $xref
84
     * @param string      $gedcom  an empty string for new/pending records
85
     * @param string|null $pending null for a record with no pending edits,
86
     *                             empty string for records with pending deletions
87
     * @param Tree        $tree
88
     *
89
     * @return Individual
90
     */
91
    public function new(string $xref, string $gedcom, ?string $pending, Tree $tree): Individual
92
    {
93
        return new Individual($xref, $gedcom, $pending, $tree);
94
    }
95
96
    /**
97
     * Fetch GEDCOM data from the database.
98
     *
99
     * @param string $xref
100
     * @param Tree   $tree
101
     *
102
     * @return string|null
103
     */
104
    protected function gedcom(string $xref, Tree $tree): ?string
105
    {
106
        return DB::table('individuals')
107
            ->where('i_id', '=', $xref)
108
            ->where('i_file', '=', $tree->id())
109
            ->value('i_gedcom');
110
    }
111
}
112