Issues (3641)

Writer/Search/ElasticsearchUpdateWriter.php (1 issue)

1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace Spryker\Zed\Collector\Business\Exporter\Writer\Search;
9
10
use Elastica\Client;
11
use Elastica\Document;
12
use Generated\Shared\Transfer\SearchCollectorConfigurationTransfer;
13
use Spryker\Zed\Collector\Business\Exporter\Writer\WriterInterface;
14
use Spryker\Zed\Collector\Business\Index\IndexFactoryInterface;
15
16
class ElasticsearchUpdateWriter implements WriterInterface, ConfigurableSearchWriterInterface
17
{
18
    /**
19
     * @var \Elastica\Client
20
     */
21
    protected $client;
22
23
    /**
24
     * @var \Generated\Shared\Transfer\SearchCollectorConfigurationTransfer
25
     */
26
    protected $searchCollectorConfiguration;
27
28
    /**
29
     * @var \Spryker\Zed\Collector\Business\Index\IndexFactoryInterface
30
     */
31
    protected $indexFactory;
32
33
    /**
34
     * @param \Elastica\Client $searchClient
35
     * @param string $indexName
36
     * @param string $type
37
     * @param \Spryker\Zed\Collector\Business\Index\IndexFactoryInterface $indexFactory
38
     */
39
    public function __construct(Client $searchClient, $indexName, $type, IndexFactoryInterface $indexFactory)
40
    {
41
        $this->client = $searchClient;
42
43
        $this->searchCollectorConfiguration = new SearchCollectorConfigurationTransfer();
44
        $this->searchCollectorConfiguration
45
            ->setIndexName($indexName)
46
            ->setTypeName($type);
47
48
        $this->indexFactory = $indexFactory;
49
    }
50
51
    /**
52
     * @param array<string, mixed> $dataSet
53
     *
54
     * @return bool
55
     */
56
    public function write(array $dataSet)
57
    {
58
        $this->getIndex()->updateDocuments($this->createDocuments($dataSet));
59
        $response = $this->getIndex()->refresh();
0 ignored issues
show
The method refresh() does not exist on Spryker\Zed\Collector\Bu...x\IndexAdapterInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Spryker\Zed\Collector\Bu...x\IndexAdapterInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
        $response = $this->getIndex()->/** @scrutinizer ignore-call */ refresh();
Loading history...
60
61
        return $response->isOk();
62
    }
63
64
    /**
65
     * @param array<string, mixed> $dataSet
66
     *
67
     * @return bool
68
     */
69
    public function delete(array $dataSet)
70
    {
71
        return false;
72
    }
73
74
    /**
75
     * @param array<string, mixed> $dataSet
76
     *
77
     * @return array<\Elastica\Document>
78
     */
79
    protected function createDocuments(array $dataSet)
80
    {
81
        $documentPrototype = new Document();
82
        $documents = [];
83
84
        foreach ($dataSet as $key => $data) {
85
            $document = clone $documentPrototype;
86
            $document->setId($key);
87
            $document->setData($data);
88
            $documents[] = $document;
89
        }
90
91
        return $documents;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getName()
98
    {
99
        return 'elasticsearch-update-writer';
100
    }
101
102
    /**
103
     * @return \Elastica\Index|\Spryker\Zed\Collector\Business\Index\IndexAdapterInterface
104
     */
105
    protected function getIndex()
106
    {
107
        return $this->indexFactory->createIndex($this->client, $this->getSearchCollectorConfiguration());
108
    }
109
110
    /**
111
     * @param \Generated\Shared\Transfer\SearchCollectorConfigurationTransfer $collectorConfigurationTransfer
112
     *
113
     * @return void
114
     */
115
    public function setSearchCollectorConfiguration(SearchCollectorConfigurationTransfer $collectorConfigurationTransfer)
116
    {
117
        $this->searchCollectorConfiguration->fromArray($collectorConfigurationTransfer->modifiedToArray());
118
    }
119
120
    /**
121
     * @return \Generated\Shared\Transfer\SearchCollectorConfigurationTransfer
122
     */
123
    public function getSearchCollectorConfiguration()
124
    {
125
        return $this->searchCollectorConfiguration;
126
    }
127
}
128