| Total Complexity | 76 |
| Total Lines | 667 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Manager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 63 | class Manager implements IManager { |
||
| 64 | |||
| 65 | /** @var IStorage */ |
||
| 66 | protected $storage; |
||
| 67 | |||
| 68 | /** @var string */ |
||
| 69 | protected $path; |
||
| 70 | |||
| 71 | /** @var object */ |
||
| 72 | protected $entity; |
||
| 73 | |||
| 74 | /** @var array[] */ |
||
| 75 | protected $operations = []; |
||
| 76 | |||
| 77 | /** @var array[] */ |
||
| 78 | protected $checks = []; |
||
| 79 | |||
| 80 | /** @var IDBConnection */ |
||
| 81 | protected $connection; |
||
| 82 | |||
| 83 | /** @var IServerContainer|\OC\Server */ |
||
| 84 | protected $container; |
||
| 85 | |||
| 86 | /** @var IL10N */ |
||
| 87 | protected $l; |
||
| 88 | |||
| 89 | /** @var LegacyDispatcher */ |
||
| 90 | protected $legacyEventDispatcher; |
||
| 91 | |||
| 92 | /** @var IEntity[] */ |
||
| 93 | protected $registeredEntities = []; |
||
| 94 | |||
| 95 | /** @var IOperation[] */ |
||
| 96 | protected $registeredOperators = []; |
||
| 97 | |||
| 98 | /** @var ICheck[] */ |
||
| 99 | protected $registeredChecks = []; |
||
| 100 | |||
| 101 | /** @var ILogger */ |
||
| 102 | protected $logger; |
||
| 103 | |||
| 104 | /** @var CappedMemoryCache */ |
||
| 105 | protected $operationsByScope = []; |
||
| 106 | |||
| 107 | /** @var IUserSession */ |
||
| 108 | protected $session; |
||
| 109 | |||
| 110 | /** @var IEventDispatcher */ |
||
| 111 | private $dispatcher; |
||
| 112 | |||
| 113 | /** @var IConfig */ |
||
| 114 | private $config; |
||
| 115 | |||
| 116 | public function __construct( |
||
| 117 | IDBConnection $connection, |
||
| 118 | IServerContainer $container, |
||
| 119 | IL10N $l, |
||
| 120 | LegacyDispatcher $eventDispatcher, |
||
| 121 | ILogger $logger, |
||
| 122 | IUserSession $session, |
||
| 123 | IEventDispatcher $dispatcher, |
||
| 124 | IConfig $config |
||
| 125 | ) { |
||
| 126 | $this->connection = $connection; |
||
| 127 | $this->container = $container; |
||
| 128 | $this->l = $l; |
||
| 129 | $this->legacyEventDispatcher = $eventDispatcher; |
||
| 130 | $this->logger = $logger; |
||
| 131 | $this->operationsByScope = new CappedMemoryCache(64); |
||
| 132 | $this->session = $session; |
||
| 133 | $this->dispatcher = $dispatcher; |
||
| 134 | $this->config = $config; |
||
| 135 | } |
||
| 136 | |||
| 137 | public function getRuleMatcher(): IRuleMatcher { |
||
| 138 | return new RuleMatcher( |
||
| 139 | $this->session, |
||
| 140 | $this->container, |
||
| 141 | $this->l, |
||
| 142 | $this, |
||
| 143 | $this->container->query(Logger::class) |
||
| 144 | ); |
||
| 145 | } |
||
| 146 | |||
| 147 | public function getAllConfiguredEvents() { |
||
| 148 | $query = $this->connection->getQueryBuilder(); |
||
| 149 | |||
| 150 | $query->select('class', 'entity', $query->expr()->castColumn('events', IQueryBuilder::PARAM_STR)) |
||
| 151 | ->from('flow_operations') |
||
| 152 | ->where($query->expr()->neq('events', $query->createNamedParameter('[]'), IQueryBuilder::PARAM_STR)) |
||
| 153 | ->groupBy('class', 'entity', $query->expr()->castColumn('events', IQueryBuilder::PARAM_STR)); |
||
| 154 | |||
| 155 | $result = $query->execute(); |
||
| 156 | $operations = []; |
||
| 157 | while ($row = $result->fetch()) { |
||
| 158 | $eventNames = \json_decode($row['events']); |
||
| 159 | |||
| 160 | $operation = $row['class']; |
||
| 161 | $entity = $row['entity']; |
||
| 162 | |||
| 163 | $operations[$operation] = $operations[$row['class']] ?? []; |
||
| 164 | $operations[$operation][$entity] = $operations[$operation][$entity] ?? []; |
||
| 165 | |||
| 166 | $operations[$operation][$entity] = array_unique(array_merge($operations[$operation][$entity], $eventNames ?? [])); |
||
| 167 | } |
||
| 168 | $result->closeCursor(); |
||
| 169 | |||
| 170 | return $operations; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param string $operationClass |
||
| 175 | * @return ScopeContext[] |
||
| 176 | */ |
||
| 177 | public function getAllConfiguredScopesForOperation(string $operationClass): array { |
||
| 178 | static $scopesByOperation = []; |
||
| 179 | if (isset($scopesByOperation[$operationClass])) { |
||
| 180 | return $scopesByOperation[$operationClass]; |
||
| 181 | } |
||
| 182 | |||
| 183 | $query = $this->connection->getQueryBuilder(); |
||
| 184 | |||
| 185 | $query->selectDistinct('s.type') |
||
| 186 | ->addSelect('s.value') |
||
| 187 | ->from('flow_operations', 'o') |
||
| 188 | ->leftJoin('o', 'flow_operations_scope', 's', $query->expr()->eq('o.id', 's.operation_id')) |
||
| 189 | ->where($query->expr()->eq('o.class', $query->createParameter('operationClass'))); |
||
| 190 | |||
| 191 | $query->setParameters(['operationClass' => $operationClass]); |
||
| 192 | $result = $query->execute(); |
||
| 193 | |||
| 194 | $scopesByOperation[$operationClass] = []; |
||
| 195 | while ($row = $result->fetch()) { |
||
| 196 | $scope = new ScopeContext($row['type'], $row['value']); |
||
| 197 | $scopesByOperation[$operationClass][$scope->getHash()] = $scope; |
||
| 198 | } |
||
| 199 | |||
| 200 | return $scopesByOperation[$operationClass]; |
||
| 201 | } |
||
| 202 | |||
| 203 | public function getAllOperations(ScopeContext $scopeContext): array { |
||
| 204 | if (isset($this->operations[$scopeContext->getHash()])) { |
||
| 205 | return $this->operations[$scopeContext->getHash()]; |
||
| 206 | } |
||
| 207 | |||
| 208 | $query = $this->connection->getQueryBuilder(); |
||
| 209 | |||
| 210 | $query->select('o.*') |
||
| 211 | ->selectAlias('s.type', 'scope_type') |
||
| 212 | ->selectAlias('s.value', 'scope_actor_id') |
||
| 213 | ->from('flow_operations', 'o') |
||
| 214 | ->leftJoin('o', 'flow_operations_scope', 's', $query->expr()->eq('o.id', 's.operation_id')) |
||
| 215 | ->where($query->expr()->eq('s.type', $query->createParameter('scope'))); |
||
| 216 | |||
| 217 | if ($scopeContext->getScope() === IManager::SCOPE_USER) { |
||
| 218 | $query->andWhere($query->expr()->eq('s.value', $query->createParameter('scopeId'))); |
||
| 219 | } |
||
| 220 | |||
| 221 | $query->setParameters(['scope' => $scopeContext->getScope(), 'scopeId' => $scopeContext->getScopeId()]); |
||
| 222 | $result = $query->execute(); |
||
| 223 | |||
| 224 | $this->operations[$scopeContext->getHash()] = []; |
||
| 225 | while ($row = $result->fetch()) { |
||
| 226 | if (!isset($this->operations[$scopeContext->getHash()][$row['class']])) { |
||
| 227 | $this->operations[$scopeContext->getHash()][$row['class']] = []; |
||
| 228 | } |
||
| 229 | $this->operations[$scopeContext->getHash()][$row['class']][] = $row; |
||
| 230 | } |
||
| 231 | |||
| 232 | return $this->operations[$scopeContext->getHash()]; |
||
| 233 | } |
||
| 234 | |||
| 235 | public function getOperations(string $class, ScopeContext $scopeContext): array { |
||
| 236 | if (!isset($this->operations[$scopeContext->getHash()])) { |
||
| 237 | $this->getAllOperations($scopeContext); |
||
| 238 | } |
||
| 239 | return $this->operations[$scopeContext->getHash()][$class] ?? []; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param int $id |
||
| 244 | * @return array |
||
| 245 | * @throws \UnexpectedValueException |
||
| 246 | */ |
||
| 247 | protected function getOperation($id) { |
||
| 248 | $query = $this->connection->getQueryBuilder(); |
||
| 249 | $query->select('*') |
||
| 250 | ->from('flow_operations') |
||
| 251 | ->where($query->expr()->eq('id', $query->createNamedParameter($id))); |
||
| 252 | $result = $query->execute(); |
||
| 253 | $row = $result->fetch(); |
||
| 254 | $result->closeCursor(); |
||
| 255 | |||
| 256 | if ($row) { |
||
| 257 | return $row; |
||
| 258 | } |
||
| 259 | |||
| 260 | throw new \UnexpectedValueException($this->l->t('Operation #%s does not exist', [$id])); |
||
| 261 | } |
||
| 262 | |||
| 263 | protected function insertOperation( |
||
| 264 | string $class, |
||
| 265 | string $name, |
||
| 266 | array $checkIds, |
||
| 267 | string $operation, |
||
| 268 | string $entity, |
||
| 269 | array $events |
||
| 270 | ): int { |
||
| 271 | $query = $this->connection->getQueryBuilder(); |
||
| 272 | $query->insert('flow_operations') |
||
| 273 | ->values([ |
||
| 274 | 'class' => $query->createNamedParameter($class), |
||
| 275 | 'name' => $query->createNamedParameter($name), |
||
| 276 | 'checks' => $query->createNamedParameter(json_encode(array_unique($checkIds))), |
||
| 277 | 'operation' => $query->createNamedParameter($operation), |
||
| 278 | 'entity' => $query->createNamedParameter($entity), |
||
| 279 | 'events' => $query->createNamedParameter(json_encode($events)) |
||
| 280 | ]); |
||
| 281 | $query->execute(); |
||
| 282 | |||
| 283 | return $query->getLastInsertId(); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $class |
||
| 288 | * @param string $name |
||
| 289 | * @param array[] $checks |
||
| 290 | * @param string $operation |
||
| 291 | * @return array The added operation |
||
| 292 | * @throws \UnexpectedValueException |
||
| 293 | * @throws DBALException |
||
| 294 | */ |
||
| 295 | public function addOperation( |
||
| 296 | string $class, |
||
| 297 | string $name, |
||
| 298 | array $checks, |
||
| 299 | string $operation, |
||
| 300 | ScopeContext $scope, |
||
| 301 | string $entity, |
||
| 302 | array $events |
||
| 303 | ) { |
||
| 304 | $this->validateOperation($class, $name, $checks, $operation, $entity, $events); |
||
| 305 | |||
| 306 | $this->connection->beginTransaction(); |
||
| 307 | |||
| 308 | try { |
||
| 309 | $checkIds = []; |
||
| 310 | foreach ($checks as $check) { |
||
| 311 | $checkIds[] = $this->addCheck($check['class'], $check['operator'], $check['value']); |
||
| 312 | } |
||
| 313 | |||
| 314 | $id = $this->insertOperation($class, $name, $checkIds, $operation, $entity, $events); |
||
| 315 | $this->addScope($id, $scope); |
||
| 316 | |||
| 317 | $this->connection->commit(); |
||
| 318 | } catch (DBALException $e) { |
||
| 319 | $this->connection->rollBack(); |
||
| 320 | throw $e; |
||
| 321 | } |
||
| 322 | |||
| 323 | return $this->getOperation($id); |
||
| 324 | } |
||
| 325 | |||
| 326 | protected function canModify(int $id, ScopeContext $scopeContext):bool { |
||
| 327 | if (isset($this->operationsByScope[$scopeContext->getHash()])) { |
||
| 328 | return in_array($id, $this->operationsByScope[$scopeContext->getHash()], true); |
||
| 329 | } |
||
| 330 | |||
| 331 | $qb = $this->connection->getQueryBuilder(); |
||
| 332 | $qb = $qb->select('o.id') |
||
| 333 | ->from('flow_operations', 'o') |
||
| 334 | ->leftJoin('o', 'flow_operations_scope', 's', $qb->expr()->eq('o.id', 's.operation_id')) |
||
| 335 | ->where($qb->expr()->eq('s.type', $qb->createParameter('scope'))); |
||
| 336 | |||
| 337 | if ($scopeContext->getScope() !== IManager::SCOPE_ADMIN) { |
||
| 338 | $qb->where($qb->expr()->eq('s.value', $qb->createParameter('scopeId'))); |
||
| 339 | } |
||
| 340 | |||
| 341 | $qb->setParameters(['scope' => $scopeContext->getScope(), 'scopeId' => $scopeContext->getScopeId()]); |
||
| 342 | $result = $qb->execute(); |
||
| 343 | |||
| 344 | $this->operationsByScope[$scopeContext->getHash()] = []; |
||
| 345 | while ($opId = $result->fetchColumn(0)) { |
||
| 346 | $this->operationsByScope[$scopeContext->getHash()][] = (int)$opId; |
||
| 347 | } |
||
| 348 | $result->closeCursor(); |
||
| 349 | |||
| 350 | return in_array($id, $this->operationsByScope[$scopeContext->getHash()], true); |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param int $id |
||
| 355 | * @param string $name |
||
| 356 | * @param array[] $checks |
||
| 357 | * @param string $operation |
||
| 358 | * @return array The updated operation |
||
| 359 | * @throws \UnexpectedValueException |
||
| 360 | * @throws \DomainException |
||
| 361 | * @throws DBALException |
||
| 362 | */ |
||
| 363 | public function updateOperation( |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param int $id |
||
| 406 | * @return bool |
||
| 407 | * @throws \UnexpectedValueException |
||
| 408 | * @throws DBALException |
||
| 409 | * @throws \DomainException |
||
| 410 | */ |
||
| 411 | public function deleteOperation($id, ScopeContext $scopeContext) { |
||
| 438 | } |
||
| 439 | |||
| 440 | protected function validateEvents(string $entity, array $events, IOperation $operation) { |
||
| 468 | } |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @param string $class |
||
| 473 | * @param string $name |
||
| 474 | * @param array[] $checks |
||
| 475 | * @param string $operation |
||
| 476 | * @throws \UnexpectedValueException |
||
| 477 | */ |
||
| 478 | public function validateOperation($class, $name, array $checks, $operation, string $entity, array $events) { |
||
| 479 | try { |
||
| 480 | /** @var IOperation $instance */ |
||
| 481 | $instance = $this->container->query($class); |
||
| 482 | } catch (QueryException $e) { |
||
| 483 | throw new \UnexpectedValueException($this->l->t('Operation %s does not exist', [$class])); |
||
| 484 | } |
||
| 485 | |||
| 486 | if (!($instance instanceof IOperation)) { |
||
| 487 | throw new \UnexpectedValueException($this->l->t('Operation %s is invalid', [$class])); |
||
| 488 | } |
||
| 489 | |||
| 490 | $this->validateEvents($entity, $events, $instance); |
||
| 491 | |||
| 492 | if (count($checks) === 0) { |
||
| 493 | throw new \UnexpectedValueException($this->l->t('At least one check needs to be provided')); |
||
| 494 | } |
||
| 495 | |||
| 496 | if (strlen((string)$operation) > IManager::MAX_OPERATION_VALUE_BYTES) { |
||
| 497 | throw new \UnexpectedValueException($this->l->t('The provided operation data is too long')); |
||
| 498 | } |
||
| 499 | |||
| 500 | $instance->validateOperation($name, $checks, $operation); |
||
| 501 | |||
| 502 | foreach ($checks as $check) { |
||
| 503 | if (!is_string($check['class'])) { |
||
| 504 | throw new \UnexpectedValueException($this->l->t('Invalid check provided')); |
||
| 505 | } |
||
| 506 | |||
| 507 | try { |
||
| 508 | /** @var ICheck $instance */ |
||
| 509 | $instance = $this->container->query($check['class']); |
||
| 510 | } catch (QueryException $e) { |
||
| 511 | throw new \UnexpectedValueException($this->l->t('Check %s does not exist', [$class])); |
||
| 512 | } |
||
| 513 | |||
| 514 | if (!($instance instanceof ICheck)) { |
||
| 515 | throw new \UnexpectedValueException($this->l->t('Check %s is invalid', [$class])); |
||
| 516 | } |
||
| 517 | |||
| 518 | if (!empty($instance->supportedEntities()) |
||
| 519 | && !in_array($entity, $instance->supportedEntities()) |
||
| 520 | ) { |
||
| 521 | throw new \UnexpectedValueException($this->l->t('Check %s is not allowed with this entity', [$class])); |
||
| 522 | } |
||
| 523 | |||
| 524 | if (strlen((string)$check['value']) > IManager::MAX_CHECK_VALUE_BYTES) { |
||
| 525 | throw new \UnexpectedValueException($this->l->t('The provided check value is too long')); |
||
| 526 | } |
||
| 527 | |||
| 528 | $instance->validateCheck($check['operator'], $check['value']); |
||
| 529 | } |
||
| 530 | } |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @param int[] $checkIds |
||
| 534 | * @return array[] |
||
| 535 | */ |
||
| 536 | public function getChecks(array $checkIds) { |
||
| 571 | } |
||
| 572 | |||
| 573 | /** |
||
| 574 | * @param string $class |
||
| 575 | * @param string $operator |
||
| 576 | * @param string $value |
||
| 577 | * @return int Check unique ID |
||
| 578 | */ |
||
| 579 | protected function addCheck($class, $operator, $value) { |
||
| 580 | $hash = md5($class . '::' . $operator . '::' . $value); |
||
| 581 | |||
| 582 | $query = $this->connection->getQueryBuilder(); |
||
| 583 | $query->select('id') |
||
| 584 | ->from('flow_checks') |
||
| 585 | ->where($query->expr()->eq('hash', $query->createNamedParameter($hash))); |
||
| 586 | $result = $query->execute(); |
||
| 587 | |||
| 588 | if ($row = $result->fetch()) { |
||
| 589 | $result->closeCursor(); |
||
| 590 | return (int) $row['id']; |
||
| 591 | } |
||
| 592 | |||
| 593 | $query = $this->connection->getQueryBuilder(); |
||
| 594 | $query->insert('flow_checks') |
||
| 595 | ->values([ |
||
| 596 | 'class' => $query->createNamedParameter($class), |
||
| 597 | 'operator' => $query->createNamedParameter($operator), |
||
| 598 | 'value' => $query->createNamedParameter($value), |
||
| 599 | 'hash' => $query->createNamedParameter($hash), |
||
| 600 | ]); |
||
| 601 | $query->execute(); |
||
| 602 | |||
| 603 | return $query->getLastInsertId(); |
||
| 604 | } |
||
| 605 | |||
| 606 | protected function addScope(int $operationId, ScopeContext $scope): void { |
||
| 607 | $query = $this->connection->getQueryBuilder(); |
||
| 608 | |||
| 609 | $insertQuery = $query->insert('flow_operations_scope'); |
||
| 610 | $insertQuery->values([ |
||
| 611 | 'operation_id' => $query->createNamedParameter($operationId), |
||
| 612 | 'type' => $query->createNamedParameter($scope->getScope()), |
||
| 613 | 'value' => $query->createNamedParameter($scope->getScopeId()), |
||
| 614 | ]); |
||
| 615 | $insertQuery->execute(); |
||
| 616 | } |
||
| 617 | |||
| 618 | public function formatOperation(array $operation): array { |
||
| 634 | } |
||
| 635 | |||
| 636 | /** |
||
| 637 | * @return IEntity[] |
||
| 638 | */ |
||
| 639 | public function getEntitiesList(): array { |
||
| 644 | } |
||
| 645 | |||
| 646 | /** |
||
| 647 | * @return IOperation[] |
||
| 648 | */ |
||
| 649 | public function getOperatorList(): array { |
||
| 650 | $this->dispatcher->dispatchTyped(new RegisterOperationsEvent($this)); |
||
| 651 | $this->legacyEventDispatcher->dispatch(IManager::EVENT_NAME_REG_OPERATION, new GenericEvent($this)); |
||
| 652 | |||
| 653 | return array_merge($this->getBuildInOperators(), $this->registeredOperators); |
||
| 654 | } |
||
| 655 | |||
| 656 | /** |
||
| 657 | * @return ICheck[] |
||
| 658 | */ |
||
| 659 | public function getCheckList(): array { |
||
| 664 | } |
||
| 665 | |||
| 666 | public function registerEntity(IEntity $entity): void { |
||
| 667 | $this->registeredEntities[get_class($entity)] = $entity; |
||
| 668 | } |
||
| 669 | |||
| 670 | public function registerOperation(IOperation $operator): void { |
||
| 672 | } |
||
| 673 | |||
| 674 | public function registerCheck(ICheck $check): void { |
||
| 676 | } |
||
| 677 | |||
| 678 | /** |
||
| 679 | * @return IEntity[] |
||
| 680 | */ |
||
| 681 | protected function getBuildInEntities(): array { |
||
| 682 | try { |
||
| 683 | return [ |
||
| 684 | File::class => $this->container->query(File::class), |
||
| 685 | ]; |
||
| 686 | } catch (QueryException $e) { |
||
| 687 | $this->logger->logException($e); |
||
| 688 | return []; |
||
| 689 | } |
||
| 690 | } |
||
| 691 | |||
| 692 | /** |
||
| 693 | * @return IOperation[] |
||
| 694 | */ |
||
| 695 | protected function getBuildInOperators(): array { |
||
| 696 | try { |
||
| 697 | return [ |
||
| 698 | // None yet |
||
| 699 | ]; |
||
| 700 | } catch (QueryException $e) { |
||
| 701 | $this->logger->logException($e); |
||
| 702 | return []; |
||
| 703 | } |
||
| 704 | } |
||
| 705 | |||
| 706 | /** |
||
| 707 | * @return ICheck[] |
||
| 708 | */ |
||
| 709 | protected function getBuildInChecks(): array { |
||
| 725 | } |
||
| 726 | } |
||
| 727 | |||
| 728 | public function isUserScopeEnabled(): bool { |
||
| 730 | } |
||
| 731 | } |
||
| 732 |