Lines 60.50% 72 / 119
Methods 50.00% 10 / 20
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 parseData 100.00% 6 / 6 100.00% 1 / 1 3
 searchAll 0.00% 0 / 13 0.00% 0 / 1 30
 fetchList 100.00% 7 / 7 100.00% 1 / 1 3
 fetchListRelated 0.00% 0 / 15 0.00% 0 / 1 12
 fetchCombinations 100.00% 1 / 1 100.00% 1 / 1 1
 fetchLocationCsv 85.71% 6 / 7 0.00% 0 / 1 2.01
 fetchFromCsv 100.00% 6 / 6 100.00% 1 / 1 4
 fetchListFromTopic 86.66% 13 / 15 0.00% 0 / 1 6.09
 readSearchResultList 100.00% 5 / 5 100.00% 1 / 1 1
 [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] fetchId 100.00% 5 / 5 100.00% 1 / 1 3
 [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
18class Service extends Base
19{
20    /**
21     * @return Collection
22     */
23    #[\Override]
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    }
35
36    /**
37     *
38     * @SuppressWarnings(Param)
39     * @return Collection
40     * @psalm-api
41     */
42    public function searchAll(mixed $querystring, bool|string $service_csv = false, bool|string $location_csv = false)
43    {
44        $serviceList = new Collection($this->fetchList($location_csv)->getArrayCopy());
45        if ($querystring) {
46            $serviceList = new Collection(array_filter((array) $serviceList, function ($item) use ($querystring) {
47                $length = (3 < strlen($querystring)) ? strlen($querystring) : 3;
48                $nameMatch = preg_match('/[' . $querystring . ']{' . $length . ',}/i', $item['name']);
49                $keywordMatch = preg_match('/[' . $querystring . ']{' . $length . ',}/i', $item['meta']['keywords']);
50                return ($nameMatch || $keywordMatch);
51            }));
52        }
53        /** @var array<int|string, Entity> $sortedItems */
54        $sortedItems = $serviceList->sortByName()->getArrayCopy();
55        $serviceList = new Collection($sortedItems);
56        if (!is_string($location_csv)) {
57            return $serviceList;
58        }
59        return $serviceList->containsLocation($location_csv);
60    }
61
62    /**
63     * @return Collection
64     *
65     * @param bool|string $location_csv
66     */
67    public function fetchList(bool|string $location_csv = false)
68    {
69        #echo '<pre>' . print_r($this,1) . '</pre>';exit;
70        $servicelist = $this->getItemList();
71        if (is_string($location_csv) && $location_csv !== '') {
72            $servicelist = new Collection(array_filter((array) $servicelist, function ($item) use ($location_csv) {
73                $service = new Entity($item);
74                return $service->containsLocation($location_csv);
75            }));
76        }
77        return $servicelist;
78    }
79
80    /**
81     *
82     * @return Collection
83     * @psalm-api
84     */
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    }
105
106    /**
107     *
108     * @return Collection
109     */
110    public function fetchCombinations(mixed $service_csv)
111    {
112        return $this->fetchList($this->fetchLocationCsv($service_csv));
113    }
114
115    /**
116     *
117     * @return string
118     */
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    }
130
131    /**
132     *
133     * @return Collection
134     */
135    public function fetchFromCsv(string $service_csv)
136    {
137        $servicelist = new Collection();
138        foreach (explode(',', $service_csv) as $service_id) {
139            $service = $this->fetchId($service_id);
140            if ($service && $service->isLocale($this->locale)) {
141                $servicelist[$service_id] = $service;
142            }
143        }
144        return $servicelist;
145    }
146
147    /**
148     * Return services by topic
149     * If topic is root, include sub-services
150     * root_topic in realations not usable because of multiple roots for one service
151     *
152     * @return Collection
153     * @psalm-api
154     */
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    }
178
179    /**
180     *
181     * @return Collection
182     * @psalm-api
183     */
184    public function readSearchResultList(mixed $query, string $service_csv = '')
185    {
186        $servicelist = $this->fetchCombinations($service_csv);
187        $servicelist = new Collection(array_filter((array) $servicelist, function ($item) use ($query) {
188            return false !== strpos($item['name'], $query);
189        }));
190        return $servicelist;
191    }
192}

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    }
145    public function fetchId(string $itemId)
146    {
147        $itemList = $this->getItemList();
148
149        if (! $itemId || !$itemList->offsetExists($itemId)) {
150            return false;
151        }
152
153        /** @var TEntity $item */
154        $item = $itemList[$itemId];
155        return $item;
156    }
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    }