Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

src/Kunstmaan/SearchBundle/Search/Search.php (7 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\SearchBundle\Search;
4
5
use Kunstmaan\SearchBundle\Provider\SearchProviderChainInterface;
6
use Kunstmaan\SearchBundle\Provider\SearchProviderInterface;
7
8
/**
9
 * Search class which will delegate to the active SearchProvider
10
 * The active SearchProvider can be overridden by overriding the "kunstmaan_search.search" parameter
11
 */
12
class Search implements SearchProviderInterface
13
{
14
    /**
15
     * @var SearchProviderChainInterface
16
     */
17
    private $searchProviderChain;
18
19
    /**
20
     * @var string
21
     */
22
    private $indexNamePrefix;
23
24
    /**
25
     * @var string
26
     */
27
    private $activeProvider;
28
29
    /**
30
     * @param SearchProviderChainInterface $searchProviderChain
31
     * @param string                       $indexNamePrefix
32
     * @param string                       $activeProvider
33
     */
34
    public function __construct(SearchProviderChainInterface $searchProviderChain, $indexNamePrefix, $activeProvider)
35
    {
36
        $this->searchProviderChain = $searchProviderChain;
37
        $this->indexNamePrefix = $indexNamePrefix;
38
        $this->activeProvider = $activeProvider;
39
    }
40
41
    /**
42
     * Get the current active SearchProvider
43
     *
44
     * @return SearchProviderInterface
45
     */
46
    public function getActiveProvider()
47
    {
48
        return $this->searchProviderChain->getProvider($this->activeProvider);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function createIndex($indexName)
55
    {
56
        return $this->getActiveProvider()->createIndex($this->indexNamePrefix . $indexName);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getIndex($indexName)
63
    {
64
        return $this->getActiveProvider()->getIndex($this->indexNamePrefix . $indexName);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getClient()
71
    {
72
        return $this->getActiveProvider()->getClient();
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 View Code Duplication
    public function createDocument($uid, $document, $indexName = '', $indexType = '')
0 ignored issues
show
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...
79
    {
80
        $indexName = strtolower($indexName);
81
82
        return $this->getActiveProvider()->createDocument(
83
            $uid,
84
            $document,
85
            $this->indexNamePrefix . $indexName,
86
            $indexType
87
        );
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 View Code Duplication
    public function addDocument($uid, $document, $indexType, $indexName)
0 ignored issues
show
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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...
94
    {
95
        $indexName = strtolower($indexName);
96
97
        return $this->getActiveProvider()->addDocument(
98
            $this->indexNamePrefix . $indexName,
99
            $indexType,
0 ignored issues
show
$indexType is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
100
            $document,
0 ignored issues
show
$document is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
101
            $uid
102
        );
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function addDocuments($documents, $indexName = '', $indexType = '')
109
    {
110
        $indexName = strtolower($indexName);
111
112
        return $this->getActiveProvider()->addDocuments($documents, $indexName, $indexType);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function deleteDocument($indexName, $indexType, $uid)
119
    {
120
        $indexName = strtolower($indexName);
121
122
        return $this->getActiveProvider()->deleteDocument($this->indexNamePrefix . $indexName, $indexType, $uid);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function deleteDocuments($indexName, $indexType, array $ids)
0 ignored issues
show
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
129
    {
130
        $indexName = strtolower($indexName);
131
132
        return $this->getActiveProvider()->deleteDocuments($this->indexNamePrefix . $indexName, $indexType, $ids);
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function deleteIndex($indexName)
0 ignored issues
show
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
139
    {
140
        $indexName = strtolower($indexName);
141
142
        return $this->getActiveProvider()->deleteIndex($this->indexNamePrefix . $indexName);
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function getName()
149
    {
150
        return 'Search';
151
    }
152
153
    /**
154
     * Get the prefix for the index' name
155
     *
156
     * @return string
157
     */
158
    public function getIndexNamePrefix()
159
    {
160
        return $this->indexNamePrefix;
161
    }
162
}
163