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

PendingChanges::handle()   C

Complexity

Conditions 12
Paths 22

Size

Total Lines 78
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 62
c 0
b 0
f 0
nc 22
nop 1
dl 0
loc 78
rs 6.4024

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\Carbon;
23
use Fisharebest\Webtrees\Family;
24
use Fisharebest\Webtrees\Gedcom;
25
use Fisharebest\Webtrees\GedcomRecord;
26
use Fisharebest\Webtrees\Header;
27
use Fisharebest\Webtrees\Http\ViewResponseTrait;
28
use Fisharebest\Webtrees\I18N;
29
use Fisharebest\Webtrees\Individual;
30
use Fisharebest\Webtrees\Media;
31
use Fisharebest\Webtrees\Note;
32
use Fisharebest\Webtrees\Repository;
33
use Fisharebest\Webtrees\Services\TreeService;
34
use Fisharebest\Webtrees\Source;
35
use Fisharebest\Webtrees\Submission;
36
use Fisharebest\Webtrees\Submitter;
37
use Fisharebest\Webtrees\Tree;
38
use Illuminate\Database\Capsule\Manager as DB;
39
use Psr\Http\Message\ResponseInterface;
40
use Psr\Http\Message\ServerRequestInterface;
41
use Psr\Http\Server\RequestHandlerInterface;
42
43
use function assert;
44
use function key;
45
use function preg_match;
46
use function reset;
47
use function route;
48
49
/**
50
 * Show all pending changes.
51
 */
52
class PendingChanges implements RequestHandlerInterface
53
{
54
    use ViewResponseTrait;
55
56
    /** @var TreeService */
57
    private $tree_service;
58
59
    /**
60
     * @param TreeService $tree_service
61
     */
62
    public function __construct(TreeService $tree_service)
63
    {
64
        $this->tree_service = $tree_service;
65
    }
66
67
    /**
68
     * @param ServerRequestInterface $request
69
     *
70
     * @return ResponseInterface
71
     */
72
    public function handle(ServerRequestInterface $request): ResponseInterface
73
    {
74
        $tree = $request->getAttribute('tree');
75
        assert($tree instanceof Tree);
76
77
        $url = $request->getQueryParams()['url'] ?? route(TreePage::class, ['tree' => $tree->name()]);
78
79
        $rows = DB::table('change')
80
            ->join('user', 'user.user_id', '=', 'change.user_id')
81
            ->join('gedcom', 'gedcom.gedcom_id', '=', 'change.gedcom_id')
82
            ->where('status', '=', 'pending')
83
            ->orderBy('change.gedcom_id')
84
            ->orderBy('change.xref')
85
            ->orderBy('change.change_id')
86
            ->select(['change.*', 'user.user_name', 'user.real_name', 'gedcom_name'])
87
            ->get();
88
89
        $changes = [];
90
        foreach ($rows as $row) {
91
            $row->change_time = Carbon::make($row->change_time);
92
93
            $change_tree = $this->tree_service->all()->get($row->gedcom_name);
94
95
            preg_match('/^0 (?:@' . Gedcom::REGEX_XREF . '@ )?(' . Gedcom::REGEX_TAG . ')/', $row->old_gedcom . $row->new_gedcom, $match);
96
97
            switch ($match[1]) {
98
                case Individual::RECORD_TYPE:
99
                    $row->record = new Individual($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
100
                    break;
101
                case Family::RECORD_TYPE:
102
                    $row->record = new Family($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
103
                    break;
104
                case Source::RECORD_TYPE:
105
                    $row->record = new Source($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
106
                    break;
107
                case Repository::RECORD_TYPE:
108
                    $row->record = new Repository($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
109
                    break;
110
                case Media::RECORD_TYPE:
111
                    $row->record = new Media($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
112
                    break;
113
                case Note::RECORD_TYPE:
114
                    $row->record = new Note($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
115
                    break;
116
                case Submitter::RECORD_TYPE:
117
                    $row->record = new Submitter($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
118
                    break;
119
                case Submission::RECORD_TYPE:
120
                    $row->record = new Submission($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
121
                    break;
122
                case Header::RECORD_TYPE:
123
                    $row->record = new Header($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
124
                    break;
125
                default:
126
                    $row->record = new GedcomRecord($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
127
                    break;
128
            }
129
130
            $changes[$row->gedcom_name][$row->xref][] = $row;
131
        }
132
133
        $title = I18N::translate('Pending changes');
134
135
        // If the current tree has changes, activate that tab.  Otherwise activate the first tab.
136
        if (($changes[$tree->id()] ?? []) === []) {
137
            reset($changes);
138
            $active_tree_name = key($changes);
139
        } else {
140
            $active_tree_name = $tree->name();
141
        }
142
143
        return $this->viewResponse('pending-changes-page', [
144
            'active_tree_name' => $active_tree_name,
145
            'changes'          => $changes,
146
            'title'            => $title,
147
            'tree'             => $tree,
148
            'trees'            => $this->tree_service->all(),
149
            'url'              => $url,
150
        ]);
151
    }
152
}
153