1
|
|
|
<?php |
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
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
15
|
|
|
|
16
|
|
|
class SphinxService |
17
|
|
|
{ |
18
|
|
|
protected static $_instance; |
19
|
|
|
|
20
|
|
|
protected $client; |
21
|
|
|
|
22
|
|
|
protected $parameters; |
23
|
|
|
|
24
|
|
|
public function setParameters(Array $customParameters = []) |
25
|
|
|
{ |
26
|
|
|
$defaultParameters = [ |
27
|
|
|
'host' => 'localhost', |
28
|
|
|
'port' => '9313', |
29
|
|
|
'timeout' => 5, |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
$array = array_merge($defaultParameters, $customParameters); |
33
|
|
|
|
34
|
|
|
$this->parameters = new ArrayCollection($array); |
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getParameters() |
40
|
|
|
{ |
41
|
|
|
if (!$this->parameters) { |
42
|
|
|
$this->setParameters(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this->parameters; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Factory e Configuracao padrao de um SphinxClient. |
50
|
|
|
* |
51
|
|
|
* @return SphinxClient |
52
|
|
|
*/ |
53
|
|
|
public function createService() |
54
|
|
|
{ |
55
|
|
|
$host = $this->getParameters()->get('host'); |
56
|
|
|
$port = $this->getParameters()->get('port'); |
57
|
|
|
$timeout = $this->getParameters()->get('timeout'); |
58
|
|
|
|
59
|
|
|
if (is_null($host) || is_null($port)) { |
60
|
|
|
throw new \Exception( |
61
|
|
|
'No sphinx server information found within the configuration!' |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$sphinxClient = new SphinxClient(); |
66
|
|
|
$sphinxClient->SetServer($host, $port); |
67
|
|
|
$sphinxClient->SetConnectTimeout($timeout); |
68
|
|
|
$sphinxClient->SetArrayResult(true); |
69
|
|
|
$sphinxClient->setMatchModeByModeName('any'); |
70
|
|
|
$sphinxClient->SetSortMode(SPH_SORT_RELEVANCE); |
71
|
|
|
$sphinxClient->SetRankingMode(SPH_RANK_PROXIMITY); |
72
|
|
|
|
73
|
|
|
return $sphinxClient; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Acesso ao Cliente Sphinx Server. |
78
|
|
|
* |
79
|
|
|
* @return Gpupo\Search\Sphinx\SphinxClient |
80
|
|
|
*/ |
81
|
|
|
public function getClient() |
82
|
|
|
{ |
83
|
|
|
if (!$this->client) { |
84
|
|
|
$this->client = $this->createService(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->client; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Semelhante a *getClient()* mas com reset. |
92
|
|
|
* |
93
|
|
|
* @return Gpupo\Search\Sphinx\SphinxClient |
94
|
|
|
*/ |
95
|
|
|
public function getFreshClient() |
96
|
|
|
{ |
97
|
|
|
return $this->reset()->getClient(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Limpa informações de pesquisas anteriores. |
102
|
|
|
*/ |
103
|
|
|
public function reset() |
104
|
|
|
{ |
105
|
|
|
$this->getClient()->ResetFilters(); |
106
|
|
|
$this->getClient()->ResetGroupBy(); |
107
|
|
|
$this->getClient()->ResetOverrides(); |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
View Code Duplication |
public static function getInstance() |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
if (!isset(self::$_instance)) { |
115
|
|
|
$class = get_called_class(); |
116
|
|
|
self::$_instance = new $class(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return self::$_instance; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.