Lines 22.68% 27 / 119
Methods 29.41% 5 / 17
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 fetchList 0.00% 0 / 47 0.00% 0 / 1 30
 addLocationsToAuthorities 0.00% 0 / 6 0.00% 0 / 1 20
 fetchId 0.00% 0 / 11 0.00% 0 / 1 20
 readListByOfficePath 0.00% 0 / 6 0.00% 0 / 1 12
 [BO\Zmsdldb\Elastic\Authority] fromLocationResults 0.00% 0 / 5 0.00% 0 / 1 6
 [BO\Zmsdldb\File\Authority] parseData 100.00% 4 / 4 100.00% 1 / 1 2
 [BO\Zmsdldb\File\Authority] fetchSource 0.00% 0 / 1 0.00% 0 / 1 2
 [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 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}

Inherited from BO\Zmsdldb\Elastic\Authority

52    public function fromLocationResults(mixed $resultList)
53    {
54        $authorityList = new Collection();
55        foreach ($resultList as $result) {
56            $location = new \BO\Zmsdldb\Entity\Location($result->getData());
57            $authorityList->addLocation($location);
58        }
59        return $authorityList;
60    }

Inherited from BO\Zmsdldb\File\Authority

24    protected function parseData(mixed $data)
25    {
26        $itemList = new Collection();
27        foreach ($data['data'] as $item) {
28            $itemList[$item['id']] = new Entity($item);
29        }
30        return $itemList;
31    }
94    public function fetchSource(): \BO\Zmsdldb\Collection\Base
95    {
96        return $this->getItemList();
97    }

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    }