Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 70
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Authority
0.00% covered (danger)
0.00%
0 / 70
0.00% covered (danger)
0.00%
0 / 4
272
0.00% covered (danger)
0.00%
0 / 1
 fetchList
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 1
30
 addLocationsToAuthorities
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 fetchId
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
 readListByOfficePath
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * @package ClientDldb
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsdldb\MySQL;
9
10use BO\Zmsdldb\MySQL\Collection\Authorities as Collection;
11use BO\Zmsdldb\MySQL\Entity\Authority as Entity;
12use BO\Zmsdldb\Elastic\Authority as Base;
13use BO\Zmsdldb\Entity\Authority as AuthorityEntity;
14use BO\Zmsdldb\Entity\Location as LocationEntity;
15
16/** @psalm-api */
17class Authority extends Base
18{
19    /**
20     * fetch locations for a list of service and group by authority
21     *
22     * @return Collection
23     */
24    #[\Override]
25    public function fetchList(mixed $servicelist = []): Collection
26    {
27        try {
28            $authorityList = new Collection();
29
30            $sqlArgs = [];
31
32            /** @psalm-suppress RiskyTruthyFalsyComparison */
33            if (is_array($servicelist) && $servicelist !== []) {
34                $sqlArgs = ['de',$this->locale];
35                $questionMarks = array_fill(0, count($servicelist), '?');
36
37                $sql = "SELECT a.data_json
38                FROM authority_service AS aservice
39                LEFT JOIN authority AS a ON a.id = aservice.authority_id AND a.locale = ?
40                WHERE aservice.locale = ? AND aservice.service_id IN (" . implode(', ', $questionMarks) . ")";
41
42                array_push($sqlArgs, ...$servicelist);
43
44                $stm = $this->access()->prepare($sql);
45                $stm->execute($sqlArgs);
46                $stm->fetchAll(\PDO::FETCH_FUNC, function (?string $data_json) use ($authorityList): void {
47                    $authority = new Entity();
48                    $authority->offsetSet('data_json', $data_json);
49                    $authorityList[$authority['id']] = $authority;
50                    $authority->clearLocations();
51                });
52
53                $sqlArgs = [$this->locale];
54                $questionMarks = array_fill(0, count($servicelist), '?');
55
56                $sql = "SELECT ls.location_id AS id
57                    FROM location_service ls
58                    -- LEFT JOIN location AS l ON l.id = ls.location_id AND l.locale = ?
59                    WHERE ls.locale = ? AND ls.service_id IN (" . implode(', ', $questionMarks) . ")
60                    GROUP BY ls.location_id 
61                    ";
62
63                array_push($sqlArgs, ...$servicelist);
64
65                $stm = $this->access()->prepare($sql);
66                $stm->setFetchMode(\PDO::FETCH_OBJ);
67                $stm->execute($sqlArgs);
68
69                $locations = $stm->fetchAll();
70                $locationsIds = [];
71
72                foreach ($locations as $location) {
73                    $locationsIds[] = $location->id;
74                }
75
76                $locations = $this->access()->fromLocation($this->locale)
77                    ->fetchFromCsv(implode(',', $locationsIds), true);
78
79                $this->addLocationsToAuthorities($authorityList, $locations);
80            } else {
81                $sqlArgs = ['de'];
82                $sql = 'SELECT data_json FROM authority WHERE locale = ?';
83                $stm = $this->access()->prepare($sql);
84                $stm->execute($sqlArgs);
85                $stm->fetchAll(\PDO::FETCH_FUNC, function (?string $data_json) use ($authorityList): void {
86                    $authority = new Entity();
87                    $authority->offsetSet('data_json', $data_json);
88                    $authorityList[$authority['id']] = $authority;
89                    $authority->clearLocations();
90                });
91
92                $locations = $this->access()->fromLocation($this->locale)->fetchList(false, true);
93
94                $this->addLocationsToAuthorities($authorityList, $locations);
95            }
96            return $authorityList;
97        } catch (\Exception $e) {
98            throw $e;
99        }
100    }
101
102    /**
103     * @param Collection $authorityList
104     * @param iterable<mixed> $locations
105     */
106    private function addLocationsToAuthorities(Collection $authorityList, iterable $locations): void
107    {
108        foreach ($locations as $location) {
109            if (!$location instanceof LocationEntity) {
110                continue;
111            }
112            $authority = $authorityList[$location['authority']['id']] ?? null;
113            if ($authority instanceof AuthorityEntity) {
114                $authority->addLocation($location);
115            }
116        }
117    }
118
119    /**
120     * fetch a single authority by id
121     *
122     * @return Entity|false
123     */
124    #[\Override]
125    public function fetchId(mixed $itemId): Entity|false
126    {
127        try {
128            $sqlArgs = [$this->locale, $itemId];
129
130            $sql = 'SELECT data_json FROM authority WHERE locale = ? AND id = ?';
131            $stm = $this->access()->prepare($sql);
132            $stm->setFetchMode(\PDO::FETCH_CLASS | \PDO::FETCH_PROPS_LATE, Entity::class);
133            $stm->execute($sqlArgs);
134            if ($stm->rowCount() === 0) {
135                return false;
136            }
137            $authority = $stm->fetch();
138            return $authority instanceof Entity ? $authority : false;
139        } catch (\Exception $e) {
140            throw $e;
141        }
142    }
143
144    /**
145     *
146     * @return Collection
147     */
148    #[\Override]
149    public function readListByOfficePath(mixed $officepath): Collection
150    {
151        $authorityList = new Collection();
152
153        $locations = $this->access()->fromLocation($this->locale)->fetchListByOffice($officepath);
154
155        foreach ($locations as $location) {
156            if ($location instanceof LocationEntity) {
157                $authorityList->addLocation($location);
158            }
159        }
160
161        return $authorityList;
162    }
163}