Search::actionIndex()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 31
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 3
nop 0
dl 0
loc 31
rs 9.5222
c 0
b 0
f 0
1
<?php
2
3
namespace Apps\Controller\Front;
4
5
use Apps\Model\Front\Search\EntitySearchMain;
6
use Extend\Core\Arch\FrontAppController as Controller;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Exception\NotFoundException;
9
use Ffcms\Core\Helper\Type\Any;
10
use Ffcms\Core\Helper\Type\Str;
11
12
/**
13
 * Class Search. Search app front controller
14
 * @package Apps\Controller\Front
15
 */
16
class Search extends Controller
17
{
18
    const EVENT_SEARCH_RUN = 'search.run';
19
    const QUERY_MAX_LENGTH = 100;
20
21
    /**
22
     * Main search method
23
     * @return string
24
     * @throws NotFoundException
25
     */
26
    public function actionIndex()
27
    {
28
        // get search query value from GET headers
29
        $query = (string)$this->request->query->get('query', null);
30
        // strip html tags
31
        $query = App::$Security->strip_tags(trim($query));
32
        // get configs
33
        $configs = $this->getConfigs();
34
35
        // check search query length
36
        if (!Any::isStr($query) || Str::likeEmpty($query) || Str::length($query) < (int)$configs['minLength']) {
37
            throw new NotFoundException(__('Search query is too short!'));
38
        }
39
40
        // prevent sh@t query's with big length
41
        if (Str::length($query) > static::QUERY_MAX_LENGTH) {
42
            throw new NotFoundException(__('Search query is too long!'));
43
        }
44
45
        // initialize search controller model
46
        $model = new EntitySearchMain($query, $configs);
47
48
        // register search event to allow extend it model results
49
        App::$Event->run(static::EVENT_SEARCH_RUN, [
50
            'model' => $model
51
        ]);
52
53
        // render output view with search result
54
        return $this->view->render('search/index', [
55
            'model' => $model,
56
            'query' => $query
57
        ]);
58
    }
59
}
60