| Total Complexity | 49 |
| Total Lines | 436 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AbstractPrincipalBackend 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 AbstractPrincipalBackend, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | abstract class AbstractPrincipalBackend implements BackendInterface { |
||
| 36 | |||
| 37 | /** @var IDBConnection */ |
||
| 38 | private $db; |
||
| 39 | |||
| 40 | /** @var IUserSession */ |
||
| 41 | private $userSession; |
||
| 42 | |||
| 43 | /** @var IGroupManager */ |
||
| 44 | private $groupManager; |
||
| 45 | |||
| 46 | /** @var ILogger */ |
||
| 47 | private $logger; |
||
| 48 | |||
| 49 | /** @var ProxyMapper */ |
||
| 50 | private $proxyMapper; |
||
| 51 | |||
| 52 | /** @var string */ |
||
| 53 | private $principalPrefix; |
||
| 54 | |||
| 55 | /** @var string */ |
||
| 56 | private $dbTableName; |
||
| 57 | |||
| 58 | /** @var string */ |
||
| 59 | private $dbMetaDataTableName; |
||
| 60 | |||
| 61 | /** @var string */ |
||
| 62 | private $dbForeignKeyName; |
||
| 63 | |||
| 64 | /** @var string */ |
||
| 65 | private $cuType; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param IDBConnection $dbConnection |
||
| 69 | * @param IUserSession $userSession |
||
| 70 | * @param IGroupManager $groupManager |
||
| 71 | * @param ILogger $logger |
||
| 72 | * @param string $principalPrefix |
||
| 73 | * @param string $dbPrefix |
||
| 74 | * @param string $cuType |
||
| 75 | */ |
||
| 76 | public function __construct(IDBConnection $dbConnection, |
||
| 77 | IUserSession $userSession, |
||
| 78 | IGroupManager $groupManager, |
||
| 79 | ILogger $logger, |
||
| 80 | ProxyMapper $proxyMapper, |
||
| 81 | string $principalPrefix, |
||
| 82 | string $dbPrefix, |
||
| 83 | string $cuType) { |
||
| 84 | $this->db = $dbConnection; |
||
| 85 | $this->userSession = $userSession; |
||
| 86 | $this->groupManager = $groupManager; |
||
| 87 | $this->logger = $logger; |
||
| 88 | $this->proxyMapper = $proxyMapper; |
||
| 89 | $this->principalPrefix = $principalPrefix; |
||
| 90 | $this->dbTableName = 'calendar_' . $dbPrefix . 's'; |
||
| 91 | $this->dbMetaDataTableName = $this->dbTableName . '_md'; |
||
| 92 | $this->dbForeignKeyName = $dbPrefix . '_id'; |
||
| 93 | $this->cuType = $cuType; |
||
| 94 | } |
||
| 95 | |||
| 96 | use PrincipalProxyTrait; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Returns a list of principals based on a prefix. |
||
| 100 | * |
||
| 101 | * This prefix will often contain something like 'principals'. You are only |
||
| 102 | * expected to return principals that are in this base path. |
||
| 103 | * |
||
| 104 | * You are expected to return at least a 'uri' for every user, you can |
||
| 105 | * return any additional properties if you wish so. Common properties are: |
||
| 106 | * {DAV:}displayname |
||
| 107 | * |
||
| 108 | * @param string $prefixPath |
||
| 109 | * @return string[] |
||
| 110 | */ |
||
| 111 | public function getPrincipalsByPrefix($prefixPath) { |
||
| 112 | $principals = []; |
||
| 113 | |||
| 114 | if ($prefixPath === $this->principalPrefix) { |
||
| 115 | $query = $this->db->getQueryBuilder(); |
||
| 116 | $query->select(['id', 'backend_id', 'resource_id', 'email', 'displayname']) |
||
| 117 | ->from($this->dbTableName); |
||
| 118 | $stmt = $query->execute(); |
||
| 119 | |||
| 120 | $metaDataQuery = $this->db->getQueryBuilder(); |
||
| 121 | $metaDataQuery->select([$this->dbForeignKeyName, 'key', 'value']) |
||
| 122 | ->from($this->dbMetaDataTableName); |
||
| 123 | $metaDataStmt = $metaDataQuery->execute(); |
||
| 124 | $metaDataRows = $metaDataStmt->fetchAll(\PDO::FETCH_ASSOC); |
||
| 125 | |||
| 126 | $metaDataById = []; |
||
| 127 | foreach($metaDataRows as $metaDataRow) { |
||
| 128 | if (!isset($metaDataById[$metaDataRow[$this->dbForeignKeyName]])) { |
||
| 129 | $metaDataById[$metaDataRow[$this->dbForeignKeyName]] = []; |
||
| 130 | } |
||
| 131 | |||
| 132 | $metaDataById[$metaDataRow[$this->dbForeignKeyName]][$metaDataRow['key']] = |
||
| 133 | $metaDataRow['value']; |
||
| 134 | } |
||
| 135 | |||
| 136 | while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
||
| 137 | $id = $row['id']; |
||
| 138 | |||
| 139 | if (isset($metaDataById[$id])) { |
||
| 140 | $principals[] = $this->rowToPrincipal($row, $metaDataById[$id]); |
||
| 141 | } else { |
||
| 142 | $principals[] = $this->rowToPrincipal($row); |
||
| 143 | } |
||
| 144 | |||
| 145 | } |
||
| 146 | |||
| 147 | $stmt->closeCursor(); |
||
| 148 | } |
||
| 149 | |||
| 150 | return $principals; |
||
|
|
|||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Returns a specific principal, specified by it's path. |
||
| 155 | * The returned structure should be the exact same as from |
||
| 156 | * getPrincipalsByPrefix. |
||
| 157 | * |
||
| 158 | * @param string $path |
||
| 159 | * @return array |
||
| 160 | */ |
||
| 161 | public function getPrincipalByPath($path) { |
||
| 162 | if (strpos($path, $this->principalPrefix) !== 0) { |
||
| 163 | return null; |
||
| 164 | } |
||
| 165 | list(, $name) = \Sabre\Uri\split($path); |
||
| 166 | |||
| 167 | list($backendId, $resourceId) = explode('-', $name, 2); |
||
| 168 | |||
| 169 | $query = $this->db->getQueryBuilder(); |
||
| 170 | $query->select(['id', 'backend_id', 'resource_id', 'email', 'displayname']) |
||
| 171 | ->from($this->dbTableName) |
||
| 172 | ->where($query->expr()->eq('backend_id', $query->createNamedParameter($backendId))) |
||
| 173 | ->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resourceId))); |
||
| 174 | $stmt = $query->execute(); |
||
| 175 | $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
||
| 176 | |||
| 177 | if(!$row) { |
||
| 178 | return null; |
||
| 179 | } |
||
| 180 | |||
| 181 | $metaDataQuery = $this->db->getQueryBuilder(); |
||
| 182 | $metaDataQuery->select(['key', 'value']) |
||
| 183 | ->from($this->dbMetaDataTableName) |
||
| 184 | ->where($metaDataQuery->expr()->eq($this->dbForeignKeyName, $metaDataQuery->createNamedParameter($row['id']))); |
||
| 185 | $metaDataStmt = $metaDataQuery->execute(); |
||
| 186 | $metaDataRows = $metaDataStmt->fetchAll(\PDO::FETCH_ASSOC); |
||
| 187 | $metadata = []; |
||
| 188 | |||
| 189 | foreach($metaDataRows as $metaDataRow) { |
||
| 190 | $metadata[$metaDataRow['key']] = $metaDataRow['value']; |
||
| 191 | } |
||
| 192 | |||
| 193 | return $this->rowToPrincipal($row, $metadata); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param int $id |
||
| 198 | * @return array|null |
||
| 199 | */ |
||
| 200 | public function getPrincipalById($id):?array { |
||
| 201 | $query = $this->db->getQueryBuilder(); |
||
| 202 | $query->select(['id', 'backend_id', 'resource_id', 'email', 'displayname']) |
||
| 203 | ->from($this->dbTableName) |
||
| 204 | ->where($query->expr()->eq('id', $query->createNamedParameter($id))); |
||
| 205 | $stmt = $query->execute(); |
||
| 206 | $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
||
| 207 | |||
| 208 | if(!$row) { |
||
| 209 | return null; |
||
| 210 | } |
||
| 211 | |||
| 212 | $metaDataQuery = $this->db->getQueryBuilder(); |
||
| 213 | $metaDataQuery->select(['key', 'value']) |
||
| 214 | ->from($this->dbMetaDataTableName) |
||
| 215 | ->where($metaDataQuery->expr()->eq($this->dbForeignKeyName, $metaDataQuery->createNamedParameter($row['id']))); |
||
| 216 | $metaDataStmt = $metaDataQuery->execute(); |
||
| 217 | $metaDataRows = $metaDataStmt->fetchAll(\PDO::FETCH_ASSOC); |
||
| 218 | $metadata = []; |
||
| 219 | |||
| 220 | foreach($metaDataRows as $metaDataRow) { |
||
| 221 | $metadata[$metaDataRow['key']] = $metaDataRow['value']; |
||
| 222 | } |
||
| 223 | |||
| 224 | return $this->rowToPrincipal($row, $metadata); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param string $path |
||
| 229 | * @param PropPatch $propPatch |
||
| 230 | * @return int |
||
| 231 | */ |
||
| 232 | function updatePrincipal($path, PropPatch $propPatch) { |
||
| 233 | return 0; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param string $prefixPath |
||
| 238 | * @param array $searchProperties |
||
| 239 | * @param string $test |
||
| 240 | * @return array |
||
| 241 | */ |
||
| 242 | function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') { |
||
| 332 | } |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Searches principals based on their metadata keys. |
||
| 337 | * This allows to search for all principals with a specific key. |
||
| 338 | * e.g.: |
||
| 339 | * '{http://nextcloud.com/ns}room-building-address' => 'ABC Street 123, ...' |
||
| 340 | * |
||
| 341 | * @param $key |
||
| 342 | * @param $value |
||
| 343 | * @return array |
||
| 344 | */ |
||
| 345 | private function searchPrincipalsByMetadataKey($key, $value):array { |
||
| 346 | $query = $this->db->getQueryBuilder(); |
||
| 347 | $query->select([$this->dbForeignKeyName]) |
||
| 348 | ->from($this->dbMetaDataTableName) |
||
| 349 | ->where($query->expr()->eq('key', $query->createNamedParameter($key))) |
||
| 350 | ->andWhere($query->expr()->iLike('value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($value) . '%'))); |
||
| 351 | $stmt = $query->execute(); |
||
| 352 | |||
| 353 | $rows = []; |
||
| 354 | while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
||
| 355 | $id = $row[$this->dbForeignKeyName]; |
||
| 356 | |||
| 357 | $principalRow = $this->getPrincipalById($id); |
||
| 358 | if (!$principalRow) { |
||
| 359 | continue; |
||
| 360 | } |
||
| 361 | |||
| 362 | $rows[] = $principalRow; |
||
| 363 | } |
||
| 364 | |||
| 365 | return $rows; |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param string $uri |
||
| 370 | * @param string $principalPrefix |
||
| 371 | * @return null|string |
||
| 372 | */ |
||
| 373 | function findByUri($uri, $principalPrefix) { |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * convert database row to principal |
||
| 432 | * |
||
| 433 | * @param String[] $row |
||
| 434 | * @param String[] $metadata |
||
| 435 | * @return Array |
||
| 436 | */ |
||
| 437 | private function rowToPrincipal(array $row, array $metadata=[]):array { |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @param $row |
||
| 448 | * @param $userGroups |
||
| 449 | * @return bool |
||
| 450 | */ |
||
| 451 | private function isAllowedToAccessResource(array $row, array $userGroups):bool { |
||
| 473 |