Search   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 96
Duplicated Lines 9.38 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 9
loc 96
c 0
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 9 9 2
B findByQuery() 0 25 2
A searchByKeyword() 0 19 1
A getResultsByKeyword() 0 10 2
A factoryCollection() 0 6 1
A getSphinxClient() 0 4 1
A getSphinxService() 0 4 1

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;
13
14
use Gpupo\Search\Paginator\Paginator;
15
use Gpupo\Search\Query\QueryInterface;
16
use Gpupo\Search\Result\Collection;
17
use Gpupo\Search\Result\CollectionInterface;
18
use Gpupo\Search\Sphinx\SphinxService;
19
20
class Search  extends SearchAbstract implements SearchInterface
0 ignored issues
show
Coding Style introduced by
Expected 1 space after class name; 2 found
Loading history...
Coding Style introduced by
Expected 1 space before extends keyword; 2 found
Loading history...
21
{
22
    protected static $_instance;
23
24 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...
25
    {
26
        if (!isset(self::$_instance)) {
27
            $class = get_called_class();
28
            self::$_instance = new $class();
29
        }
30
31
        return self::$_instance;
32
    }
33
34
    public function findByQuery(QueryInterface $query)
35
    {
36
        $collection = $this->getCollection(
37
            $query->getIndex(),
38
            $query->getFilters(),
39
            $query->getQueries(),
40
            $query->getFieldWeights(),
41
            $query->getLimit(),
42
            $query->getOffSet(),
43
            $query->getCountableAttributes()
44
        );
45
46
        $paginator = new Paginator();
47
48
        if ($query->getPaginator()) {
49
            $page = $query->getPaginator()->getCurrentPageNumber();
50
        } else {
51
            $page = 1;
52
        }
53
54
        $paginator->paginateResult($collection, $page);
55
        $collection->setPaginator($paginator);
56
57
        return $collection;
58
    }
59
60
    public function searchByKeyword($keyword)
61
    {
62
        $results = $this->search(
63
               'produtoIndex',
64
               null,
65
               [[
66
                   'key'     => '*',
67
                    'values' => [
68
                       $keyword,
69
                    ],
70
                   'strict' => false,
71
               ]],
72
               null,
73
               20,
74
               0
75
           );
76
77
        return $results;
78
    }
79
80
    public function getResultsByKeyword($keyword, CollectionInterface $collection)
81
    {
82
        $results = $this->searchByKeyword($keyword);
83
84
        if ($results) {
85
            $collection->load($results);
86
87
            return $collection;
88
        }
89
    }
90
91
    /**
92
     * @return CollectionInterface
93
     */
94
    public function factoryCollection(array $array)
95
    {
96
        $collection =  new Collection($array);
97
98
        return $collection;
99
    }
100
101
    /**
102
     * Acesso ao Client Sphinx Search.
103
     *
104
     * @return \SphinxClient
105
     */
106
    public function getSphinxClient()
107
    {
108
        return $this->getSphinxService()->getFreshClient();
109
    }
110
111
    public function getSphinxService()
112
    {
113
        return SphinxService::getInstance();
114
    }
115
}
116