ShopSearchMysql   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 13
c 4
b 0
f 1
lcom 0
cbo 9
dl 0
loc 80
ccs 0
cts 39
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B searchFromVars() 0 40 5
B getSearchFields() 0 21 8
1
<?php
2
/**
3
 * Adapter that will use MySQL's full text search features.
4
 *
5
 * @author Mark Guinn <[email protected]>
6
 * @date 11.13.2013
7
 * @package shop_search
8
 * @subpackage adapters
9
 */
10
class ShopSearchMysql extends Object implements ShopSearchAdapter
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...
11
{
12
    /**
13
     * @param string $keywords
14
     * @param array $filters [optional]
15
     * @param array $facetSpec [optional]
16
     * @param int $start [optional]
17
     * @param int $limit [optional]
18
     * @param string $sort [optional]
19
     * @return ArrayData
20
     */
21
    public function searchFromVars($keywords, array $filters=array(), array $facetSpec=array(), $start=-1, $limit=-1, $sort='')
22
    {
23
        $searchable = ShopSearch::get_searchable_classes();
24
        $matches = new ArrayList;
25
26
        foreach ($searchable as $className) {
27
            $list = DataObject::get($className);
28
29
            // get searchable fields
30
            $keywordFields = $this->getSearchFields($className);
31
32
            // build the filter
33
            $filter = array();
34
35
            // Use parametrized query if SilverStripe >= 3.2
36
            if (SHOP_SEARCH_IS_SS32) {
37
                foreach ($keywordFields as $indexFields) {
38
                    $filter[] = array("MATCH ($indexFields) AGAINST (?)" => $keywords);
39
                }
40
                $list = $list->whereAny($filter);
41
            } else {
42
                foreach ($keywordFields as $indexFields) {
43
                    $filter[] = sprintf("MATCH ($indexFields) AGAINST ('%s')", Convert::raw2sql($keywords));
44
                }
45
                // join all the filters with an "OR" statement
46
                $list = $list->where(implode(' OR ', $filter));
47
            }
48
49
            // add in any other filters
50
            $list = FacetHelper::inst()->addFiltersToDataList($list, $filters);
51
52
            // add any matches to the big list
53
            $matches->merge($list);
54
        }
55
56
        return new ArrayData(array(
57
            'Matches'   => $matches,
58
            'Facets'    => FacetHelper::inst()->buildFacets($matches, $facetSpec, (bool)Config::inst()->get('ShopSearch', 'auto_facet_attributes')),
59
        ));
60
    }
61
62
63
    /**
64
     * @param $className
65
     * @return array an array containing fields per index
66
     * @throws Exception
67
     */
68
    protected function getSearchFields($className)
69
    {
70
        $indexes = Config::inst()->get($className, 'indexes');
71
72
        $indexList = array();
73
        foreach ($indexes as $name => $index) {
0 ignored issues
show
Bug introduced by
The expression $indexes of type array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
74
            if (is_array($index)) {
75
                if (!empty($index['type']) && $index['type'] == 'fulltext' && !empty($index['value'])) {
76
                    $indexList[] = trim($index['value']);
77
                }
78
            } elseif (preg_match('/fulltext\((.+)\)/', $index, $m)) {
79
                $indexList[] = trim($m[1]);
80
            }
81
        }
82
83
        if (count($indexList) === 0) {
84
            throw new Exception("Class $className does not appear to have any fulltext indexes");
85
        }
86
87
        return $indexList;
88
    }
89
}
90