Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.69% covered (warning)
81.69%
290 / 355
74.19% covered (warning)
74.19%
23 / 31
CRAP
0.00% covered (danger)
0.00%
0 / 1
Scope
81.69% covered (warning)
81.69%
290 / 355
74.19% covered (warning)
74.19%
23 / 31
213.36
0.00% covered (danger)
0.00%
0 / 1
 readEntity
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
7
 readEntitiesByIds
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
110
 readResolvedReferences
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 readByClusterId
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
1 / 1
11
 readByProviderId
76.67% covered (warning)
76.67%
23 / 30
0.00% covered (danger)
0.00%
0 / 1
11.27
 readByRequestId
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 readByDepartmentId
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
1 / 1
11
 readListBySource
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 testSource
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 readCollection
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 readList
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
9
 readIsOpened
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 readIsEnabled
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 readWaitingNumberUpdated
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 readDisplayNumberUpdated
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 readIsGivenNumberInContingent
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 readQueueList
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 readWithWorkstationCount
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 readQueueListWithWaitingTime
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 readListWithScopeAdminEmail
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
4
 writeEntity
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 updateEntity
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 replacePreferences
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 updateGhostWorkstationCount
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 updateEmergency
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 writeImageData
93.33% covered (success)
93.33%
14 / 15
0.00% covered (danger)
0.00%
0 / 1
4.00
 readImageData
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 deleteImage
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 deleteEntity
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 deletePreferences
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 removeCache
56.25% covered (warning)
56.25%
9 / 16
0.00% covered (danger)
0.00%
0 / 1
21.13
1<?php
2
3namespace BO\Zmsdb;
4
5use BO\Zmsentities\Scope as Entity;
6use BO\Zmsentities\Collection\ScopeList as Collection;
7use BO\Zmsdb\Application as App;
8
9/**
10 *
11 * @SuppressWarnings(Public)
12 * @SuppressWarnings(Coupling)
13 * @SuppressWarnings(Complexity)
14 * @SuppressWarnings(TooManyMethods)
15 *
16 */
17class Scope extends Base
18{
19    public static $cache = [ ];
20
21    public function readEntity($scopeId, $resolveReferences = 0, $disableCache = false)
22    {
23        $cacheKey = "scope-$scopeId-$resolveReferences";
24
25        if (!$disableCache && App::$cache && App::$cache->has($cacheKey)) {
26            $scope = App::$cache->get($cacheKey);
27        }
28
29        if (empty($scope)) {
30            $query = new Query\Scope(Query\Base::SELECT);
31            $query->addEntityMapping()
32                ->addResolvedReferences($resolveReferences)
33                ->addConditionScopeId($scopeId);
34            $scope = $this->fetchOne($query, new Entity());
35            if (! $scope->hasId()) {
36                return null;
37            }
38
39            if (App::$cache) {
40                App::$cache->set($cacheKey, $scope);
41            }
42        }
43
44        return $this->readResolvedReferences($scope, $resolveReferences, $disableCache);
45    }
46
47    public function readEntitiesByIds(array $scopeIds, int $resolveReferences = 0, bool $disableCache = false): array
48    {
49        $scopeIds = array_values(array_unique(array_filter(array_map('intval', $scopeIds))));
50        if (!$scopeIds) {
51            return [];
52        }
53
54        $result  = [];
55        $missing = [];
56        foreach ($scopeIds as $scopeId) {
57            $cacheKey = "{$scopeId}-{$resolveReferences}";
58            if (!$disableCache && array_key_exists($cacheKey, self::$cache)) {
59                $result[$scopeId] = self::$cache[$cacheKey];
60            } else {
61                $missing[] = $scopeId;
62            }
63        }
64
65        if ($missing) {
66            $query = new Query\Scope(Query\Base::SELECT);
67            $query
68                ->addEntityMapping()
69                ->addResolvedReferences($resolveReferences)
70                ->addConditionScopeIds($missing);
71
72            $fetched = $this->fetchList($query, new Entity());
73            foreach ($fetched as $entity) {
74                if (!$entity->hasId()) {
75                    continue;
76                }
77                $entity = $this->readResolvedReferences($entity, $resolveReferences);
78                $result[$entity->id] = $entity;
79                self::$cache["{$entity->id}-{$resolveReferences}"] = $entity;
80            }
81        }
82
83        $ordered = [];
84        foreach ($scopeIds as $id) {
85            if (isset($result[$id])) {
86                $ordered[$id] = $result[$id];
87            }
88        }
89
90        return $ordered;
91    }
92
93    public function readResolvedReferences(
94        \BO\Zmsentities\Schema\Entity $scope,
95        $resolveReferences,
96        $disableCache = false
97    ) {
98        if (0 < $resolveReferences) {
99            $scope['dayoff'] = (new DayOff())->readByScopeId($scope->id, $disableCache);
100            $scope['closure'] = (new Closure())->readByScopeId($scope->id, $disableCache);
101        }
102        return $scope;
103    }
104
105    public function readByClusterId(
106        $clusterId,
107        $resolveReferences = 0,
108        $disableCache = false
109    ) {
110        $cacheKey = "scopeReadByClusterId-$clusterId-$resolveReferences";
111
112        if (!$disableCache && App::$cache && App::$cache->has($cacheKey)) {
113            $result = App::$cache->get($cacheKey);
114        }
115
116        if (empty($result)) {
117            if ($resolveReferences > 0) {
118                $query = new Query\Scope(Query\Base::SELECT);
119                $query->addEntityMapping()
120                    ->addResolvedReferences($resolveReferences - 1)
121                    ->addConditionClusterId($clusterId);
122                $result = $this->fetchList($query, new Entity());
123            } else {
124                $result = $this->getReader()->perform(
125                    (new Query\Scope(Query\Base::SELECT))->getQuerySimpleClusterMatch(),
126                    [$clusterId]
127                );
128            }
129
130            if (App::$cache && !($result instanceof \PDOStatement)) {
131                App::$cache->set($cacheKey, $result);
132            }
133        }
134
135        $scopeList = new Collection();
136        if (!$result) {
137            return $scopeList;
138        }
139
140        foreach ($result as $entity) {
141            if (0 == $resolveReferences) {
142                $entity = new Entity(
143                    array(
144                        'id' => $entity['id'],
145                        '$ref' => '/scope/' . $entity['id'] . '/'
146                    )
147                );
148                $scopeList->addEntity($entity);
149            } else {
150                $scopeList->addEntity($this->readResolvedReferences(
151                    $entity,
152                    $resolveReferences - 1,
153                    $disableCache
154                ));
155            }
156        }
157
158        return $scopeList;
159    }
160
161    public function readByProviderId($providerId, $resolveReferences = 0, $disableCache = false)
162    {
163        $cacheKey = "scopeReadByProviderId-$providerId-$resolveReferences";
164
165        if (!$disableCache && App::$cache && App::$cache->has($cacheKey)) {
166            $result = App::$cache->get($cacheKey);
167        }
168
169        if (empty($result)) {
170            $query = new Query\Scope(Query\Base::SELECT);
171            $query->addEntityMapping()
172                ->addResolvedReferences($resolveReferences)
173                ->addConditionProviderId($providerId);
174            $result = $this->fetchList($query, new Entity());
175
176            if (App::$cache) {
177                App::$cache->set($cacheKey, $result);
178            }
179        }
180
181        $scopeList = new Collection();
182        if (count($result)) {
183            foreach ($result as $entity) {
184                if (0 == $resolveReferences) {
185                    $entity = new Entity(
186                        array(
187                            'id' => $entity->id,
188                            '$ref' => '/scope/' . $entity->id . '/'
189                        )
190                    );
191                    $scopeList->addEntity($entity);
192                } else {
193                    if ($entity instanceof Entity) {
194                        $entity = $this->readResolvedReferences(
195                            $entity,
196                            $resolveReferences - 1,
197                            $disableCache
198                        );
199                        $scopeList->addEntity($entity);
200                    }
201                }
202            }
203        }
204
205        return $scopeList;
206    }
207
208    public function readByRequestId($requestId, $source, $resolveReferences = 0)
209    {
210        $scopeList = new Collection();
211        $providerList = (new Provider())->readListBySource($source, 0, true, $requestId);
212
213        foreach ($providerList as $provider) {
214            $scopeListByProvider = $this->readByProviderId($provider->getId(), $resolveReferences);
215            if ($scopeListByProvider->count()) {
216                $scopeList->addList($scopeListByProvider);
217            }
218        }
219        return $scopeList->withUniqueScopes();
220    }
221
222    public function readByDepartmentId($departmentId, $resolveReferences = 0, $disableCache = false)
223    {
224        $cacheKey = "scopeReadByDepartmentId-$departmentId-$resolveReferences";
225
226        if (!$disableCache && App::$cache && App::$cache->has($cacheKey)) {
227            $result = App::$cache->get($cacheKey);
228        }
229
230        $scopeList = new Collection();
231
232        if (empty($result)) {
233            if ($resolveReferences > 0) {
234                $query = new Query\Scope(Query\Base::SELECT);
235                $query->addEntityMapping()
236                    ->addResolvedReferences($resolveReferences)
237                    ->addConditionDepartmentId($departmentId);
238                $result = $this->fetchList($query, new Entity());
239
240                if (App::$cache) {
241                    App::$cache->set($cacheKey, $result);
242                }
243            } else {
244                $result = $this->getReader()->perform(
245                    (new Query\Scope(Query\Base::SELECT))->getQuerySimpleDepartmentMatch(),
246                    [$departmentId]
247                );
248            }
249        }
250
251        if ($result) {
252            foreach ($result as $entity) {
253                if (0 == $resolveReferences) {
254                    $entity = new Entity(
255                        array(
256                            'id' => $entity['id'],
257                            'contact' => ['name' => $entity['contact__name']],
258                            '$ref' => '/scope/' . $entity['id'] . '/'
259                        )
260                    );
261                    $scopeList->addEntity($entity);
262                } else {
263                    if ($entity instanceof Entity) {
264                        $entity = $this->readResolvedReferences($entity, $resolveReferences, $disableCache);
265                        $scopeList->addEntity($entity);
266                    }
267                }
268            }
269        }
270
271        return $scopeList;
272    }
273    public function readListBySource($source, $resolveReferences = 0)
274    {
275        $this->testSource($source);
276        $query = new Query\Request(Query\Base::SELECT);
277        $query->setResolveLevel($resolveReferences);
278        $query->addConditionRequestSource($source);
279        $query->addEntityMapping();
280        $requestList = $this->readCollection($query);
281        return ($requestList->count()) ? $requestList->sortByCustomKey('id') : $requestList;
282    }
283
284    protected function testSource($source)
285    {
286        if (! (new Source())->readEntity($source)) {
287            throw new Exception\Source\UnknownDataSource();
288        }
289    }
290
291    protected function readCollection($query)
292    {
293        $requestList = new Collection();
294        $statement = $this->fetchStatement($query);
295        while ($requestData = $statement->fetch(\PDO::FETCH_ASSOC)) {
296            $request = new Entity($query->postProcessJoins($requestData));
297            $requestList->addEntity($request);
298        }
299        return $requestList;
300    }
301
302    public function readList($resolveReferences = 0, $disableCache = false)
303    {
304        $cacheKey = "scopeReadList-$resolveReferences";
305
306        if (!$disableCache && App::$cache && App::$cache->has($cacheKey)) {
307            $result = App::$cache->get($cacheKey);
308        }
309
310        if (empty($result)) {
311            $query = new Query\Scope(Query\Base::SELECT);
312            $query->addEntityMapping()
313                ->addResolvedReferences($resolveReferences);
314            $result = $this->fetchList($query, new Entity());
315
316            if (App::$cache) {
317                App::$cache->set($cacheKey, $result);
318            }
319        }
320
321        $scopeList = new Collection();
322        if (count($result)) {
323            foreach ($result as $entity) {
324                if ($entity instanceof Entity) {
325                    $entity = $this->readResolvedReferences($entity, $resolveReferences);
326                    $scopeList->addEntity($entity);
327                }
328            }
329        }
330        return $scopeList;
331    }
332
333    /**
334     * get a scope and return true if it is opened
335     *
336     * * @param
337     * scopeId
338     * now
339     *
340     * @return Bool
341     */
342    public function readIsOpened($scopeId, $now)
343    {
344        $isOpened = false;
345        $availabilityList = (new Availability())->readOpeningHoursListByDate($scopeId, $now, 2);
346        if ($availabilityList->isOpened($now)) {
347            $isOpened = true;
348        }
349        return $isOpened;
350    }
351
352    public function readIsEnabled($scopeId, $now)
353    {
354        $query = new Query\Scope(Query\Base::SELECT);
355        $query->addEntityMapping()
356            ->setResolveLevel(0)
357            ->addConditionScopeId($scopeId);
358        $scope = $this->fetchOne($query, new Entity());
359        return (
360            $this->readIsOpened($scopeId, $now) &&
361            $this->readIsGivenNumberInContingent($scopeId) &&
362            ! $scope->getStatus('ticketprinter', 'deactivated')
363        );
364    }
365
366    /**
367     * get last given waitingnumer and return updated (+1) waitingnumber
368     *
369     * * @param
370     * scopeId
371     * now
372     *
373     * @return Bool
374     */
375    public function readWaitingNumberUpdated($scopeId, $dateTime, $respectContingent = true)
376    {
377        if (! $this->readIsGivenNumberInContingent($scopeId) && $respectContingent) {
378            throw new Exception\Scope\GivenNumberCountExceeded();
379        }
380        $this->perform(
381            (new Query\Scope(Query\Base::SELECT))->getQueryLastWaitingNumber(),
382            ['scope_id' => $scopeId]
383        );
384        $entity = $this->readEntity($scopeId, 0, true)->updateStatusQueue($dateTime);
385        $scope = $this->updateEntity($scopeId, $entity);
386        return $scope->getStatus('queue', 'lastGivenNumber');
387    }
388
389    public function readDisplayNumberUpdated($scopeId)
390    {
391        $this->perform(
392            (new Query\Scope(Query\Base::SELECT))->getQueryLastDisplayNumber(),
393            ['scope_id' => $scopeId]
394        );
395        $entity = $this->readEntity($scopeId, 0, true)->incrementDisplayNumber();
396        $scope = $this->updateEntity($scopeId, $entity);
397        return $scope->getStatus('queue', 'lastDisplayNumber');
398    }
399
400    /**
401     * get last given waitingnumer and return updated (+1) waitingnumber
402     *
403     * * @param
404     * scopeId
405     * now
406     *
407     * @return Bool
408     */
409    public function readIsGivenNumberInContingent($scopeId)
410    {
411        $isInContingent = $this->getReader()
412            ->fetchValue((new Query\Scope(Query\Base::SELECT))
413            ->getQueryGivenNumbersInContingent(), ['scope_id' => $scopeId]);
414        return ($isInContingent) ? true : false;
415    }
416
417    /**
418     * get list of queues on scope by daytime
419     *
420     * * @param
421     * scopeId
422     * now
423     *
424     * @return number
425     */
426    public function readQueueList($scopeId, $dateTime, $resolveReferences = 0)
427    {
428        if ($resolveReferences > 0) {
429            // resolveReferences > 0 is only necessary for a resolved process
430            $queueList = (new Process())
431                ->readProcessListByScopeAndTime($scopeId, $dateTime, $resolveReferences - 1)
432                ->toQueueList($dateTime);
433        } else {
434            $queueList = (new Queue())
435                ->readListByScopeAndTime($scopeId, $dateTime, $resolveReferences);
436        }
437        return $queueList->withSortedArrival();
438    }
439
440    /**
441     * get waitingtime of scope
442     *
443     * * @param
444     * scopeId
445     * now
446     *
447     * @return \BO\Zmsentities\Scope
448     */
449    public function readWithWorkstationCount($scopeId, $dateTime, $resolveReferences = 0)
450    {
451        //get scope
452        $query = new Query\Scope(Query\Base::SELECT);
453        $query
454            ->addEntityMapping()
455            ->addConditionScopeId($scopeId)
456            ->addResolvedReferences($resolveReferences)
457            ->addSelectWorkstationCount($dateTime);
458        $scope = $this->fetchOne($query, new Entity());
459        $scope = $this->readResolvedReferences($scope, $resolveReferences);
460        return ($scope->hasId()) ? $scope : null;
461    }
462
463    public function readQueueListWithWaitingTime($scope, $dateTime, $resolveReferences = 0)
464    {
465        $timeAverage = $scope->getPreference('queue', 'processingTimeAverage');
466        $scope = (! $timeAverage) ? (new Scope())->readEntity($scope->id) : $scope;
467        $queueList = $this->readQueueList($scope->id, $dateTime, $resolveReferences);
468        $timeAverage = $scope->getPreference('queue', 'processingTimeAverage');
469        $workstationCount = $scope->getCalculatedWorkstationCount();
470        return $queueList->withEstimatedWaitingTime($timeAverage, $workstationCount, $dateTime);
471    }
472
473    /**
474     * get list of scopes with admin
475     *
476     * * @param
477     * scopeId
478     * now
479     *
480     * @return number
481     */
482    public function readListWithScopeAdminEmail($resolveReferences = 0)
483    {
484        $scopeList = new Collection();
485        $query = new Query\Scope(Query\Base::SELECT);
486        $query
487            ->addEntityMapping()
488            ->addConditionWithAdminEmail()
489            ->addResolvedReferences($resolveReferences);
490        $result = $this->fetchList($query, new Entity());
491        if (count($result)) {
492            foreach ($result as $entity) {
493                if ($entity instanceof Entity) {
494                    $entity = $this->readResolvedReferences($entity, $resolveReferences);
495                    $scopeList->addEntity($entity);
496                }
497            }
498        }
499        return $scopeList;
500    }
501
502    /**
503     * write a scope
504     *
505     * @return Entity
506     */
507    public function writeEntity(\BO\Zmsentities\Scope $entity, $parentId)
508    {
509        self::$cache = [];
510        $query = new Query\Scope(Query\Base::INSERT);
511        $values = $query->reverseEntityMapping($entity, $parentId);
512        $query->addValues($values);
513        $this->writeItem($query);
514        $lastInsertId = $this->getWriter()
515            ->lastInsertId();
516        $this->replacePreferences($entity);
517
518        $this->removeCache($entity);
519
520        return $this->readEntity($lastInsertId);
521    }
522
523    /**
524     * update a scope
525     *
526     * @param
527     *            scopeId
528     *
529     * @return Entity
530     */
531    public function updateEntity($scopeId, \BO\Zmsentities\Scope $entity, $resolveReferences = 0)
532    {
533        self::$cache = [];
534        $query = new Query\Scope(Query\Base::UPDATE);
535        $query->addConditionScopeId($scopeId);
536        $values = $query->reverseEntityMapping($entity);
537        $query->addValues($values);
538        $this->writeItem($query);
539        $this->replacePreferences($entity);
540
541        $this->removeCache($entity);
542
543        return $this->readEntity($scopeId, $resolveReferences, true);
544    }
545
546    public function replacePreferences(\BO\Zmsentities\Scope $entity)
547    {
548        if (isset($entity['preferences'])) {
549            $preferenceQuery = new Preferences();
550            $entityName = 'scope';
551            $entityId = $entity['id'];
552            foreach ($entity['preferences'] as $groupName => $groupValues) {
553                foreach ($groupValues as $name => $value) {
554                    $preferenceQuery->replaceProperty($entityName, $entityId, $groupName, $name, $value);
555                }
556            }
557
558            $this->removeCache($entity);
559        }
560    }
561
562    /**
563     * update ghostWorkstationCount
564     *
565     * @param
566     *         scopeId
567     *         entity
568     *         dateTime (now)
569     *
570     * @return Entity
571     */
572    public function updateGhostWorkstationCount(\BO\Zmsentities\Scope $entity, \DateTimeInterface $dateTime)
573    {
574        $query = new Query\Scope(Query\Base::UPDATE);
575        $query->addConditionScopeId($entity->id);
576        $values = $query->setGhostWorkstationCountEntityMapping($entity, $dateTime);
577        $query->addValues($values);
578        $this->writeItem($query);
579
580        $this->removeCache($entity);
581
582        return $entity;
583    }
584
585    /**
586     * update emergency
587     *
588     * @param
589     *         scopeId
590     *         entity
591     *
592     * @return Entity
593     */
594    public function updateEmergency($scopeId, \BO\Zmsentities\Scope $entity)
595    {
596        self::$cache = [];
597        $query = new Query\Scope(Query\Base::UPDATE);
598        $query->addConditionScopeId($scopeId);
599        $values = $query->setEmergencyEntityMapping($entity);
600        $query->addValues($values);
601        $this->writeItem($query);
602
603        $this->removeCache($entity);
604
605        return $this->readEntity($scopeId, 0, true);
606    }
607
608    /**
609     * update image data for call display image
610     *
611     * @param
612     *         scopeId
613     *         Mimepart entity
614     *
615     * @return Mimepart entity
616     */
617    public function writeImageData($scopeId, \BO\Zmsentities\Mimepart $entity)
618    {
619        if ($entity->mime && $entity->content) {
620            $this->deleteImage($scopeId);
621            $extension = $entity->getExtension();
622            if ($extension == 'jpeg') {
623                $extension = 'jpg'; //compatibility ZMS1
624            }
625            $imageName = 's_' . $scopeId . '_bild.' . $extension;
626            $this->getWriter()->perform(
627                (new Query\Scope(Query\Base::REPLACE))->getQueryWriteImageData(),
628                array(
629                    'imagename' => $imageName,
630                    'imagedata' => $entity->content
631                )
632            );
633        }
634        $entity->id = $scopeId;
635        return $entity;
636    }
637
638    /**
639     * read image data
640     *
641     * @param
642     *         scopeId
643     *
644     * @return Mimepart entity
645     */
646    public function readImageData($scopeId)
647    {
648        $imageName = 's_' . $scopeId . '_bild';
649        $imageData = new \BO\Zmsentities\Mimepart();
650        $fileData = $this->getReader()->fetchAll(
651            (new Query\Scope(Query\Base::SELECT))->getQueryReadImageData(),
652            ['imagename' => "$imageName%"]
653        );
654        if ($fileData) {
655            $imageData->content = $fileData[0]['imagecontent'];
656            $imageData->mime = pathinfo($fileData[0]['imagename'])['extension'];
657        }
658        return $imageData;
659    }
660
661    /**
662     * delete image data for call display image
663     *
664     * @param
665     *         scopeId
666     *
667     * @return Status
668     */
669    public function deleteImage($scopeId)
670    {
671        $imageName = 's_' . $scopeId . '_bild';
672        return $this->perform((new Query\Scope(Query\Base::DELETE))->getQueryDeleteImage(), array(
673            'imagename' => "$imageName%"
674        ));
675    }
676
677    /**
678     * remove a scope
679     *
680     * @param
681     *            scopeId
682     *
683     * @return Resource Status
684     */
685    public function deleteEntity($scopeId)
686    {
687        $processListCount = (new Process())->readProcessListCountByScope($scopeId);
688        if (0 < $processListCount) {
689            throw new Exception\Scope\ScopeHasProcesses();
690        }
691        self::$cache = [];
692        $entity = $this->readEntity($scopeId);
693        $query = new Query\Scope(Query\Base::DELETE);
694        $query->addConditionScopeId($scopeId);
695        $this->deletePreferences($entity);
696
697        $this->removeCache($entity);
698
699        return ($this->deleteItem($query)) ? $entity : null;
700    }
701
702    public function deletePreferences(\BO\Zmsentities\Scope $entity)
703    {
704        $preferenceQuery = new Preferences();
705        $entityName = 'scope';
706        $entityId = $entity['id'];
707        foreach ($entity['preferences'] as $groupName => $groupValues) {
708            foreach (array_keys($groupValues) as $name) {
709                $preferenceQuery->deleteProperty($entityName, $entityId, $groupName, $name);
710            }
711        }
712
713        $this->removeCache($entity);
714    }
715
716    public function removeCache($scope)
717    {
718        if (!App::$cache) {
719            return;
720        }
721
722        if (isset($scope->provider) && isset($this->provider->id)) {
723            if (App::$cache->has('scopeReadByProviderId-' . $scope->getProviderId() . '-0')) {
724                App::$cache->delete('scopeReadByProviderId-' . $scope->getProviderId() . '-0');
725            }
726
727            if (App::$cache->has('scopeReadByProviderId-' . $scope->getProviderId() . '-1')) {
728                App::$cache->delete('scopeReadByProviderId-' . $scope->getProviderId() . '-1');
729            }
730
731            if (App::$cache->has('scopeReadByProviderId-' . $scope->getProviderId() . '-2')) {
732                App::$cache->delete('scopeReadByProviderId-' . $scope->getProviderId() . '-2');
733            }
734        }
735
736        if (isset($scope->id)) {
737            if (App::$cache->has("scope-$scope->id-0")) {
738                App::$cache->delete("scope-$scope->id-0");
739            }
740
741            if (App::$cache->has("scope-$scope->id-1")) {
742                App::$cache->delete("scope-$scope->id-1");
743            }
744
745            if (App::$cache->has("scope-$scope->id-2")) {
746                App::$cache->delete("scope-$scope->id-2");
747            }
748        }
749    }
750}