Passed
Push — master ( 60dbc8...203b6c )
by Greg
11:35
created

AbstractSelect2Handler::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 21
rs 9.8666
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
    /** @var SearchService */
47
    protected $search_service;
48
49
    /**
50
     * AutocompleteController constructor.
51
     *
52
     * @param SearchService $search_service
53
     */
54
    public function __construct(SearchService $search_service)
55
    {
56
        $this->search_service = $search_service;
57
    }
58
59
    /**
60
     * @param ServerRequestInterface $request
61
     *
62
     * @return ResponseInterface
63
     */
64
    public function handle(ServerRequestInterface $request): ResponseInterface
65
    {
66
        $tree = $request->getAttribute('tree');
67
        assert($tree instanceof Tree);
68
69
        $query = $request->getParsedBody()['q'] ?? '';
70
        assert(strlen($query) >= self::MINIMUM_INPUT_LENGTH);
71
72
        $page = (int) ($request->getParsedBody()['page'] ?? 1);
73
74
        // Fetch one more row than we need, so we can know if more rows exist.
75
        $offset = ($page - 1) * self::RESULTS_PER_PAGE;
76
        $limit  = self::RESULTS_PER_PAGE + 1;
77
78
        // Perform the search.
79
        $results = $this->search($tree, $query, $offset, $limit);
80
81
        return response([
82
            'results'    => $results->slice(0, self::RESULTS_PER_PAGE)->all(),
83
            'pagination' => [
84
                'more' => $results->count() > self::RESULTS_PER_PAGE,
85
            ],
86
        ]);
87
    }
88
89
    /**
90
     * Perform the search
91
     *
92
     * @param Tree   $tree
93
     * @param string $query
94
     * @param int    $offset
95
     * @param int    $limit
96
     *
97
     * @return Collection
98
     */
99
    abstract protected function search(Tree $tree, string $query, int $offset, int $limit): Collection;
100
}
101