Lines 54.25% 51 / 94
Methods 61.11% 11 / 18
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __clone 100.00% 2 / 2 100.00% 1 / 1 2
 addLocation 100.00% 15 / 15 100.00% 1 / 1 9
 addAuthority 100.00% 4 / 4 100.00% 1 / 1 2
 hasAuthority 100.00% 1 / 1 100.00% 1 / 1 1
 readByExtendedService 0.00% 0 / 5 0.00% 0 / 1 12
 hasLocations 0.00% 0 / 4 0.00% 0 / 1 12
 hasAppointments 75.00% 3 / 4 0.00% 0 / 1 3.14
 hasLocationId 100.00% 4 / 4 100.00% 1 / 1 3
 removeLocation 100.00% 4 / 4 100.00% 1 / 1 2
 removeLocationsWithoutAppointments 0.00% 0 / 12 0.00% 0 / 1 56
 removeEmptyAuthorities 100.00% 5 / 5 100.00% 1 / 1 3
 removeLocations 100.00% 4 / 4 100.00% 1 / 1 2
 toListWithOfficePath 100.00% 4 / 4 100.00% 1 / 1 2
 toListWithAssociatedLocations 100.00% 4 / 4 100.00% 1 / 1 2
 getAuthorityIds 0.00% 0 / 4 0.00% 0 / 1 6
 [BO\Zmsdldb\Collection\Base] offsetSet 100.00% 1 / 1 100.00% 1 / 1 1
 [BO\Zmsdldb\Collection\Base] sortByName 0.00% 0 / 5 0.00% 0 / 1 2
 [BO\Zmsdldb\Collection\Base] sortWithCollator 0.00% 0 / 12 0.00% 0 / 1 6
