| Total Complexity | 60 |
| Total Lines | 407 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AutoAssign 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 AutoAssign, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class AutoAssign extends Base |
||
| 22 | { |
||
| 23 | /** @var string Basic table name */ |
||
| 24 | public const TABLE_NAME = 's_#__auto_assign'; |
||
| 25 | /** @var array Members tables */ |
||
| 26 | public const MEMBERS_TABLES = ['s_#__auto_assign_users' => 'id', 's_#__auto_assign_groups' => 'id', 's_#__auto_assign_roles' => 'id']; |
||
| 27 | /** @var string Round robin table name */ |
||
| 28 | public const ROUND_ROBIN_TABLE = 'u_#__auto_assign_rr'; |
||
| 29 | |||
| 30 | /** @var int Status inactive */ |
||
| 31 | public const STATUS_INACTIVE = 0; |
||
| 32 | /** @var int Status active */ |
||
| 33 | public const STATUS_ACTIVE = 1; |
||
| 34 | |||
| 35 | /** @var int Manual mode */ |
||
| 36 | public const MODE_MANUAL = 1; |
||
| 37 | /** @var int Handler mode */ |
||
| 38 | public const MODE_HANDLER = 2; |
||
| 39 | /** @var int Workflow mode */ |
||
| 40 | public const MODE_WORKFLOW = 4; |
||
| 41 | |||
| 42 | /** @var int Load balance method */ |
||
| 43 | public const METHOD_LOAD_BALANCE = 0; |
||
| 44 | /** @var int Round robin method */ |
||
| 45 | public const METHOD_ROUND_ROBIN = 1; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Get all auto assign entries for module. |
||
| 49 | * |
||
| 50 | * @param string $moduleName |
||
| 51 | * @param int $mode A bitmask of one or more of the mode flags |
||
| 52 | * @param int $state |
||
| 53 | * |
||
| 54 | * @return array |
||
| 55 | */ |
||
| 56 | public static function getByModule(string $moduleName, int $mode = self::MODE_HANDLER | self::MODE_WORKFLOW | self::MODE_MANUAL, int $state = self::STATUS_ACTIVE): array |
||
| 57 | { |
||
| 58 | $query = (new Db\Query())->from(self::TABLE_NAME) |
||
| 59 | ->where(['tabid' => Module::getModuleId($moduleName), 'state' => $state]); |
||
| 60 | $mods = ['or']; |
||
| 61 | foreach ([self::MODE_MANUAL => 'gui', self::MODE_HANDLER => 'handler', self::MODE_WORKFLOW => 'workflow'] as $key => $column) { |
||
| 62 | if ($mode & $key) { |
||
| 63 | $mods[] = [$column => 1]; |
||
| 64 | } |
||
| 65 | } |
||
| 66 | $query->andWhere($mods); |
||
| 67 | |||
| 68 | return $query->all(); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Get all auto assign instances for module. |
||
| 73 | * |
||
| 74 | * @param string $moduleName |
||
| 75 | * @param int|null $mode A bitmask of one or more of the mode flags |
||
| 76 | * |
||
| 77 | * @return array |
||
| 78 | */ |
||
| 79 | public static function getInstancesByModule(string $moduleName, int $mode = null): array |
||
| 80 | { |
||
| 81 | $instances = []; |
||
| 82 | foreach (self::getByModule($moduleName, $mode) as $autoAssignData) { |
||
| 83 | $instances[$autoAssignData['id']] = self::getInstance($autoAssignData); |
||
| 84 | } |
||
| 85 | return $instances; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Get auto assign instance for record. |
||
| 90 | * |
||
| 91 | * @param \Vtiger_Record_Model $recordModel |
||
| 92 | * @param int|null $mode A bitmask of one or more of the mode flags |
||
| 93 | * |
||
| 94 | * @return self|null |
||
| 95 | */ |
||
| 96 | public static function getAutoAssignForRecord(\Vtiger_Record_Model $recordModel, int $mode = null): ?self |
||
| 97 | { |
||
| 98 | $autoAssignInstance = null; |
||
| 99 | foreach (self::getByModule($recordModel->getModuleName(), $mode) as $autoAssignData) { |
||
| 100 | $conditions = Json::isEmpty($autoAssignData['conditions']) ? [] : Json::decode($autoAssignData['conditions']); |
||
| 101 | if (Condition::checkConditions($conditions, $recordModel)) { |
||
| 102 | $autoAssignInstance = self::getInstance($autoAssignData); |
||
| 103 | break; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | return $autoAssignInstance; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get auto assign instance by ID. |
||
| 111 | * |
||
| 112 | * @param int $id |
||
| 113 | * |
||
| 114 | * @return self|null |
||
| 115 | */ |
||
| 116 | public static function getInstanceById(int $id): ?self |
||
| 117 | { |
||
| 118 | $data = (new Db\Query())->from(self::TABLE_NAME)->where(['id' => $id])->one(); |
||
| 119 | return $data ? (new self())->setData($data) : null; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get auto assign instance by data. |
||
| 124 | * |
||
| 125 | * @param array $data |
||
| 126 | * |
||
| 127 | * @return self|null |
||
| 128 | */ |
||
| 129 | public static function getInstance(array $data): ?self |
||
| 130 | { |
||
| 131 | return $data ? (new self())->setData($data) : null; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Function to get the Id. |
||
| 136 | * |
||
| 137 | * @return int |
||
| 138 | */ |
||
| 139 | public function getId(): int |
||
| 140 | { |
||
| 141 | return $this->get('id'); |
||
|
|
|||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get name of auto assign instance. |
||
| 146 | * |
||
| 147 | * @param bool $encode |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function getName(bool $encode = true): string |
||
| 152 | { |
||
| 153 | return Language::translate($this->get('subject'), 'Settings:AutomaticAssignment', false, $encode); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Get module name. |
||
| 158 | * |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | public function getModuleName(): string |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Check conditions for record. |
||
| 168 | * |
||
| 169 | * @param \Vtiger_Record_Model $recordModel |
||
| 170 | * |
||
| 171 | * @return bool |
||
| 172 | */ |
||
| 173 | public function checkConditionForRecord(\Vtiger_Record_Model $recordModel): bool |
||
| 174 | { |
||
| 175 | $conditions = Json::isEmpty($this->get('conditions')) ? [] : Json::decode($this->get('conditions')); |
||
| 176 | return Condition::checkConditions($conditions, $recordModel); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Check if the instance is active in a given mode. |
||
| 181 | * |
||
| 182 | * @param int $mode |
||
| 183 | * |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | public function isActive(int $mode): bool |
||
| 187 | { |
||
| 188 | switch ($mode) { |
||
| 189 | case self::MODE_MANUAL: |
||
| 190 | $result = !$this->isEmpty('gui'); |
||
| 191 | break; |
||
| 192 | case self::MODE_HANDLER: |
||
| 193 | $result = !$this->isEmpty('handler'); |
||
| 194 | break; |
||
| 195 | case self::MODE_WORKFLOW: |
||
| 196 | $result = !$this->isEmpty('workflow'); |
||
| 197 | break; |
||
| 198 | default: |
||
| 199 | $result = false; |
||
| 200 | break; |
||
| 201 | } |
||
| 202 | return $result && self::STATUS_ACTIVE === (int) $this->get('state'); |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Get an automatic selected user ID. |
||
| 207 | * |
||
| 208 | * @return int|null |
||
| 209 | */ |
||
| 210 | public function getOwner(): ?int |
||
| 211 | { |
||
| 212 | switch ($this->get('method')) { |
||
| 213 | case self::METHOD_LOAD_BALANCE: |
||
| 214 | $owner = $this->getQueryByLoadBalance()->scalar() ?: null; |
||
| 215 | break; |
||
| 216 | case self::METHOD_ROUND_ROBIN: |
||
| 217 | $owner = $this->getQueryByRoundRobin()->scalar() ?: null; |
||
| 218 | break; |
||
| 219 | default: |
||
| 220 | $owner = null; |
||
| 221 | break; |
||
| 222 | } |
||
| 223 | |||
| 224 | return $owner ? $owner : $this->getDefaultOwner(); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Get automatic selected users. |
||
| 229 | * |
||
| 230 | * @return array |
||
| 231 | */ |
||
| 232 | public function getOwners(): array |
||
| 233 | { |
||
| 234 | switch ($this->get('method')) { |
||
| 235 | case self::METHOD_LOAD_BALANCE: |
||
| 236 | $owner = $this->getQueryByLoadBalance()->all(); |
||
| 237 | break; |
||
| 238 | case self::METHOD_ROUND_ROBIN: |
||
| 239 | $owner = $this->getQueryByRoundRobin()->all(); |
||
| 240 | break; |
||
| 241 | default: |
||
| 242 | $owner = []; |
||
| 243 | break; |
||
| 244 | } |
||
| 245 | |||
| 246 | return $owner; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Get default owner. |
||
| 251 | * |
||
| 252 | * @return int |
||
| 253 | */ |
||
| 254 | public function getDefaultOwner(): ?int |
||
| 255 | { |
||
| 256 | $owner = null; |
||
| 257 | $defaultOwner = (int) $this->get('default_assign'); |
||
| 258 | $ownerModel = Fields\Owner::getInstance($this->getModuleName()); |
||
| 259 | |||
| 260 | $type = $defaultOwner ? Fields\Owner::getType($defaultOwner) : null; |
||
| 261 | if ('Users' === $type) { |
||
| 262 | $owner = User::isExists($defaultOwner) ? $defaultOwner : $owner; |
||
| 263 | } elseif ($type) { |
||
| 264 | $owner = \array_key_exists($defaultOwner, $ownerModel->getAccessibleGroupForModule()) ? $defaultOwner : $owner; |
||
| 265 | } |
||
| 266 | |||
| 267 | return $owner; |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Query object for users allowed to be assigned by load balanced method. |
||
| 272 | * |
||
| 273 | * In order to correctly balance the entries attribution |
||
| 274 | * we need ot randomize the order in which they are returned. |
||
| 275 | * Otherwise, when multiple users have the same amount of entries |
||
| 276 | * it is always the first one in the results who will be assigned to new entry. |
||
| 277 | * |
||
| 278 | * @return Db\Query |
||
| 279 | */ |
||
| 280 | public function getQueryByLoadBalance(): Db\Query |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Query object for users allowed to be assigned by round robin. |
||
| 287 | * |
||
| 288 | * @return Db\Query |
||
| 289 | */ |
||
| 290 | public function getQueryByRoundRobin(): Db\Query |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Query object for users allowed for assignment. |
||
| 304 | * |
||
| 305 | * @return Db\Query |
||
| 306 | */ |
||
| 307 | public function getQuery(): Db\Query |
||
| 308 | { |
||
| 309 | $ownerFieldName = 'assigned_user_id'; |
||
| 310 | $queryGeneratorUsers = $this->getAvailableUsersQuery(); |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Query generator object of available users. |
||
| 339 | * |
||
| 340 | * @return QueryGenerator |
||
| 341 | */ |
||
| 342 | public function getAvailableUsersQuery(): QueryGenerator |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Get members. |
||
| 386 | * |
||
| 387 | * @return array |
||
| 388 | */ |
||
| 389 | public function getMembers(): array |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Post process action. |
||
| 412 | * |
||
| 413 | * @param int $userId |
||
| 414 | * |
||
| 415 | * @return void |
||
| 416 | */ |
||
| 417 | public function postProcess(int $userId) |
||
| 432 |