ShopSearchControllerExtension   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 19
Bugs 2 Features 9
Metric Value
wmc 8
c 19
b 2
f 9
lcom 1
cbo 8
dl 0
loc 84
ccs 0
cts 40
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A SearchForm() 0 4 1
B search_suggest() 0 27 3
A onAfterInit() 0 23 3
A generateLongTitle() 0 3 1
1
<?php
2
/**
3
 * Adds SearchForm and results methods to the controller.
4
 *
5
 * @author Mark Guinn <[email protected]>
6
 * @date 08.29.2013
7
 * @package shop_search
8
 */
9
class ShopSearchControllerExtension extends Extension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
    private static $allowed_actions = array('SearchForm', 'results', 'search_suggest');
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
13
    /**
14
     * @return ShopSearchForm
15
     */
16
    public function SearchForm()
17
    {
18
        return ShopSearchForm::create($this->owner, 'SearchForm', $this->owner->Link() . 'search-suggest');
19
    }
20
21
22
    /**
23
     * @param SS_HTTPRequest $req
24
     * @return string
25
     */
26
    public function search_suggest(SS_HTTPRequest $req)
27
    {
28
        /** @var SS_HTTPResponse $response */
29
        $response    = $this->owner->getResponse();
30
        $callback    = $req->requestVar('callback');
31
32
        // convert the search results into usable json for search-as-you-type
33
        if (ShopSearch::config()->search_as_you_type_enabled) {
34
            $searchVars = $req->requestVars();
35
            $searchVars[ ShopSearch::config()->qs_query ] = $searchVars['term'];
36
            unset($searchVars['term']);
37
            $results = ShopSearch::inst()->suggestWithResults($searchVars);
38
        } else {
39
            $results = array(
40
                'suggestions'   => ShopSearch::inst()->suggest($req->requestVar('term')),
41
            );
42
        }
43
44
        if ($callback) {
45
            $response->addHeader('Content-type', 'application/javascript');
46
            $response->setBody($callback . '(' . json_encode($results) . ');');
47
        } else {
48
            $response->addHeader('Content-type', 'application/json');
49
            $response->setBody(json_encode($results));
50
        }
51
        return $response;
52
    }
53
54
55
    /**
56
     * If there is a search encoded in the link, go ahead and log it.
57
     * This happens when you click through on a search suggestion
58
     */
59
    public function onAfterInit()
60
    {
61
        $req = $this->owner->getRequest();
62
        $src = $req->requestVar(Config::inst()->get('ShopSearch', 'qs_source'));
63
        if ($src) {
64
            $qs_q   = Config::inst()->get('ShopSearch', 'qs_query');
65
            $qs_f   = Config::inst()->get('ShopSearch', 'qs_filters');
66
            $vars   = json_decode(base64_decode($src), true);
67
68
            // log the search
69
            $log = SearchLog::create(array(
70
                'Query'         => strtolower($vars[$qs_q]),
71
                'Link'          => $req->getURL(false), // These searches will never have child searches, but this will allow us to know what they clicked
72
                'NumResults'    => $vars['total'],
73
                'MemberID'      => Member::currentUserID(),
74
                'Filters'       => !empty($vars[$qs_f]) ? json_encode($vars[$qs_f]) : null,
75
            ));
76
            $log->write();
77
78
            // redirect to the clean page
79
            $this->owner->redirect($req->getURL(false));
80
        }
81
    }
82
83
84
    /**
85
     * @param ArrayData $results
86
     * @param array     $data
87
     * @return string
88
     */
89
    protected function generateLongTitle(ArrayData $results, array $data)
0 ignored issues
show
Unused Code introduced by
The parameter $results is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
    {
91
    }
92
}
93