ActionSearch   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 26
dl 0
loc 48
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A search() 0 41 4
1
<?php
2
3
namespace Apps\Controller\Front\Profile;
4
5
use Apps\ActiveRecord\Profile as ProfileRecords;
6
use Apps\Model\Front\Profile\FormUserSearch;
7
use Ffcms\Core\Arch\View;
8
use Ffcms\Core\Network\Request;
9
use Ffcms\Core\Network\Response;
10
11
/**
12
 * Trait ActionSearch
13
 * @package Apps\Controller\Front\Profile
14
 * @property View $view
15
 * @property Request $request
16
 * @property Response $response
17
 * @method array getConfigs()
18
 */
19
trait ActionSearch
20
{
21
    /**
22
     * Search users
23
     * @return string
24
     * @throws \Ffcms\Core\Exception\SyntaxException
25
     */
26
    public function search(): ?string
27
    {
28
        // create model object
29
        $model = new FormUserSearch();
30
        $model->setSubmitMethod('get');
31
32
        // get app configs
33
        $cfgs = $this->getConfigs();
34
35
        $records = null;
36
        $pagination = null;
37
        // check if request is sended
38
        if ($model->send() && $model->validate()) {
39
            // get records from db
40
            $records = ProfileRecords::where('nick', 'like', '%' . $model->query . '%');
41
            $page = (int)$this->request->query->get('page');
42
            $userPerPage = (int)$cfgs['usersOnPage'];
43
            if ($userPerPage < 1) {
44
                $userPerPage = 1;
45
            }
46
47
            $offset = $page * $userPerPage;
48
            // build pagination
49
            $pagination = [
50
                'url' => ['profile/search', null, [$model->getFormName().'[query]' => $model->query, $model->getFormName().'[submit]' => true]],
51
                'page' => $page,
52
                'step' => $userPerPage,
53
                'total' => $records->count()
54
            ];
55
            // make query finally
56
            $records = $records->skip($offset)
57
                ->take($userPerPage)
58
                ->get();
59
        }
60
61
        // display response
62
        return $this->view->render('profile/search', [
63
            'model' => $model,
64
            'records' => $records,
65
            'pagination' => $pagination,
66
            'ratingOn' => (bool)$cfgs['rating']
67
        ]);
68
    }
69
}
70