Lines 41.17% 28 / 68
Functions and Methods 14.28% 1 / 7
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Closure 41.17% 28 / 68 14.28% 1 / 7 92.48 0.00% 0 / 1
 readByScopeId 82.35% 14 / 17 0.00% 0 / 1 8.35
 readByScopeIdAndDate 0.00% 0 / 5 0.00% 0 / 1 2
 readByScopesInRange 100.00% 14 / 14 100.00% 1 / 1 2
 deleteEntity 0.00% 0 / 5 0.00% 0 / 1 2
 createOne 0.00% 0 / 13 0.00% 0 / 1 2
 invalidateCaches 0.00% 0 / 10 0.00% 0 / 1 30
 readEntity 0.00% 0 / 4 0.00% 0 / 1 2
1<?php
2
3namespace BO\Zmsbackend\Availability\Service;
4
5use BO\Zmsbackend\Application as App;
6use BO\Zmsentities\Closure as Entity;
7use BO\Zmsentities\Collection\ClosureList as Collection;
8use DateTime;
9
10class Closure extends \BO\Zmsbackend\Base
11{
12    public function readByScopeId(int $scopeId = 0, bool $disableCache = false)
13    {
14        $cacheKey = "closuresByScope-$scopeId";
15
16        if (!$disableCache && App::$cache) {
17            $data = App::$cache->get($cacheKey);
18            if (!empty($data)) {
19                return $data;
20            }
21        }
22
23        $closureList = new Collection();
24        $query = new \BO\Zmsbackend\Availability\Repository\Closure(\BO\Zmsbackend\Query\Base::SELECT);
25        $query->addEntityMapping()
26            ->addConditionScopeId($scopeId);
27        $result = $this->fetchList($query, new Entity());
28        if (count($result)) {
29            foreach ($result as $entity) {
30                if ($entity instanceof Entity) {
31                    $closureList->addEntity($entity);
32                }
33            }
34        }
35
36        if (App::$cache) {
37            App::$cache->set($cacheKey, $closureList);
38        }
39
40        return $closureList;
41    }
42
43    public function readByScopeIdAndDate(string|int $scopeId, DateTime $date): Entity
44    {
45        $query = new \BO\Zmsbackend\Availability\Repository\Closure(\BO\Zmsbackend\Query\Base::SELECT);
46        $query->addEntityMapping()
47            ->addConditionScopeId($scopeId)
48            ->addConditionDate($date);
49
50        return $this->fetchOne($query, new \BO\Zmsentities\Closure());
51    }
52
53    public function readByScopesInRange(
54        array $scopeIds,
55        \DateTimeInterface $from,
56        \DateTimeInterface $until
57    ): array {
58        $query = (new \BO\Zmsbackend\Availability\Repository\Closure(\BO\Zmsbackend\Query\Base::SELECT))
59            ->addEntityMapping()
60            ->addSelectVirtualDate()
61            ->addConditionScopeIds($scopeIds)
62            ->addConditionDateRange($from, $until);
63
64        $entities = $this->fetchList($query, new Entity());
65
66        $result  = [];
67        foreach ($entities as $entity) {
68            $date = $entity->date ?? $entity->getDateTime()->format('Y-m-d');
69            $result [] = [
70                'scopeId' => (int) $entity->scopeId,
71                'date'    => (string) $date,
72            ];
73        }
74        return $result ;
75    }
76
77    public function deleteEntity(\BO\Zmsentities\Closure $closure): bool
78    {
79        $query = new \BO\Zmsbackend\Availability\Repository\Closure(\BO\Zmsbackend\Query\Base::DELETE);
80        $query->addConditionId($closure->getId());
81
82        $deleted = $this->deleteItem($query);
83        $this->invalidateCaches((int) $closure->scopeId);
84
85        return $deleted;
86    }
87
88    public function createOne(string|int $scopeId, DateTime $date)
89    {
90        $query = new \BO\Zmsbackend\Availability\Repository\Closure(\BO\Zmsbackend\Query\Base::INSERT);
91        $query->addValues(
92            array(
93                'StandortID' => $scopeId,
94                'year' => (int) $date->format('Y'),
95                'month' => (int) $date->format('m'),
96                'day' => (int) $date->format('d')
97            )
98        );
99
100        $this->writeItem($query);
101        $id = $this->getWriter()->lastInsertId();
102        $this->invalidateCaches((int) $scopeId);
103
104        return $this->readEntity($id);
105    }
106
107    /**
108     * Clear closure list cache and scope caches that embed closures
109     * (scope-{id}-{resolveReferences}, department/cluster lists, in-process static).
110     *
111     * Always delete scope-* keys (do not gate on CacheInterface::has()): some adapters
112     * return false for existing keys, which would leave stale scope.closure for the
113     * month overview after toggle.
114     */
115    protected function invalidateCaches(int $scopeId): void
116    {
117        if ($scopeId < 1) {
118            return;
119        }
120
121        if (App::$cache) {
122            App::$cache->delete('closuresByScope-' . $scopeId);
123            for ($resolveReferences = 0; $resolveReferences <= 3; $resolveReferences++) {
124                App::$cache->delete("scope-{$scopeId}-{$resolveReferences}");
125            }
126        }
127
128        $scope = new \BO\Zmsentities\Scope(['id' => $scopeId]);
129        (new \BO\Zmsbackend\Scope\Service\Scope())->removeCacheByContext($scope);
130
131        for ($resolveReferences = 0; $resolveReferences <= 3; $resolveReferences++) {
132            unset(\BO\Zmsbackend\Scope\Service\Scope::$cache["{$scopeId}-{$resolveReferences}"]);
133        }
134    }
135
136    public function readEntity(string|false $id): Entity
137    {
138        $query = new \BO\Zmsbackend\Availability\Repository\Closure(\BO\Zmsbackend\Query\Base::SELECT);
139        $query->addEntityMapping()
140            ->addConditionId($id);
141
142        return $this->fetchOne($query, new \BO\Zmsentities\Closure());
143    }
144}