AlgoliaSearchAdapter   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

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

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A search() 0 12 1
A addDocument() 0 7 1
A addDocuments() 0 17 2
A removeDocument() 0 7 1
A updateDocument() 0 9 1
A getIndexName() 0 4 1
A createIndex() 0 3 1
A removeIndex() 0 6 1
A flushIndex() 0 7 1
A getStats() 0 4 1
A createQueryBuilder() 0 6 1
A createDocumentBody() 0 10 1
A getClient() 0 8 2
A processResults() 0 10 2
A getOption() 0 4 1
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 * 
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 * 
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Component\Search\Adapter\Algolia;
14
15
use AlgoliaSearch\Client;
16
use Doctrine\Common\Collections\Collection;
17
use WellCommerce\Component\Search\Adapter\AdapterInterface;
18
use WellCommerce\Component\Search\Adapter\QueryBuilderInterface;
19
use WellCommerce\Component\Search\Adapter\SearchAdapterConfiguratorInterface;
20
use WellCommerce\Component\Search\Model\DocumentInterface;
21
use WellCommerce\Component\Search\Model\FieldInterface;
22
use WellCommerce\Component\Search\Request\SearchRequestInterface;
23
24
/**
25
 * Class AlgoliaSearchAdapter
26
 *
27
 * @author  Adam Piotrowski <[email protected]>
28
 */
29
final class AlgoliaSearchAdapter implements AdapterInterface
30
{
31
    /**
32
     * @var SearchAdapterConfiguratorInterface
33
     */
34
    private $configurator;
35
    
36
    /**
37
     * @var Client
38
     */
39
    private $client;
40
    
41
    public function __construct(SearchAdapterConfiguratorInterface $configurator)
42
    {
43
        $this->configurator = $configurator;
44
    }
45
    
46
    public function search(SearchRequestInterface $request): array
47
    {
48
        $indexName = $this->getIndexName($request->getLocale(), $request->getType()->getName());
49
        $index     = $this->getClient()->initIndex($indexName);
50
        $body      = $this->createQueryBuilder($request)->getQuery();
51
        $results   = $index->search($request->getPhrase(), [
52
            'attributesToRetrieve' => $body,
53
            'hitsPerPage'          => $this->getOption('maxResults'),
54
        ]);
55
        
56
        return $this->processResults($results);
57
    }
58
    
59
    public function addDocument(DocumentInterface $document)
60
    {
61
        $indexName = $this->getIndexName($document->getLocale(), $document->getType()->getName());
62
        $index     = $this->getClient()->initIndex($indexName);
63
        
64
        $index->addObject($this->createDocumentBody($document), $document->getIdentifier());
65
    }
66
    
67
    public function addDocuments(Collection $documents, string $locale, string $type)
68
    {
69
        $request = [];
70
        
71
        $documents->map(function (DocumentInterface $document) use (&$request) {
72
            $body             = $this->createDocumentBody($document);
73
            $body['objectID'] = $document->getIdentifier();
74
            $request[]        = $body;
75
        });
76
        
77
        $indexName = $this->getIndexName($locale, $type);
78
        $index     = $this->getClient()->initIndex($indexName);
79
        
80
        if (!empty($request)) {
81
            $index->addObjects($request);
82
        }
83
    }
84
    
85
    public function removeDocument(DocumentInterface $document)
86
    {
87
        $indexName = $this->getIndexName($document->getLocale(), $document->getType()->getName());
88
        $index     = $this->getClient()->initIndex($indexName);
89
        
90
        $index->deleteObject($document->getIdentifier());
91
    }
92
    
93
    public function updateDocument(DocumentInterface $document)
94
    {
95
        $indexName        = $this->getIndexName($document->getLocale(), $document->getType()->getName());
96
        $index            = $this->getClient()->initIndex($indexName);
97
        $body             = $this->createDocumentBody($document);
98
        $body['objectID'] = $document->getIdentifier();
99
        
100
        $index->saveObject($body);
101
    }
102
    
103
    public function getIndexName(string $locale, string $type): string
104
    {
105
        return sprintf('%s%s_%s', $this->getOption('indexPrefix'), $locale, $type);
106
    }
107
    
108
    public function createIndex(string $locale, string $type)
109
    {
110
    }
111
    
112
    public function removeIndex(string $locale, string $type)
113
    {
114
        $indexName = $this->getIndexName($locale, $type);
115
        
116
        return $this->getClient()->deleteIndex($indexName);
117
    }
118
    
119
    public function flushIndex(string $locale, string $type)
120
    {
121
        $indexName = $this->getIndexName($locale, $type);
122
        $index     = $this->getClient()->initIndex($indexName);
123
        
124
        return $index->clearIndex();
125
    }
126
    
127
    public function getStats(string $locale)
0 ignored issues
show
Unused Code introduced by
The parameter $locale is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
128
    {
129
        return $this->getClient()->getLogs(0, 100);
130
    }
131
    
132
    private function createQueryBuilder(SearchRequestInterface $request): QueryBuilderInterface
133
    {
134
        $queryBuilderClass = $this->getOption('builderClass');
135
        
136
        return new $queryBuilderClass($request, $this->getOption('termMinLength'));
137
    }
138
    
139
    private function createDocumentBody(DocumentInterface $document): array
140
    {
141
        $body = [];
142
        
143
        $document->getFields()->map(function (FieldInterface $field) use (&$body) {
144
            $body[$field->getName()] = $field->getValue();
145
        });
146
        
147
        return $body;
148
    }
149
    
150
    private function getClient(): Client
151
    {
152
        if (null === $this->client) {
153
            $this->client = new Client($this->getOption('appId'), $this->getOption('apiKey'));
154
        }
155
        
156
        return $this->client;
157
    }
158
    
159
    private function processResults(array $results): array
160
    {
161
        $identifiers = [];
162
        
163
        foreach ($results['hits'] as $hit) {
164
            $identifiers[] = $hit['objectID'];
165
        }
166
        
167
        return $identifiers;
168
    }
169
    
170
    private function getOption(string $name)
171
    {
172
        return $this->configurator->getSearchAdapterOptions()[$name];
173
    }
174
}
175