SphinxClient   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 105
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A SetMatchMode() 0 4 1
A setMatchModeByModeName() 0 18 2
A setSortExtended() 0 4 1
A setGroupByAttr() 0 4 1
B addFacetedQuery() 0 26 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 25 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/*
4
 * This file is part of gpupo/search
5
 *
6
 * (c) Gilmar Pupo <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gpupo\Search\Sphinx;
13
14
assert_options(ASSERT_ACTIVE, 1);
15
assert_options(ASSERT_WARNING, 0);
16
assert_options(ASSERT_QUIET_EVAL, 1);
17
18
include dirname(__FILE__).'/sphinxapi.php';
19
20
/**
21
 * Acesso a API PHP Oficial do Sphinx.
22
 *
23
 * @see https://code.google.com/p/sphinxsearch/
24
 */
25
class SphinxClient extends \SphinxClient
26
{
27
    /**
28
     * {@inheritdoc }.
29
     *
30
     * SPH_MATCH_ALL, matches all query words (<b>default mode</b>);
31
     * SPH_MATCH_ANY, matches any of the query words;
32
     * SPH_MATCH_PHRASE, matches query as a phrase, requiring perfect match;
33
     * SPH_MATCH_BOOLEAN, matches query as a boolean expression;
34
     * SPH_MATCH_EXTENDED, matches query as an expression in Sphinx internal query language;
35
     * SPH_MATCH_FULLSCAN, matches query, forcibly using the "full scan" mode as below.
36
     *
37
     * @see http://sphinxsearch.com/docs/current.html#matching-modes Matching modes
38
     */
39
    public function SetMatchMode($mode)
40
    {
41
        return parent::SetMatchMode($mode);
42
    }
43
44
    /**
45
     * Uso amigavel da definicao de Match Mode.
46
     *
47
     * @param string $modeName all|any|phrase|boolean
48
     */
49
    public function setMatchModeByModeName($modeName)
50
    {
51
        $modes = [
52
            'all'       => 0,
53
            'any'       => 1,
54
            'phrase'    => 2,
55
            'boolean'   => 3,
56
            'extended'  => 4,
57
            'fullscan'  => 5,
58
        ];
59
60
        if (array_key_exists($modeName, $modes)) {
61
            $mode = $modes[$modeName];
62
            $this->SetMatchMode($mode);
63
        } else {
64
            throw new \LogicException('Wrong Mode');
65
        }
66
    }
67
68
    /**
69
     * Define a ordem de resultados.
70
     *
71
     * @param string $string
72
     */
73
    public function setSortExtended($string)
74
    {
75
        $this->SetSortMode(SPH_SORT_EXTENDED, $string);
76
    }
77
78
    /**
79
     * Agrupa resultados por atributo.
80
     *
81
     * @param string $attr
82
     */
83
    public function setGroupByAttr($attr)
84
    {
85
        $this->SetGroupBy($attr, SPH_GROUPBY_ATTR);
86
    }
87
88
    /**
89
     * Adiciona multiplas queries a partir de uma query
90
     * matriz e chaves usadas na busca facetada.
91
     *
92
     * Multi-queries are just a mechanism that lets you send several search
93
     * queries to searchd in one batch. That, in turn, lets searchd internally
94
     * optimize common parts between the queries. And that’s where the savings
95
     * come from.
96
     *
97
     * @see http://sphinxsearch.com/blog/2010/04/05/facets-multi-queries-and-searching-3x-faster/
98
     *
99
     * @param string $query
100
     * @param string $index
101
     * @param array  $keys
102
     */
103
    public function addFacetedQuery($query, $index, array $keys)
104
    {
105
        $this->AddQuery($query, $index);
106
107
        //Clear Offset
108
        $currentOffset = $this->_offset;
109
        $mode = $this->_sort;
110
        $sortby = $this->_sortby;
111
        $limit = $this->_limit;
112
113
        $this->_offset = 0;
114
        $this->_sort = 0;
115
        $this->_sortby = '';
116
        $this->SetLimits(0, 999);
117
118
        foreach ($keys as $key) {
119
            $this->setGroupByAttr($key);
120
            $this->AddQuery($query, $index);
121
        }
122
123
        //Reset
124
        $this->_offset = $currentOffset;
125
        $this->_sort = $mode;
126
        $this->_sortby = $sortby;
127
        $this->SetLimits($currentOffset, $limit);
128
    }
129
}
130