SphinxService   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 106
Duplicated Lines 8.49 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 2
dl 9
loc 106
c 0
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setParameters() 0 14 1
A getParameters() 0 8 2
A createService() 0 22 3
A getClient() 0 8 2
A getFreshClient() 0 4 1
A reset() 0 8 1
A getInstance() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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