Lines n/a 0 / 0
Functions and Methods n/a 0 / 0
Classes and Traits n/a 0 / 0
Name Lines Functions and Methods CRAP Classes and Traits
Zmsdldb n/a 0 / 0 n/a 0 / 0 25 n/a 0 / 0
 getFixturesImportPath n/a 0 / 0 n/a 0 / 0 1
 setImportPath n/a 0 / 0 n/a 0 / 0 1
 startImport n/a 0 / 0 n/a 0 / 0 4
 writeRequestList n/a 0 / 0 n/a 0 / 0 7
 writeProviderList n/a 0 / 0 n/a 0 / 0 2
 updateAvailability n/a 0 / 0 n/a 0 / 0 6
 writeRequestRelationList n/a 0 / 0 n/a 0 / 0 2
 writeLastUpdate n/a 0 / 0 n/a 0 / 0 2
1<?php
2
3namespace BO\Zmsbackend\Source;
4
5/**
6 * @codeCoverageIgnore
7 */
8class Zmsdldb extends \BO\Zmsbackend\Base
9{
10    public static $importPath = '';
11    public static $repository = null;
12    public static $verbose = false;
13
14    /**
15     * @psalm-api
16     *
17     * @return false|string
18     */
19    public static function getFixturesImportPath(): string|false
20    {
21        $dir = dirname(__FILE__);
22        $importPath = realpath($dir . '/../../../tests/Zmsbackend/Service/fixtures/');
23        return $importPath;
24    }
25
26    /**
27     * @psalm-api
28     */
29    public static function setImportPath(string $path): void
30    {
31        self::$importPath = $path;
32    }
33
34    /**
35     * @return void
36     */
37    public function startImport(bool $verbose = true, bool $updateAvailability = true)
38    {
39        if (!static::$importPath) {
40            throw new \Exception('No data path given');
41        }
42        if ($verbose) {
43            self::$verbose = $verbose;
44            print("Use source-path for dldb: " . static::$importPath . "\n\n");
45        }
46        self::$repository = new \BO\Zmsdldb\FileAccess();
47        self::$repository->loadFromPath(static::$importPath);
48
49        \BO\Zmsbackend\Connection\Select::setTransaction();
50        $this->writeRequestList();
51        $providers = $this->writeProviderList();
52        $this->writeRequestRelationList();
53        $this->writeLastUpdate($verbose);
54        \BO\Zmsbackend\Connection\Select::writeCommit();
55
56        if ($updateAvailability) {
57            $this->updateAvailability($providers);
58        }
59    }
60
61    protected function writeRequestList(): void
62    {
63        $startTime = microtime(true);
64        $requestQuery = (new \BO\Zmsbackend\Request\Service\Request());
65        $requestQuery->writeDeleteListBySource('dldb');
66        foreach (self::$repository->fromService()->fetchList() as $request) {
67            if (isset($request['relation']) && isset($request['relation']['root_topic'])) {
68                $topic = self::$repository->fromTopic()->fetchId($request['relation']['root_topic']);
69                if ($topic && isset($topic['name'])) {
70                    $request['group'] = $topic['name'];
71                }
72            }
73            $requestQuery->writeImportEntity($request);
74        }
75        $time = round(microtime(true) - $startTime, 3);
76        if (self::$verbose) {
77            print("Requests: Took $time seconds\n\n");
78        }
79    }
80
81    protected function writeProviderList()
82    {
83        $startTime = microtime(true);
84        (new \BO\Zmsbackend\Provider\Service\Provider())->writeDeleteListBySource('dldb');
85        $providers = (new \BO\Zmsbackend\Provider\Service\Provider())->writeImportList(self::$repository->fromLocation()->fetchList());
86
87        $time = round(microtime(true) - $startTime, 3);
88        if (self::$verbose) {
89            print("Provider: Took $time seconds\n\n");
90        }
91
92        return $providers;
93    }
94
95    protected function updateAvailability(\BO\Zmsentities\Collection\ProviderList $providers): void
96    {
97        foreach ($providers as $provider) {
98            $newSlotMinutes = (int) ($provider->data['slotTimeInMinutes'] ?? 0);
99            if ($newSlotMinutes <= 0) {
100                continue;
101            }
102            $scopes = (new \BO\Zmsbackend\Scope\Service\Scope())->readByProviderId($provider->getId());
103            foreach ($scopes as $scope) {
104                $availabilities = (new \BO\Zmsbackend\Availability\Service\Availability())->readList($scope->getId());
105
106                foreach ($availabilities as $availability) {
107                    if ((int) $availability->slotTimeInMinutes === $newSlotMinutes) {
108                        continue;
109                    }
110
111                    $availability->slotTimeInMinutes = $newSlotMinutes;
112                    $availability->version = $availability->version + 1;
113                    $updated = (new \BO\Zmsbackend\Availability\Service\Availability())
114                        ->updateEntity($availability->getId(), $availability, 2);
115                    (new \BO\Zmsbackend\Availability\Service\AvailabilityHistory())
116                        ->writeDldbSlotUpdate($updated);
117                }
118            }
119        }
120    }
121
122    protected function writeRequestRelationList(): void
123    {
124        $startTime = microtime(true);
125        (new \BO\Zmsbackend\RequestRelation\Service\RequestRelation())->writeDeleteListBySource('dldb');
126        (new \BO\Zmsbackend\RequestRelation\Service\RequestRelation())->writeImportList(self::$repository->fromLocation()->fetchList());
127        $time = round(microtime(true) - $startTime, 3);
128        if (self::$verbose) {
129            print("RequestRelation: Took $time seconds\n\n");
130        }
131    }
132
133    protected function writeLastUpdate(): void
134    {
135        $startTime = microtime(true);
136        (new \BO\Zmsbackend\Config\Service\Config())->replaceProperty('sources_dldb_last', date('c'));
137        $time = round(microtime(true) - $startTime, 3);
138        if (self::$verbose) {
139            print("LastImportTimeToConfig: Took $time seconds\n\n");
140        }
141    }
142}