16class Authorities extends Base
17{
18    public function __clone()
19    {
20        foreach ($this as $key => $authority) {
21            $this[$key] = clone $authority;
22        }
23    }
24
25    public function addLocation(\BO\Zmsdldb\Entity\Location $location): static
26    {
27        if (
28            $location->offsetExists('authority')
29            && (($location['authority'] instanceof \BO\Zmsdldb\Entity\Base
30                    && $location['authority']->offsetExists('id')
31                    && $location['authority']->offsetExists('authority')
32                ) || (
33                    is_array($location['authority'])
34                    && array_key_exists('id', $location['authority'])
35                    && array_key_exists('name', $location['authority'])
36                )
37            )
38            && $location['authority']['id']
39        ) {
40            $authorityId = $location['authority']['id'];
41            $this->addAuthority($authorityId, $location['authority']['name']);
42            $authority = $this[$authorityId];
43            $authority['locations'][$location['id']] = $location;
44        }
45        return $this;
46    }
47
48    public function addAuthority(mixed $authority_id, mixed $name): static
49    {
50        if (! $this->hasAuthority($authority_id)) {
51            $authority = \BO\Zmsdldb\Entity\Authority::create($name);
52            $this[$authority_id] = $authority;
53        }
54        return $this;
55    }
56
57    public function hasAuthority(mixed $authority_id): bool
58    {
59        return $this->offsetExists($authority_id);
60    }
61
62    /**
63     * @psalm-api
64     */
65    public function readByExtendedService(mixed $service): static
66    {
67        foreach ($service['authorities'] as $authority) {
68            if (! $this->hasAuthority($authority['id'])) {
69                $this->addAuthority($authority['id'], $authority['name']);
70                $this[$authority['id']]['webinfo'] = $authority['webinfo'];
71            }
72        }
73        return $this;
74    }
75
76    /**
77     * Check if appointments are available
78     *
79     * @return bool
80     * @psalm-api
81     */
82    public function hasLocations()
83    {
84        foreach ($this as $authority) {
85            if ($authority->hasLocations()) {
86                return true;
87            }
88        }
89        return false;
90    }
91
92    /**
93     * Check if appointments are available
94     *
95     * @param String $serviceCsv
96     *            only check for this serviceCsv
97     * @param Bool $external
98     *            allow external links, default false
99     *
100     * @return Bool
101     * @psalm-api
102     */
103    public function hasAppointments($serviceCsv = null, $external = false)
104    {
105        foreach ($this as $authority) {
106            if ($authority->hasAppointments($serviceCsv, $external)) {
107                return true;
108            }
109        }
110        return false;
111    }
112
113    /**
114     * Check if ea_id location exists
115     *
116     * @param Int $locationId
117     *
118     * @return Bool
119     * @psalm-api
120     */
121    public function hasLocationId($locationId)
122    {
123        foreach ($this as $authority) {
124            if ($authority->hasLocationId($locationId)) {
125                return true;
126            }
127        }
128        return false;
129    }
130
131    /**
132     * Remove a location
133     *
134     * @param Int $locationId
135     *
136     * @return self
137     * @psalm-api
138     */
139    public function removeLocation($locationId)
140    {
141        $authorityList = clone $this;
142        foreach ($authorityList as $key => $authority) {
143            $authorityList[$key] = $authority->removeLocation($locationId);
144        }
145        return $authorityList;
146    }
147
148    /**
149     * remove locations if no appointment is available
150     *
151     * @param String $serviceCsv
152     *            only check for this serviceCsv
153     * @param Bool $external
154     *            allow external links, default false
155     *
156     * @return self
157     * @psalm-api
158     */
159    public function removeLocationsWithoutAppointments($serviceCsv = null, $external = false)
160    {
161        $authorityIterator = $this->getIterator();
162        foreach ($authorityIterator as $key => $authority) {
163            if (!$authority instanceof \BO\Zmsdldb\Entity\Authority) {
164                continue;
165            }
166            if ($authority->hasAppointments($serviceCsv, $external)) {
167                $locationIterator = $authority['locations']->getIterator();
168                foreach ($locationIterator as $subkey => $location) {
169                    if (! $location->hasAppointments($serviceCsv, $external)) {
170                        $locationIterator->offsetUnset($subkey);
171                    }
172                }
173            } else {
174                if ($key !== null) {
175                    $authorityIterator->offsetUnset($key);
176                }
177            }
178        }
179        return $this;
180    }
181
182    public function removeEmptyAuthorities(): self
183    {
184        $authoritylist = new self();
185        foreach ($this as $key => $authority) {
186            if ($authority->hasLocations()) {
187                $authoritylist[$key] = clone $authority;
188            }
189        }
190        return $authoritylist;
191    }
192
193    public function removeLocations(): static
194    {
195        $authoritylist = clone $this;
196        foreach ($authoritylist as $authority) {
197            $authority['locations'] = new Locations();
198        }
199        return $authoritylist;
200    }
201
202    public function toListWithOfficePath(mixed $officepath): \BO\Zmsdldb\Collection\Authorities
203    {
204        $authoritylist = clone $this;
205        foreach ($authoritylist as $key => $authority) {
206            $authoritylist[$key] = $authority->getLocationListByOfficePath($officepath);
207        }
208        return $authoritylist->removeEmptyAuthorities();
209    }
210
211    /**
212     * transform list to authorities with accociated locations
213     *
214     * @return self
215     * @psalm-api
216     */
217
218    public function toListWithAssociatedLocations(mixed $locationlist)
219    {
220        $authoritylist = $this->removeLocations();
221        foreach ($locationlist as $location) {
222            $authoritylist->addLocation($location);
223        }
224        return $authoritylist;
225    }
226
227    /**
228     * @psalm-api
229     *
230     * @return (int|mixed|string)[]
231     *
232     */
233    public function getAuthorityIds(): array
234    {
235        $ids = [];
236
237        foreach ($this as $key => $authority) {
238            $ids[] = $authority['id'] ?? $key;
239        }
240        return $ids;
241    }
242}

Inherited from BO\Zmsdldb\Collection\Base

19    public function offsetSet(mixed $key, mixed $value): void
20    {
21        parent::offsetSet($key, $value);
22    }
24    public function sortByName(): static
25    {
26        $itemList = clone $this;
27        $itemList->uasort(function ($a, $b) {
28            return strcmp(Sorter::toSortableString($a->getName()), Sorter::toSortableString($b->getName()));
29        });
30        return $itemList;
31    }
36    public function sortWithCollator(string $field = 'name', string $locale = 'de'): static
37    {
38        $collator = collator_create($locale);
39        if ($collator === null) {
40            throw new \RuntimeException('Could not create collator for locale ' . $locale);
41        }
42        $collator->setStrength(\Collator::QUATERNARY);
43        $collator->setAttribute(\Collator::QUATERNARY, \Collator::ON);
44        $collator->setAttribute(\Collator::CASE_FIRST, \Collator::ON);
45        $collator->setAttribute(\Collator::NUMERIC_COLLATION, \Collator::ON);
46
47        $itemList = clone $this;
48        $itemList->uasort(function ($itemA, $itemB) use ($collator, $field) {
49            return collator_compare($collator, $itemA[$field], $itemB[$field]);
50        });
51        return $itemList;
52    }