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
240
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
20
 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($servicelist = []): Collection
26    {
27        try {
28            $authorityList = new Collection();
29
30            $sqlArgs = [];
31
32            if (!empty($servicelist)) {
33                $sqlArgs = ['de',$this->locale];
34                $questionMarks = array_fill(0, count($servicelist), '?');
35
36                $sql = "SELECT a.data_json
37                FROM authority_service AS aservice
38                LEFT JOIN authority AS a ON a.id = aservice.authority_id AND a.locale = ?
39                WHERE aservice.locale = ? AND aservice.service_id IN (" . implode(', ', $questionMarks) . ")";
40
41                array_push($sqlArgs, ...$servicelist);
42
43                $stm = $this->access()->prepare($sql);
44                $stm->execute($sqlArgs);
45                $stm->fetchAll(\PDO::FETCH_FUNC, function (?string $data_json) use ($authorityList): void {
46                    $authority = new Entity();
47                    $authority->offsetSet('data_json', $data_json);
48                    $authorityList[$authority['id']] = $authority;
49                    $authority->clearLocations();
50                });
51
52                $sqlArgs = [$this->locale];
53                $questionMarks = array_fill(0, count($servicelist), '?');
54
55                $sql = "SELECT ls.location_id AS id
56                    FROM location_service ls
57                    -- LEFT JOIN location AS l ON l.id = ls.location_id AND l.locale = ?
58                    WHERE ls.locale = ? AND ls.service_id IN (" . implode(', ', $questionMarks) . ")
59                    GROUP BY ls.location_id 
60                    ";
61
62                array_push($sqlArgs, ...$servicelist);
63
64                $stm = $this->access()->prepare($sql);
65                $stm->setFetchMode(\PDO::FETCH_OBJ);
66                $stm->execute($sqlArgs);
67
68                $locations = $stm->fetchAll();
69                $locationsIds = [];
70
71                foreach ($locations as $location) {
72                    $locationsIds[] = $location->id;
73                }
74
75                $locations = $this->access()->fromLocation($this->locale)
76                    ->fetchFromCsv(implode(',', $locationsIds), true);
77
78                $this->addLocationsToAuthorities($authorityList, $locations);
79            } else {
80                $sqlArgs = ['de'];
81                $sql = 'SELECT data_json FROM authority WHERE locale = ?';
82                $stm = $this->access()->prepare($sql);
83                $stm->execute($sqlArgs);
84                $stm->fetchAll(\PDO::FETCH_FUNC, function (?string $data_json) use ($authorityList): void {
85                    $authority = new Entity();
86                    $authority->offsetSet('data_json', $data_json);
87                    $authorityList[$authority['id']] = $authority;
88                    $authority->clearLocations();
89                });
90
91                $locations = $this->access()->fromLocation($this->locale)->fetchList(false, true);
92
93                $this->addLocationsToAuthorities($authorityList, $locations);
94            }
95            return $authorityList;
96        } catch (\Exception $e) {
97            throw $e;
98        }
99    }
100
101    /**
102     * @param Collection $authorityList
103     * @param iterable<mixed> $locations
104     */
105    private function addLocationsToAuthorities(Collection $authorityList, iterable $locations): void
106    {
107        foreach ($locations as $location) {
108            if (!$location instanceof LocationEntity) {
109                continue;
110            }
111            $authority = $authorityList[$location['authority']['id']] ?? null;
112            if ($authority instanceof AuthorityEntity) {
113                $authority->addLocation($location);
114            }
115        }
116    }
117
118    /**
119     * fetch a single authority by id
120     *
121     * @return Entity|false
122     */
123    #[\Override]
124    public function fetchId(mixed $itemId): Entity|false
125    {
126        try {
127            $sqlArgs = [$this->locale, $itemId];
128
129            $sql = 'SELECT data_json FROM authority WHERE locale = ? AND id = ?';
130            $stm = $this->access()->prepare($sql);
131            $stm->setFetchMode(\PDO::FETCH_CLASS | \PDO::FETCH_PROPS_LATE, Entity::class);
132            $stm->execute($sqlArgs);
133            if ($stm->rowCount() === 0) {
134                return false;
135            }
136            $authority = $stm->fetch();
137            return $authority instanceof Entity ? $authority : false;
138        } catch (\Exception $e) {
139            throw $e;
140        }
141    }
142
143    /**
144     *
145     * @return Collection
146     */
147    #[\Override]
148    public function readListByOfficePath($officepath): Collection
149    {
150        $authorityList = new Collection();
151
152        $locations = $this->access()->fromLocation($this->locale)->fetchListByOffice($officepath);
153
154        foreach ($locations as $location) {
155            if ($location instanceof LocationEntity) {
156                $authorityList->addLocation($location);
157            }
158        }
159
160        return $authorityList;
161    }
162}