Search::doSearch()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
/**
4
 * @package Autocomplete search
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2017, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\autocomplete_search\controllers;
11
12
use gplcart\core\controllers\frontend\Controller;
13
use gplcart\core\models\Search as SearchModel;
14
15
/**
16
 * Handles incoming requests and outputs data related to Autocomplete search module
17
 */
18
class Search extends Controller
19
{
20
21
    /**
22
     * Search model instance
23
     * @var \gplcart\core\models\Search $search
24
     */
25
    protected $search;
26
27
    /**
28
     * @param SearchModel $search
29
     */
30
    public function __construct(SearchModel $search)
31
    {
32
        parent::__construct();
33
34
        $this->search = $search;
35
    }
36
37
    /**
38
     * Route page callback
39
     * Output JSON for autocomplete suggestions
40
     */
41
    public function doSearch()
42
    {
43
        if ($this->isPosted('term')) {
44
            $this->outputJson($this->getProductsSearch());
45
        }
46
    }
47
48
    /**
49
     * Returns an array of found products
50
     * @return array
51
     */
52
    protected function getProductsSearch()
53
    {
54
        $term = $this->getPosted('term', '', true, 'string');
55
56
        $entity_options = array(
57
            'entity' => 'product',
58
            'template_item' => 'autocomplete_search|suggestion'
59
        );
60
61
        $search_options = array(
62
            'status' => true,
63
            'store_id' => $this->store_id,
64
            'limit' => array(0, $this->module->getSettings('autocomplete_search', 'max_result')));
65
66
        $products = $this->search->search('product', $term, $search_options);
67
        $this->prepareEntityItems($products, $entity_options);
68
69
        return $products;
70
    }
71
72
}
73