Lines 22.89% 49 / 214
Methods 28.57% 6 / 21
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 fetchId 0.00% 0 / 13 0.00% 0 / 1 12
 fetchList 0.00% 0 / 16 0.00% 0 / 1 20
 fetchFromCsv 0.00% 0 / 17 0.00% 0 / 1 6
 searchAll 0.00% 0 / 33 0.00% 0 / 1 72
 readSearchResultList 0.00% 0 / 24 0.00% 0 / 1 20
 fetchServicesForCompilation 0.00% 0 / 28 0.00% 0 / 1 30
 [BO\Zmsdldb\File\Service] parseData 100.00% 6 / 6 100.00% 1 / 1 3
 [BO\Zmsdldb\File\Service] fetchListRelated 0.00% 0 / 15 0.00% 0 / 1 12
 [BO\Zmsdldb\File\Service] fetchCombinations 100.00% 1 / 1 100.00% 1 / 1 1
 [BO\Zmsdldb\File\Service] fetchLocationCsv 85.71% 6 / 7 0.00% 0 / 1 2.01
 [BO\Zmsdldb\File\Service] fetchListFromTopic 86.66% 13 / 15 0.00% 0 / 1 6.09
 [BO\Zmsdldb\File\Base] __construct 100.00% 2 / 2 100.00% 1 / 1 1
 [BO\Zmsdldb\File\Base] readDataFile 91.66% 11 / 12 0.00% 0 / 1 5.01
 [BO\Zmsdldb\File\Base] getDataAsArray 0.00% 0 / 4 0.00% 0 / 1 6
 [BO\Zmsdldb\File\Base] getHash 0.00% 0 / 4 0.00% 0 / 1 6
 [BO\Zmsdldb\File\Base] getData 0.00% 0 / 4 0.00% 0 / 1 6
 [BO\Zmsdldb\File\Base] loadData 100.00% 4 / 4 100.00% 1 / 1 2
 [BO\Zmsdldb\File\Base] getItemList 100.00% 3 / 3 100.00% 1 / 1 2
 [BO\Zmsdldb\File\Base] setItemList 0.00% 0 / 2 0.00% 0 / 1 2
 [BO\Zmsdldb\File\Base] setAccessInstance 100.00% 1 / 1 100.00% 1 / 1 1
 [BO\Zmsdldb\File\Base] access 66.66% 2 / 3 0.00% 0 / 1 2.15
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}

Inherited from BO\Zmsdldb\File\Service

24    protected function parseData(mixed $data)
25    {
26        $itemList = new Collection();
27        foreach ($data['data'] as $item) {
28            $service = new Entity($item);
29            if ($service->isLocale($this->locale)) {
30                $itemList[$item['id']] = $service;
31            }
32        }
33        return $itemList;
34    }
85    public function fetchListRelated(mixed $service_id)
86    {
87        $service = $this->fetchId($service_id);
88        if ($service === false) {
89            return new Collection();
90        }
91        $serviceList = $this->getItemList();
92
93        $relatedList = new Collection(
94            array_filter(
95                (array) $serviceList,
96                function ($item) use ($service) {
97                    $leikaIdentItem = substr(strval($item['leika']), 0, 11);
98                    $leikaIdentService = substr(strval($service['leika']), 0, 11);
99                    return ($leikaIdentItem == $leikaIdentService && $item['id'] != $service['id']);
100                }
101            )
102        );
103        return $relatedList;
104    }
110    public function fetchCombinations(mixed $service_csv)
111    {
112        return $this->fetchList($this->fetchLocationCsv($service_csv));
113    }
119    protected function fetchLocationCsv(mixed $service_csv)
120    {
121        $locationlist = $this->access()
122            ->fromLocation()
123            ->fetchList($service_csv);
124        $locationIdList = array();
125        foreach ($locationlist as $location) {
126            $locationIdList[] = $location['id'];
127        }
128        return implode(',', $locationIdList);
129    }
155    public function fetchListFromTopic(\BO\Zmsdldb\Entity\Topic $topic)
156    {
157        $itemlist = new Collection();
158        $serviceIds = $topic->getServiceIds();
159        if ($topic['relation']['navi'] && isset($topic['relation']['childs'])) {
160            foreach ($topic['relation']['childs'] as $child) {
161                $childtopic = $this->access()
162                    ->fromTopic()
163                    ->fetchPath($child['path']);
164                if ($childtopic) {
165                    $serviceIds = array_merge($serviceIds, $childtopic->getServiceIds());
166                }
167            }
168        }
169        if (count($serviceIds)) {
170            $servicelistCSV = implode(',', $serviceIds);
171            $servicelist = $this->fetchFromCsv($servicelistCSV);
172            return $servicelist;
173        }
174        /** @var Collection $sorted */
175        $sorted = $itemlist->sortByName();
176        return $sorted;
177    }

Inherited from BO\Zmsdldb\File\Base

49    public function __construct(mixed $dataFile, string $locale = "de")
50    {
51        $this->dataFile = $dataFile;
52        $this->locale = $locale;
53    }
55    public function readDataFile(): mixed
56    {
57        if (empty($this->data)) {
58            $jsonFile = $this->dataFile;
59            if (!is_readable($jsonFile)) {
60                throw new Exception("Cannot read $jsonFile");
61            }
62            $json = file_get_contents($jsonFile);
63            if ($json === false) {
64                throw new Exception("Cannot read $jsonFile");
65            }
66            $data = json_decode($json, true);
67            if (!$data) {
68                throw new Exception("Could not decide $jsonFile");
69            }
70            $this->data = $data;
71        }
72        return $this->data;
73    }
76    public function getDataAsArray(): mixed
77    {
78        try {
79            $data = $this->readDataFile();
80
81            return $data['data'];
82        } catch (\Exception $e) {
83            throw $e;
84        }
85    }
88    public function getHash(): mixed
89    {
90        try {
91            $data = $this->readDataFile();
92
93            return $data['hash'];
94        } catch (\Exception $e) {
95            throw $e;
96        }
97    }
100    public function getData(): mixed
101    {
102        try {
103            $data = $this->readDataFile();
104
105            return $data;
106        } catch (\Exception $e) {
107            throw $e;
108        }
109    }
114    public function loadData()
115    {
116        try {
117            $data = $this->readDataFile();
118            $this->itemList = $this->parseData($data);
119        } catch (\Exception $e) {
120            throw $e;
121        }
122    }
127    public function getItemList()
128    {
129        if (null === $this->itemList) {
130            $this->loadData();
131        }
132        return $this->itemList;
133    }
135    protected function setItemList(\BO\Zmsdldb\Collection\Base $list): static
136    {
137        /** @var TCollection $list */
138        $this->itemList = $list;
139        return $this;
140    }
158    public function setAccessInstance(\BO\Zmsdldb\AbstractAccess $accessInstance): void
159    {
160        $this->accessInstance = $accessInstance;
161    }
163    public function access(): \BO\Zmsdldb\AbstractAccess
164    {
165        if (!$this->accessInstance instanceof \BO\Zmsdldb\AbstractAccess) {
166            throw new Exception('Access instance is not initialized');
167        }
168        return $this->accessInstance;
169    }