Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
n/a
0 / 0
n/a
0 / 0
CRAP
n/a
0 / 0
Zmsdldb
n/a
0 / 0
n/a
0 / 0
24
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
5
 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($path): void
30    {
31        self::$importPath = $path;
32    }
33
34    /**
35     * @return void
36     */
37    public function startImport($verbose = true, $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($providers): void
96    {
97        foreach ($providers as $provider) {
98            $providerData = $provider->data;
99
100            $scopes = (new \BO\Zmsbackend\Scope\Service\Scope())->readByProviderId($provider->getId());
101            foreach ($scopes as $scope) {
102                $availabilities = (new \BO\Zmsbackend\Availability\Service\Availability())->readList($scope->getId());
103
104                foreach ($availabilities as $availability) {
105                    if ((int) $availability->slotTimeInMinutes === (int) $providerData['slotTimeInMinutes']) {
106                        continue;
107                    }
108
109                    $availability->slotTimeInMinutes = $providerData['slotTimeInMinutes'];
110                    $availability->version = $availability->version + 1;
111                    $updated = (new \BO\Zmsbackend\Availability\Service\Availability())
112                        ->updateEntity($availability->getId(), $availability, 2);
113                    (new \BO\Zmsbackend\Availability\Service\AvailabilityHistory())
114                        ->writeDldbSlotUpdate($updated);
115                }
116            }
117        }
118    }
119
120    protected function writeRequestRelationList(): void
121    {
122        $startTime = microtime(true);
123        (new \BO\Zmsbackend\RequestRelation\Service\RequestRelation())->writeDeleteListBySource('dldb');
124        (new \BO\Zmsbackend\RequestRelation\Service\RequestRelation())->writeImportList(self::$repository->fromLocation()->fetchList());
125        $time = round(microtime(true) - $startTime, 3);
126        if (self::$verbose) {
127            print("RequestRelation: Took $time seconds\n\n");
128        }
129    }
130
131    protected function writeLastUpdate(): void
132    {
133        $startTime = microtime(true);
134        (new \BO\Zmsbackend\Config\Service\Config())->replaceProperty('sources_dldb_last', date('c'));
135        $time = round(microtime(true) - $startTime, 3);
136        if (self::$verbose) {
137            print("LastImportTimeToConfig: Took $time seconds\n\n");
138        }
139    }
140}