Lines
95.72%
112 / 117
Methods
94.73%
18 / 19
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| 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 | ||
| [BO\Zmsbackend\Base] __construct | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] init | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] getWriter | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsbackend\Base] getReader | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | ||
| [BO\Zmsbackend\Base] fetchPreparedStatement | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 3 | ||
| [BO\Zmsbackend\Base] startExecute | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] pdoExceptionHandler | 100.00% | 13 / 13 | 100.00% | 1 / 1 | 4 | ||
| [BO\Zmsbackend\Base] fetchStatement | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] fetchOne | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsbackend\Base] fetchRow | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] fetchValue | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] fetchAll | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] fetchHandle | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] fetchList | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsbackend\Base] writeItem | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] deleteItem | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] perform | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] fetchAffected | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] readResolvedReferences | n/a | 0 / 0 | n/a | 0 / 0 | 1 | ||
| [BO\Zmsbackend\Base] hashStringValue | 50.00% | 5 / 10 | 0.00% | 0 / 1 | 8.12 | ||
| 8 | class 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(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($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 | } |
Inherited from BO\Zmsbackend\Base
| 38 | public function __construct(?Connection\Pdo $writeConnection = null, ?Connection\Pdo $readConnection = null) | |
| 39 | { | |
| 40 | $this->writeDb = $writeConnection; | |
| 41 | $this->readDb = $readConnection; | |
| 42 | } |
| 44 | public static function init(?Connection\Pdo $writeConnection = null, ?Connection\Pdo $readConnection = null): static | |
| 45 | { | |
| 46 | $instance = new static($writeConnection, $readConnection); | |
| 47 | return $instance; | |
| 48 | } |
| 50 | public function getWriter(): Connection\Pdo | |
| 51 | { | |
| 52 | if (null === $this->writeDb) { | |
| 53 | $this->writeDb = Connection\Select::getWriteConnection(); | |
| 54 | $this->readDb = $this->writeDb; | |
| 55 | } | |
| 56 | return $this->writeDb; | |
| 57 | } |
| 59 | public function getReader(): Connection\Pdo | |
| 60 | { | |
| 61 | if (null !== $this->writeDb) { | |
| 62 | // if readDB gets a reset, still use writeDB | |
| 63 | return $this->writeDb; | |
| 64 | } | |
| 65 | if (null === $this->readDb) { | |
| 66 | $this->readDb = Connection\Select::getReadConnection(); | |
| 67 | } | |
| 68 | return $this->readDb; | |
| 69 | } |
| 71 | public function fetchPreparedStatement(Query\Base|string $query) | |
| 72 | { | |
| 73 | $sql = "$query"; | |
| 74 | $reader = $this->getReader(); | |
| 75 | if (spl_object_hash($reader) != static::$preparedConnectionId) { | |
| 76 | // do not use prepared statements on a different connection | |
| 77 | static::$preparedCache = []; | |
| 78 | static::$preparedConnectionId = spl_object_hash($reader); | |
| 79 | } | |
| 80 | if (!isset(static::$preparedCache[$sql])) { | |
| 81 | $prepared = $this->getReader()->prepare($sql); | |
| 82 | static::$preparedCache[$sql] = $prepared; | |
| 83 | } | |
| 84 | return static::$preparedCache[$sql]; | |
| 85 | } |
| 87 | public function startExecute($statement, array $parameters) | |
| 88 | { | |
| 89 | $statement = static::pdoExceptionHandler(function () use ($statement, $parameters) { | |
| 90 | $statement->execute($parameters); | |
| 91 | return $statement; | |
| 92 | }); | |
| 93 | return $statement; | |
| 94 | } |
| 96 | protected static function pdoExceptionHandler(\Closure $pdoFunction, $parameters = []) | |
| 97 | { | |
| 98 | try { | |
| 99 | $statement = $pdoFunction($parameters); | |
| 100 | } catch (\PDOException $pdoException) { | |
| 101 | if (stripos($pdoException->getMessage(), 'Lock wait timeout') !== false) { | |
| 102 | throw new \BO\Zmsbackend\Exception\Pdo\LockTimeout(); | |
| 103 | } | |
| 104 | //@codeCoverageIgnoreStart | |
| 105 | if (stripos($pdoException->getMessage(), 'Deadlock found') !== false) { | |
| 106 | throw new \BO\Zmsbackend\Exception\Pdo\DeadLockFound(); | |
| 107 | } | |
| 108 | //@codeCoverageIgnoreEnd | |
| 109 | $message = "SQL: " | |
| 110 | . " Err: " | |
| 111 | . $pdoException->getMessage() | |
| 112 | //. " || Statement: " | |
| 113 | //.$statement->queryString | |
| 114 | //." || Parameters=". var_export($parameters, true) | |
| 115 | ; | |
| 116 | throw new \BO\Zmsbackend\Exception\Pdo\PDOFailed($message, 0, $pdoException); | |
| 117 | } | |
| 118 | return $statement; | |
| 119 | } |
| 121 | public function fetchStatement(Query\Base $query) | |
| 122 | { | |
| 123 | $parameters = $query->getParameters(); | |
| 124 | $statement = $this->startExecute($this->fetchPreparedStatement($query), $parameters); | |
| 125 | return $statement; | |
| 126 | } |
| 133 | public function fetchOne(Query\Base $query, \BO\Zmsentities\Schema\Entity $entity) | |
| 134 | { | |
| 135 | $statement = $this->fetchStatement($query); | |
| 136 | $data = $statement->fetch(\PDO::FETCH_ASSOC); | |
| 137 | if ($data) { | |
| 138 | $entity->exchangeArray($query->postProcessJoins($data)); | |
| 139 | $entity->setResolveLevel($query->getResolveLevel()); | |
| 140 | } | |
| 141 | $statement->closeCursor(); | |
| 142 | return $entity; | |
| 143 | } |
| 145 | public function fetchRow(Query\Base|string $query, array|null $parameters = null) | |
| 146 | { | |
| 147 | return static::pdoExceptionHandler(function () use ($query, $parameters) { | |
| 148 | $prepared = $this->fetchPreparedStatement($query); | |
| 149 | $prepared->execute($parameters); | |
| 150 | $row = $prepared->fetch(\PDO::FETCH_ASSOC); | |
| 151 | $prepared->closeCursor(); | |
| 152 | return $row; | |
| 153 | }); | |
| 154 | } |
| 156 | public function fetchValue(Query\Base|string $query, array|null $parameters = null) | |
| 157 | { | |
| 158 | return static::pdoExceptionHandler(function () use ($query, $parameters) { | |
| 159 | $prepared = $this->fetchPreparedStatement($query); | |
| 160 | $prepared->execute($parameters); | |
| 161 | $value = $prepared->fetchColumn(); | |
| 162 | $prepared->closeCursor(); | |
| 163 | return $value; | |
| 164 | }); | |
| 165 | } |
| 167 | public function fetchAll(Query\Base|string $query, array|null $parameters = null) | |
| 168 | { | |
| 169 | return static::pdoExceptionHandler(function () use ($query, $parameters) { | |
| 170 | $prepared = $this->fetchPreparedStatement($query); | |
| 171 | $prepared->execute($parameters); | |
| 172 | $list = $prepared->fetchAll(\PDO::FETCH_ASSOC); | |
| 173 | $prepared->closeCursor(); | |
| 174 | return $list; | |
| 175 | }); | |
| 176 | } |
| 178 | public function fetchHandle(Query\Base|string $query, array|null $parameters = null) | |
| 179 | { | |
| 180 | return static::pdoExceptionHandler(function () use ($query, $parameters) { | |
| 181 | $prepared = $this->fetchPreparedStatement($query); | |
| 182 | $prepared->execute($parameters); | |
| 183 | return $prepared; | |
| 184 | }); | |
| 185 | } |
| 187 | public function fetchList(Query\Base $query, \BO\Zmsentities\Schema\Entity $entity, array|\ArrayAccess $resultList = []) | |
| 188 | { | |
| 189 | $statement = $this->fetchStatement($query); | |
| 190 | while ($data = $statement->fetch(\PDO::FETCH_ASSOC)) { | |
| 191 | $dataEntity = clone $entity; | |
| 192 | $dataEntity->exchangeArray($query->postProcessJoins($data)); | |
| 193 | $dataEntity->setResolveLevel($query->getResolveLevel()); | |
| 194 | $resultList[] = $dataEntity; | |
| 195 | } | |
| 196 | $statement->closeCursor(); | |
| 197 | return $resultList; | |
| 198 | } |
| 206 | public function writeItem(Query\Base $query) | |
| 207 | { | |
| 208 | return static::pdoExceptionHandler(function () use ($query) { | |
| 209 | $this->getWriter(); //Switch to writer for write/delete | |
| 210 | $statement = $this->fetchPreparedStatement($query); | |
| 211 | $status = $statement->execute($query->getParameters()); | |
| 212 | $statement->closeCursor(); | |
| 213 | return $status; | |
| 214 | }); | |
| 215 | } |
| 221 | public function deleteItem(Query\Base $query) | |
| 222 | { | |
| 223 | return $this->writeItem($query); | |
| 224 | } |
| 226 | public function perform(string $sql, array|null $parameters = null) | |
| 227 | { | |
| 228 | return static::pdoExceptionHandler(function () use ($sql, $parameters) { | |
| 229 | $this->getWriter(); //Switch to writer for perform | |
| 230 | $prepared = $this->fetchPreparedStatement($sql); | |
| 231 | $status = $prepared->execute($parameters); | |
| 232 | $prepared->closeCursor(); | |
| 233 | return $status; | |
| 234 | }); | |
| 235 | } |
| 237 | public function fetchAffected(string $sql, array|null $parameters = null) | |
| 238 | { | |
| 239 | return static::pdoExceptionHandler(function () use ($sql, $parameters) { | |
| 240 | $this->getWriter(); //Switch to writer for perform | |
| 241 | $prepared = $this->fetchPreparedStatement($sql); | |
| 242 | $prepared->execute($parameters); | |
| 243 | $count = $prepared->rowCount(); | |
| 244 | $prepared->closeCursor(); | |
| 245 | return $count; | |
| 246 | }); | |
| 247 | } |
| 256 | public function readResolvedReferences(\BO\Zmsentities\Schema\Entity $entity, int $resolveReferences) | |
| 257 | { | |
| 258 | return $entity; | |
| 259 | } |
| 268 | public function hashStringValue(string $value, ?callable $callable = null): string | |
| 269 | { | |
| 270 | if ($callable === null) { | |
| 271 | $callable = 'sha1'; | |
| 272 | } | |
| 273 | ||
| 274 | if (is_callable($callable)) { | |
| 275 | $hash = $callable($value); | |
| 276 | ||
| 277 | if (is_string($callable)) { | |
| 278 | return $callable . ':' . $hash; | |
| 279 | } | |
| 280 | if ($callable instanceof \Closure) { | |
| 281 | return 'closure:' . $hash; | |
| 282 | } | |
| 283 | // else | |
| 284 | return 'custom:' . $hash; | |
| 285 | } | |
| 286 | ||
| 287 | throw new \InvalidArgumentException('hashStringValue() should be called with a callable as second parameter.'); | |
| 288 | } |