Passed
Push — master ( 06fe1e...3959ee )
by Greg
07:18
created

AbstractSelect2Handler::__construct()   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) 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\Services\SearchService;
23
use Fisharebest\Webtrees\Tree;
24
use Illuminate\Support\Collection;
25
use Psr\Http\Message\ResponseInterface;
26
use Psr\Http\Message\ServerRequestInterface;
27
use Psr\Http\Server\RequestHandlerInterface;
28
29
use function assert;
30
use function response;
31
32
/**
33
 * Autocomplete for Select2 based controls.
34
 */
35
abstract class AbstractSelect2Handler implements RequestHandlerInterface
36
{
37
    // For clients that request one page of data at a time.
38
    private const RESULTS_PER_PAGE = 20;
39
40
    // Minimum number of characters for a search.
41
    public const MINIMUM_INPUT_LENGTH = 2;
42
43
    // Wait for the user to pause typing before sending request.
44
    public const AJAX_DELAY = 350;
45
46
    /**
47
     * @param ServerRequestInterface $request
48
     *
49
     * @return ResponseInterface
50
     */
51
    public function handle(ServerRequestInterface $request): ResponseInterface
52
    {
53
        $tree = $request->getAttribute('tree');
54
        assert($tree instanceof Tree);
55
56
        $params = (array) $request->getParsedBody();
57
58
        $query = $params['q'] ?? '';
59
        assert(strlen($query) >= self::MINIMUM_INPUT_LENGTH);
60
61
        $page = (int) ($params['page'] ?? 1);
62
63
        // Fetch one more row than we need, so we can know if more rows exist.
64
        $offset = ($page - 1) * self::RESULTS_PER_PAGE;
65
        $limit  = self::RESULTS_PER_PAGE + 1;
66
67
        // Perform the search.
68
        $results = $this->search($tree, $query, $offset, $limit);
69
70
        return response([
71
            'results'    => $results->slice(0, self::RESULTS_PER_PAGE)->all(),
72
            'pagination' => [
73
                'more' => $results->count() > self::RESULTS_PER_PAGE,
74
            ],
75
        ]);
76
    }
77
78
    /**
79
     * Perform the search
80
     *
81
     * @param Tree   $tree
82
     * @param string $query
83
     * @param int    $offset
84
     * @param int    $limit
85
     *
86
     * @return Collection<array<string,string>>
87
     */
88
    abstract protected function search(Tree $tree, string $query, int $offset, int $limit): Collection;
89
}
90