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

AbstractGedcomRecordFactory::pendingChanges()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
rs 10
c 1
b 0
f 0
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 Fisharebest\Webtrees\Cache;
23
use Fisharebest\Webtrees\Gedcom;
24
use Fisharebest\Webtrees\Tree;
25
use Illuminate\Database\Capsule\Manager as DB;
26
use Illuminate\Support\Collection;
27
use stdClass;
28
29
/**
30
 * Make a GedcomRecord object.
31
 */
32
abstract class AbstractGedcomRecordFactory
33
{
34
    /** @var Cache */
35
    protected $cache;
36
37
    /**
38
     * GedcomRecordFactory constructor.
39
     *
40
     * @param Cache $cache
41
     */
42
    public function __construct(Cache $cache)
43
    {
44
        $this->cache = $cache;
45
    }
46
47
    /**
48
     * @param Tree $tree
49
     *
50
     * @return Collection<stdClass>
51
     */
52
    protected function pendingChanges(Tree $tree): Collection
53
    {
54
        return $this->cache->remember(__CLASS__ . $tree->id(), static function () use ($tree): Collection {
55
            return DB::table('change')
56
                ->where('gedcom_id', '=', $tree->id())
57
                ->where('status', '=', 'pending')
58
                ->orderBy('change_id')
59
                ->pluck('new_gedcom', 'xref');
60
        });
61
    }
62
63
    /**
64
     * We may have searched for X123, but found the record for x123.
65
     *
66
     * @param string $gedcom
67
     * @param string $xref
68
     *
69
     * @return mixed|string
70
     */
71
    protected function extractXref(string $gedcom, string $xref)
72
    {
73
        if (preg_match('/^0 @(' . Gedcom::REGEX_XREF . ')@/', $gedcom, $match)) {
74
            return $match[1];
75
        }
76
77
        return $xref;
78
    }
79
}
80