Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 131
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Service
0.00% covered (danger)
0.00%
0 / 131
0.00% covered (danger)
0.00%
0 / 6
702
0.00% covered (danger)
0.00%
0 / 1
 fetchId
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 fetchList
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
20
 fetchFromCsv
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
6
 searchAll
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
72
 readSearchResultList
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
20
 fetchServicesForCompilation
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3/**
4 * @package ClientDldb
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsdldb\Elastic;
9
10use BO\Zmsdldb\Entity\Service as Entity;
11use BO\Zmsdldb\Collection\Services as Collection;
12use BO\Zmsdldb\File\Service as Base;
13
14/**
15 * @SuppressWarnings(Coupling)
16 */
17class Service extends Base
18{
19    /**
20     *
21     * @return Entity|false
22     */
23    #[\Override]
24    public function fetchId(mixed $itemId)
25    {
26        if ($itemId) {
27            $query = Helper::boolFilteredQuery();
28            $filter = new \Elastica\Filter\Ids();
29            $filter->setIds($this->locale . $itemId);
30            $query->getFilter()->addMust($filter);
31            $result = $this->access()
32                ->getIndex()
33                ->getType('service')
34                ->search($query);
35            if ($result->count() == 1) {
36                $locationList = $result->getResults();
37                return new Entity($locationList[0]->getData());
38            }
39        }
40        return false;
41    }
42
43    /**
44     *
45     * @return Collection
46     */
47    #[\Override]
48    public function fetchList(bool|string $location_csv = false)
49    {
50        $boolquery = Helper::boolFilteredQuery();
51        $boolquery->getFilter()->addMust(Helper::localeFilter($this->locale));
52        $query = \Elastica\Query::create($boolquery);
53        /** @psalm-suppress RiskyTruthyFalsyComparison */
54        if (is_string($location_csv) && $location_csv !== '') {
55            $filter = new \Elastica\Filter\Terms('locations.location', explode(',', $location_csv));
56            $filter->setExecution('and');
57            $query->setPostFilter($filter);
58        }
59        $resultList = $this->access()
60            ->getIndex()
61            ->getType('service')
62            ->search($query, 10000);
63        $serviceList = new Collection();
64        foreach ($resultList as $result) {
65            $service = new Entity($result->getData());
66            $serviceList[$service['id']] = $service;
67        }
68        return $serviceList;
69    }
70
71    /**
72     *
73     * @return Collection
74     */
75    #[\Override]
76    public function fetchFromCsv(mixed $service_csv)
77    {
78        $query = Helper::boolFilteredQuery();
79        $filter = new \Elastica\Filter\Ids();
80        $ids = explode(',', $service_csv);
81        $ids = array_map(function ($value) {
82            return $this->locale . $value;
83        }, $ids);
84        $filter->setIds($ids);
85        $query->getFilter()->addMust($filter);
86        $resultList = $this->access()
87            ->getIndex()
88            ->getType('service')
89            ->search($query, 10000);
90        $serviceList = new Collection();
91        foreach ($resultList as $result) {
92            $service = new Entity($result->getData());
93            $serviceList[$service['id']] = $service;
94        }
95        return $serviceList;
96    }
97
98    /**
99     *
100     * @return Collection
101     */
102    #[\Override]
103    public function searchAll(mixed $querystring, bool|string $service_csv = '', bool|string $location_csv = '')
104    {
105        $query = new \Elastica\Query();
106        $locationsCsvByUser = false;
107        /** @psalm-suppress RiskyTruthyFalsyComparison */
108        if (! $location_csv) {
109            $location_csv = $this->fetchLocationCsv($service_csv);
110        } else {
111            $locationsCsvByUser = true;
112        }
113
114        $boolquery = new \Elastica\Query\BoolQuery();
115        $searchquery = new \Elastica\Query\QueryString();
116        if ('' === trim($querystring)) {
117            $searchquery->setQuery('*');
118        } else {
119            $searchquery->setQuery($querystring);
120        }
121        $searchquery->setFields([
122            'name^9',
123            'keywords^5'
124        ]);
125
126        $boolquery->addShould($searchquery);
127        $filter = new \Elastica\Filter\BoolFilter();
128        $filter->addMust(Helper::localeFilter($this->locale));
129        /** @psalm-suppress RiskyTruthyFalsyComparison */
130        if (is_string($location_csv) && $location_csv !== '') {
131            $filter->addMust(new \Elastica\Filter\Terms('locations.location', explode(',', $location_csv)));
132        }
133        $filteredQuery = new \Elastica\Query\Filtered($boolquery, $filter);
134        $query->setQuery($filteredQuery);
135        $query->addSort(['sort' => 'asc']);
136        $resultList = $this->access()
137        ->getIndex()
138        ->getType('service')
139        ->search($query, 1000);
140        $serviceList = new Collection();
141        foreach ($resultList as $result) {
142            $service = new Entity($result->getData());
143            $serviceList[$service['id']] = $service;
144        }
145        if ($locationsCsvByUser && is_string($location_csv)) {
146            $serviceList = $serviceList->containsLocation($location_csv);
147        }
148        return $serviceList;
149    }
150
151    /**
152     * this function is similar to self::searchAll() but it might get different boosts in the future
153     * additionally, a restriction by locale is missing
154     *
155     * @return Collection
156     */
157    #[\Override]
158    public function readSearchResultList(mixed $query, string $service_csv = '')
159    {
160        $boolquery = Helper::boolFilteredQuery();
161        $boolquery->getFilter()->addMust(Helper::localeFilter($this->locale));
162        $searchquery = new \Elastica\Query\QueryString();
163        if ('' === trim($query)) {
164            $searchquery->setQuery('*');
165        } else {
166            $searchquery->setQuery($query);
167        }
168        $searchquery->setFields([
169            'name^9',
170            'keywords^5'
171        ]);
172        $boolquery->getQuery()->addShould($searchquery);
173        $filter = null;
174        if ($service_csv) {
175            $filter = new \Elastica\Filter\Terms('services.service', explode(',', $service_csv));
176        }
177        $query = new \Elastica\Query\Filtered($boolquery, $filter);
178        $resultList = $this->access()
179            ->getIndex()
180            ->getType('service')
181            ->search($query, 1000);
182        $serviceList = new Collection();
183        foreach ($resultList as $result) {
184            $service = new Entity($result->getData());
185            $serviceList[$service['id']] = $service;
186        }
187        return $serviceList;
188    }
189
190    /**
191     * @psalm-api
192     */
193    public function fetchServicesForCompilation(array $authoritys = [], array $locations = [], array $services = []): Collection
194    {
195        $limit = 1000;
196
197        $localeFilter = new \Elastica\Query\Term(array(
198            'meta.locale' => $this->locale
199        ));
200
201        $boolquery = new \Elastica\Query\BoolQuery();
202        $boolquery->addMust($localeFilter);
203
204        if (!empty($authoritys)) {
205            $authorityFilter = new \Elastica\Query\Terms('authorities.id', array_values($authoritys));
206            $boolquery->addMust($authorityFilter);
207        }
208        if (!empty($locations)) {
209            $locationFilter = new \Elastica\Query\Terms('locations.location', array_values($locations));
210            $boolquery->addMust($locationFilter);
211        }
212        if (!empty($services)) {
213            $serviceFilter = new \Elastica\Query\Terms('id', array_values($services));
214            $boolquery->addMust($serviceFilter);
215        }
216
217        $query = \Elastica\Query::create($boolquery);
218        $query->addSort(['sort' => 'asc']);
219        $resultList = $this
220            ->access()
221            ->getIndex()
222            ->getType('service')
223            ->search($query, $limit)
224        ;
225        $serviceList = new Collection();
226        foreach ($resultList as $result) {
227            $service = new Entity($result->getData());
228            $serviceList[$service['id']] = $service;
229        }
230        return $serviceList;
231    }
232}