getCurrentSortParam()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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\Plugin\ElasticSearch\ResultFormatter;
9
10
use Elastica\ResultSet;
11
use Generated\Shared\Transfer\SortSearchResultTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\SortSearchResultTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Spryker\Client\Search\Plugin\Elasticsearch\ResultFormatter\AbstractElasticsearchResultFormatterPlugin;
13
14
/**
15
 * @method \SprykerEco\Client\FactFinderNg\FactFinderNgFactory getFactory()
16
 */
17
class FactFinderSortedResultFormatterPlugin extends AbstractElasticsearchResultFormatterPlugin
0 ignored issues
show
Deprecated Code introduced by
The class Spryker\Client\Search\Pl...chResultFormatterPlugin has been deprecated: Use {@link \Spryker\Client\SearchElasticsearch\Plugin\ResultFormatter\AbstractElasticsearchResultFormatterPlugin} instead. ( Ignorable by Annotation )

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

17
class FactFinderSortedResultFormatterPlugin extends /** @scrutinizer ignore-deprecated */ AbstractElasticsearchResultFormatterPlugin
Loading history...
18
{
19
    public const NAME = 'sort';
20
    public const SORT_ITEMS = 'sortItems';
21
    public const KEY_NAME = 'name';
22
    public const KEY_ORDER = 'order';
23
24
    /**
25
     * {@inheritDoc}
26
     *
27
     * @api
28
     *
29
     * @return string
30
     */
31
    public function getName(): string
32
    {
33
        return static::NAME;
34
    }
35
36
    /**
37
     * @param \Elastica\ResultSet $searchResult
38
     * @param array $requestParameters
39
     *
40
     * @return mixed
41
     */
42
    protected function formatSearchResult(ResultSet $searchResult, array $requestParameters)
43
    {
44
        $sortSearchResultTransfer = new SortSearchResultTransfer();
45
        $sortSearchResultTransfer
46
            ->setSortParamNames($this->mapSortParamNames($searchResult->getResponse()->getData()[static::SORT_ITEMS] ?? []))
47
            ->setCurrentSortParam($this->getCurrentSortParam($requestParameters));
48
49
        return $sortSearchResultTransfer;
50
    }
51
52
    /**
53
     * @param array $sortItems
54
     *
55
     * @return array
56
     */
57
    protected function mapSortParamNames(array $sortItems): array
58
    {
59
        $paramNames = [];
60
61
        foreach ($sortItems as $item) {
62
            $paramNames[] = sprintf('%s_%s', mb_strtolower($item[static::KEY_NAME]), $item[static::KEY_ORDER]);
63
        }
64
65
        return $paramNames;
66
    }
67
68
    /**
69
     * @param array $requestParameters
70
     *
71
     * @return string
72
     */
73
    protected function getCurrentSortParam(array $requestParameters): string
74
    {
75
        return $requestParameters[static::NAME] ?? '';
76
    }
77
}
78