Passed
Push — master ( 00a79d...f5402f )
by Greg
05:33
created

SearchPhoneticPage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
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 2
dl 0
loc 4
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\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\Http\ViewResponseTrait;
23
use Fisharebest\Webtrees\I18N;
24
use Fisharebest\Webtrees\Services\SearchService;
25
use Fisharebest\Webtrees\Services\TreeService;
26
use Fisharebest\Webtrees\Site;
27
use Fisharebest\Webtrees\Tree;
28
use InvalidArgumentException;
29
use Psr\Http\Message\ResponseInterface;
30
use Psr\Http\Message\ServerRequestInterface;
31
use Psr\Http\Server\RequestHandlerInterface;
32
33
use function array_filter;
34
use function assert;
35
use function in_array;
36
37
/**
38
 * Search for (and optionally replace) genealogy data
39
 */
40
class SearchPhoneticPage implements RequestHandlerInterface
41
{
42
    use ViewResponseTrait;
43
44
    /** @var SearchService */
45
    private $search_service;
46
47
    /** @var TreeService */
48
    private $tree_service;
49
50
    /**
51
     * SearchController constructor.
52
     *
53
     * @param SearchService $search_service
54
     * @param TreeService   $tree_service
55
     */
56
    public function __construct(SearchService $search_service, TreeService $tree_service)
57
    {
58
        $this->search_service = $search_service;
59
        $this->tree_service   = $tree_service;
60
    }
61
62
    /**
63
     * The phonetic search.
64
     *
65
     * @param ServerRequestInterface $request
66
     *
67
     * @return ResponseInterface
68
     */
69
    public function handle(ServerRequestInterface $request): ResponseInterface
70
    {
71
        $tree = $request->getAttribute('tree');
72
        assert($tree instanceof Tree, new InvalidArgumentException());
73
74
        $params    = $request->getQueryParams();
75
        $firstname = $params['firstname'] ?? '';
76
        $lastname  = $params['lastname'] ?? '';
77
        $place     = $params['place'] ?? '';
78
        $soundex   = $params['soundex'] ?? 'Russell';
79
80
        // What trees to seach?
81
        if (Site::getPreference('ALLOW_CHANGE_GEDCOM') === '1') {
82
            $all_trees = $this->tree_service->all()->all();
83
        } else {
84
            $all_trees = [$tree];
85
        }
86
87
        $search_tree_names = $params['search_trees'] ?? [];
88
89
        $search_trees = array_filter($all_trees, static function (Tree $tree) use ($search_tree_names): bool {
90
            return in_array($tree->name(), $search_tree_names, true);
91
        });
92
93
        if ($search_trees === []) {
94
            $search_trees = [$tree];
95
        }
96
97
        $individuals = [];
98
99
        if ($lastname !== '' || $firstname !== '' || $place !== '') {
100
            $individuals = $this->search_service->searchIndividualsPhonetic($soundex, $lastname, $firstname, $place, $search_trees);
101
        }
102
103
        $title = I18N::translate('Phonetic search');
104
105
        return $this->viewResponse('search-phonetic-page', [
106
            'all_trees'    => $all_trees,
107
            'firstname'    => $firstname,
108
            'individuals'  => $individuals,
109
            'lastname'     => $lastname,
110
            'place'        => $place,
111
            'search_trees' => $search_trees,
112
            'soundex'      => $soundex,
113
            'title'        => $title,
114
            'tree'         => $tree,
115
        ]);
116
    }
117
}
118