Passed
Pull Request — master (#3)
by Volodymyr
07:03 queued 01:54
created

FactFinderHandler::throwSearchException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 8
rs 10
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Client\FactFinderNg\Handler;
9
10
use Elastica\Query;
11
use Elastica\ResultSet;
12
use Exception;
13
use Spryker\Client\Search\Dependency\Plugin\QueryInterface;
14
use Spryker\Client\Search\Exception\SearchResponseException;
15
use Spryker\Client\Search\Model\Handler\SearchHandlerInterface;
16
use SprykerEco\Client\FactFinderNg\Api\Adapter\Http\Factory\AdapterFactoryInterface;
17
use SprykerEco\Client\FactFinderNg\Dependency\Client\FactFinderNgToLocaleClientInterface;
18
use SprykerEco\Client\FactFinderNg\Dependency\Client\FactFinderNgToStoreClientInterface;
19
use SprykerEco\Client\FactFinderNg\Dependency\Service\FactFinderNgToUtilEncodingServiceInterface;
20
use SprykerEco\Client\FactFinderNg\Mapper\Elastica\FactFinderToElasticaMapperInterface;
21
use SprykerEco\Client\FactFinderNg\Mapper\Request\FactFinderNgRequestMapperInterface;
22
use SprykerEco\Client\FactFinderNg\Parser\ResponseParserInterface;
23
24
abstract class FactFinderHandler implements SearchHandlerInterface
25
{
26
    /**
27
     * @var \SprykerEco\Client\FactFinderNg\Mapper\Request\FactFinderNgRequestMapperInterface
28
     */
29
    protected $requestMapper;
30
31
    /**
32
     * @var \SprykerEco\Client\FactFinderNg\Api\Adapter\Http\Factory\AdapterFactoryInterface
33
     */
34
    protected $adapterFactory;
35
36
    /**
37
     * @var \SprykerEco\Client\FactFinderNg\Parser\ResponseParserInterface
38
     */
39
    protected $responseParser;
40
41
    /**
42
     * @var \SprykerEco\Client\FactFinderNg\Mapper\Elastica\FactFinderToElasticaMapperInterface
43
     */
44
    protected $factFinderToElasticaMapper;
45
46
    /**
47
     * @var \SprykerEco\Client\FactFinderNg\Dependency\Client\FactFinderNgToLocaleClientInterface
48
     */
49
    protected $localeClient;
50
51
    /**
52
     * @var \SprykerEco\Client\FactFinderNg\Dependency\Client\FactFinderNgToStoreClientInterface
53
     */
54
    protected $storeClient;
55
56
    /**
57
     * @var \SprykerEco\Client\FactFinderNg\Dependency\Service\FactFinderNgToUtilEncodingServiceInterface
58
     */
59
    protected $utilEncodingService;
60
61
    /**
62
     * @param \SprykerEco\Client\FactFinderNg\Mapper\Request\FactFinderNgRequestMapperInterface $factFinderNgRequestMapper
63
     * @param \SprykerEco\Client\FactFinderNg\Api\Adapter\Http\Factory\AdapterFactoryInterface $adapterFactory
64
     * @param \SprykerEco\Client\FactFinderNg\Parser\ResponseParserInterface $responseParser
65
     * @param \SprykerEco\Client\FactFinderNg\Mapper\Elastica\FactFinderToElasticaMapperInterface $factFinderToElasticaMapper
66
     * @param \SprykerEco\Client\FactFinderNg\Dependency\Client\FactFinderNgToLocaleClientInterface $localeClient
67
     * @param \SprykerEco\Client\FactFinderNg\Dependency\Client\FactFinderNgToStoreClientInterface $storeClient
68
     * @param \SprykerEco\Client\FactFinderNg\Dependency\Service\FactFinderNgToUtilEncodingServiceInterface $utilEncodingService
69
     */
70
    public function __construct(
71
        FactFinderNgRequestMapperInterface $factFinderNgRequestMapper,
72
        AdapterFactoryInterface $adapterFactory,
73
        ResponseParserInterface $responseParser,
74
        FactFinderToElasticaMapperInterface $factFinderToElasticaMapper,
75
        FactFinderNgToLocaleClientInterface $localeClient,
76
        FactFinderNgToStoreClientInterface $storeClient,
77
        FactFinderNgToUtilEncodingServiceInterface $utilEncodingService
78
    ) {
79
        $this->requestMapper = $factFinderNgRequestMapper;
80
        $this->adapterFactory = $adapterFactory;
81
        $this->responseParser = $responseParser;
82
        $this->factFinderToElasticaMapper = $factFinderToElasticaMapper;
83
        $this->localeClient = $localeClient;
84
        $this->storeClient = $storeClient;
85
        $this->utilEncodingService = $utilEncodingService;
86
    }
87
88
    /**
89
     * @param \Spryker\Client\Search\Dependency\Plugin\QueryInterface $searchQuery
90
     * @param \Spryker\Client\Search\Dependency\Plugin\ResultFormatterPluginInterface[] $resultFormatters
91
     * @param array $requestParameters
92
     *
93
     * @return array|\Elastica\ResultSet
94
     */
95
    public function search(
96
        QueryInterface $searchQuery,
97
        array $resultFormatters = [],
98
        array $requestParameters = []
99
    ) {
100
        $elasticaQuery = $searchQuery->getSearchQuery();
101
        $searchResult = $this->executeQuery($elasticaQuery, $requestParameters);
102
        $elasticaSearchResult = $this->factFinderToElasticaMapper->map(
103
            $searchResult,
104
            $elasticaQuery,
105
            $this->localeClient->getCurrentLocale(),
106
            $this->storeClient->getCurrentStore()
107
        );
108
109
        if (!$resultFormatters) {
110
            return $elasticaSearchResult;
111
        }
112
113
        return $this->formatSearchResults($resultFormatters, $elasticaSearchResult, $requestParameters);
114
    }
115
116
    /**
117
     * @param \Spryker\Client\Search\Dependency\Plugin\ResultFormatterPluginInterface[] $resultFormatters
118
     * @param \Elastica\ResultSet $rawSearchResult
119
     * @param array $requestParameters
120
     *
121
     * @return array
122
     */
123
    protected function formatSearchResults(
124
        array $resultFormatters,
125
        ResultSet $rawSearchResult,
126
        array $requestParameters
127
    ): array {
128
        $formattedSearchResult = [];
129
130
        foreach ($resultFormatters as $resultFormatter) {
131
            $formattedSearchResult[$resultFormatter->getName()] = $resultFormatter->formatResult($rawSearchResult, $requestParameters);
132
        }
133
134
        return $formattedSearchResult;
135
    }
136
137
    /**
138
     * @param \Exception $exception
139
     * @param \Elastica\Query $query
140
     *
141
     * @throws \Spryker\Client\Search\Exception\SearchResponseException
142
     *
143
     * @return void
144
     */
145
    protected function throwSearchException(Exception $exception, Query $query): void
146
    {
147
        $rawQuery = $this->utilEncodingService->encodeJson($query->toArray());
148
149
        throw new SearchResponseException(
150
            sprintf("Search failed with the following reason: %s. Query: %s", $exception->getMessage(), $rawQuery),
151
            $exception->getCode(),
152
            $exception
153
        );
154
    }
155
156
    /**
157
     * @param \Elastica\Query $query
158
     * @param array $requestParameters
159
     *
160
     * @throws \Spryker\Client\Search\Exception\SearchResponseException
161
     *
162
     * @return array
163
     */
164
    abstract protected function executeQuery(Query $query, array $requestParameters): array;
165
}
166