Passed
Push — master ( 60a4ae...29fcc8 )
by
unknown
46:42 queued 14:26
created

SspAssetSearchReader::prepareRequestParameters()   A

Complexity

Conditions 6
Paths 20

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
c 1
b 0
f 0
nc 20
nop 1
dl 0
loc 23
rs 9.2222
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
declare(strict_types = 1);
9
10
namespace SprykerFeature\Client\SelfServicePortal\Search\Reader;
11
12
use Generated\Shared\Transfer\SspAssetSearchCollectionTransfer;
13
use Generated\Shared\Transfer\SspAssetSearchCriteriaTransfer;
14
use Spryker\Client\CompanyUser\CompanyUserClientInterface;
15
use Spryker\Client\Search\SearchClientInterface;
16
use Spryker\Client\SearchExtension\Dependency\Plugin\QueryInterface;
17
use Spryker\Client\SearchExtension\Dependency\Plugin\SearchStringSetterInterface;
18
19
class SspAssetSearchReader implements SspAssetSearchReaderInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    protected const SEARCH_RESULT_KEY = 'ssp-asset-search';
25
26
    /**
27
     * @var string
28
     */
29
    protected const PARAMETER_SEARCH_STRING = 'q';
30
31
    /**
32
     * @var string
33
     */
34
    protected const PARAMETER_OFFSET = 'offset';
35
36
    /**
37
     * @var string
38
     */
39
    protected const PARAMETER_LIMIT = 'limit';
40
41
    /**
42
     * @var string
43
     */
44
    protected const PARAMETER_SORT = 'sort';
45
46
    /**
47
     * @param \Spryker\Client\Search\SearchClientInterface $searchClient
48
     * @param \Spryker\Client\SearchExtension\Dependency\Plugin\QueryInterface $sspAssetSearchQueryPlugin
49
     * @param \Spryker\Client\CompanyUser\CompanyUserClientInterface $companyUserClient
50
     * @param array<\Spryker\Client\SearchExtension\Dependency\Plugin\QueryExpanderPluginInterface> $sspAssetSearchQueryExpanderPlugins
51
     * @param array<\Spryker\Client\SearchExtension\Dependency\Plugin\ResultFormatterPluginInterface> $sspAssetSearchResultFormatterPlugins
52
     */
53
    public function __construct(
54
        protected SearchClientInterface $searchClient,
55
        protected QueryInterface $sspAssetSearchQueryPlugin,
56
        protected CompanyUserClientInterface $companyUserClient,
57
        protected array $sspAssetSearchQueryExpanderPlugins = [],
58
        protected array $sspAssetSearchResultFormatterPlugins = []
59
    ) {
60
    }
61
62
    public function getSspAssetSearchCollection(
63
        SspAssetSearchCriteriaTransfer $sspAssetSearchCriteriaTransfer
64
    ): SspAssetSearchCollectionTransfer {
65
        if (!$this->companyUserClient->findCompanyUser()) {
66
            return new SspAssetSearchCollectionTransfer();
67
        }
68
69
        $searchQuery = $this->sspAssetSearchQueryPlugin;
70
71
        $searchString = $sspAssetSearchCriteriaTransfer->getSearchString();
72
        $requestParameters = $this->prepareRequestParameters($sspAssetSearchCriteriaTransfer);
73
74
        if ($searchString && $searchQuery instanceof SearchStringSetterInterface) {
75
            $searchQuery->setSearchString($searchString);
76
        }
77
78
        $searchQuery = $this->searchClient->expandQuery(
79
            $searchQuery,
80
            $this->sspAssetSearchQueryExpanderPlugins,
81
            $requestParameters,
82
        );
83
84
        $result = $this->searchClient->search(
0 ignored issues
show
Deprecated Code introduced by
The function Spryker\Client\Search\Se...ientInterface::search() has been deprecated. ( Ignorable by Annotation )

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

84
        $result = /** @scrutinizer ignore-deprecated */ $this->searchClient->search(
Loading history...
85
            $searchQuery,
86
            $this->sspAssetSearchResultFormatterPlugins,
87
            $requestParameters,
88
        );
89
90
        return $result[static::SEARCH_RESULT_KEY] ?? new SspAssetSearchCollectionTransfer();
91
    }
92
93
    /**
94
     * @param \Generated\Shared\Transfer\SspAssetSearchCriteriaTransfer $criteriaTransfer
95
     *
96
     * @return array<string, mixed>
97
     */
98
    protected function prepareRequestParameters(SspAssetSearchCriteriaTransfer $criteriaTransfer): array
99
    {
100
        $parameters = [];
101
102
        if ($criteriaTransfer->getSearchString()) {
103
            $parameters[static::PARAMETER_SEARCH_STRING] = $criteriaTransfer->getSearchString();
104
        }
105
106
        if ($criteriaTransfer->getPagination()) {
107
            $pagination = $criteriaTransfer->getPagination();
108
            if ($pagination->getOffset()) {
109
                $parameters[static::PARAMETER_OFFSET] = $pagination->getOffset();
110
            }
111
            if ($pagination->getLimit()) {
112
                $parameters[static::PARAMETER_LIMIT] = $pagination->getLimit();
113
            }
114
        }
115
116
        if ($criteriaTransfer->getSort()) {
117
            $parameters[static::PARAMETER_SORT] = $criteriaTransfer->getSort();
118
        }
119
120
        return $parameters;
121
    }
122
}
123