| Total Complexity | 47 |
| Total Lines | 178 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Operator 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 Operator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Operator extends TightModel implements OperatorInterface |
||
| 12 | { |
||
| 13 | public const TABLE_NAME = 'kadro_operator'; |
||
| 14 | public const TABLE_ALIAS = 'operator'; |
||
| 15 | |||
| 16 | |||
| 17 | protected $permissions = null; |
||
| 18 | |||
| 19 | // use Permissionability; |
||
| 20 | use \HexMakina\TightORM\RelationManyToMany; |
||
| 21 | |||
| 22 | public function __toString() |
||
| 23 | { |
||
| 24 | return $this->get('username'); |
||
| 25 | } |
||
| 26 | |||
| 27 | public function isActive(): bool |
||
| 28 | { |
||
| 29 | return !empty($this->get('active')); |
||
| 30 | } |
||
| 31 | |||
| 32 | public function operatorId() |
||
| 33 | { |
||
| 34 | return $this->getId(); |
||
| 35 | } |
||
| 36 | |||
| 37 | public function username() |
||
| 38 | { |
||
| 39 | return $this->get('username'); |
||
| 40 | } |
||
| 41 | |||
| 42 | // TODO remove this, pwd only useful when checkinin |
||
| 43 | public function password() |
||
| 44 | { |
||
| 45 | return $this->get('password'); |
||
| 46 | } |
||
| 47 | |||
| 48 | public function passwordChange($string) |
||
| 49 | { |
||
| 50 | $this->set('password', password_hash($this->validate_password($string), PASSWORD_DEFAULT)); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function passwordVerify($string): bool |
||
| 54 | { |
||
| 55 | return password_verify($this->validate_password($string), $this->password() ?? ''); |
||
| 56 | } |
||
| 57 | |||
| 58 | public function name() |
||
| 59 | { |
||
| 60 | return $this->get('name'); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function email() |
||
| 64 | { |
||
| 65 | return $this->get('email'); |
||
| 66 | } |
||
| 67 | public function phone() |
||
| 68 | { |
||
| 69 | return $this->get('phone'); |
||
| 70 | } |
||
| 71 | |||
| 72 | public function languageCode() |
||
| 73 | { |
||
| 74 | return $this->get('language_code'); |
||
| 75 | } |
||
| 76 | |||
| 77 | public static function safeLoading($op_id): OperatorInterface |
||
| 78 | { |
||
| 79 | $op = static::one($op_id); |
||
| 80 | $op->set('password', null); |
||
| 81 | return $op; |
||
| 82 | } |
||
| 83 | |||
| 84 | public static function query_retrieve($filters = [], $options = []): SelectInterface |
||
| 85 | { |
||
| 86 | $Query = static::table()->select(); |
||
| 87 | if (isset($options['eager']) && $options['eager'] === true) { |
||
| 88 | $Query->groupBy('id'); |
||
| 89 | |||
| 90 | AutoJoin::join($Query, [ACL::table(), 'acl'], null, 'LEFT OUTER'); |
||
| 91 | AutoJoin::join($Query, [Permission::table(), 'kadro_permission'], null, 'LEFT OUTER'); |
||
| 92 | $Query->selectAlso(["GROUP_CONCAT(DISTINCT kadro_permission.id) as permission_ids", "GROUP_CONCAT(DISTINCT kadro_permission.name) as permission_names"]); |
||
| 93 | } |
||
| 94 | |||
| 95 | if (isset($filters['model']) && !empty($filters['model'])) { |
||
| 96 | $Query->join([static::otm('t'), static::otm('a')], [[static::otm('a'),static::otm('k'), 't_from','id']], 'INNER'); |
||
| 97 | $Query->whereFieldsEQ(['model_id' => $filters['model']->getId(), 'model_type' => get_class($filters['model'])::model_type()], static::otm('a')); |
||
| 98 | } |
||
| 99 | |||
| 100 | $Query->orderBy([$Query->tableLabel(), 'name', 'ASC']); |
||
| 101 | |||
| 102 | |||
| 103 | return $Query; |
||
| 104 | } |
||
| 105 | |||
| 106 | public function immortal(): bool |
||
| 107 | { |
||
| 108 | return true; // never delete a user, always deactivate |
||
| 109 | } |
||
| 110 | |||
| 111 | public function permission_names() |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | public function permissions() |
||
| 129 | { |
||
| 130 | |||
| 146 | } |
||
| 147 | |||
| 148 | public function hasPermission($p): bool |
||
| 149 | { |
||
| 150 | // new instances or inactive operators, none shall pass |
||
| 151 | if ($this->isNew() === true || $this->isActive() === false) { |
||
| 152 | return false; |
||
| 153 | } |
||
| 154 | |||
| 155 | $permission_name = $permission_id = null; |
||
| 156 | if (is_subclass_of($p, '\HexMakina\kadro\Auth\Permission')) { |
||
| 157 | $permission_name = $p->get('name'); |
||
| 158 | $permission_id = $p->getId(); |
||
| 159 | } elseif (preg_match('/[0-9]+/', $p)) { |
||
| 160 | $permission_id = $p; |
||
| 161 | } else { |
||
| 162 | $permission_name = $p; |
||
| 163 | } |
||
| 164 | |||
| 165 | if (!is_null($this->get('permission_names')) && !is_null($permission_name)) { |
||
| 166 | return strpos($this->get('permission_names'), $permission_name) !== false; |
||
|
|
|||
| 167 | } elseif (!is_null($this->get('permission_ids')) && !is_null($permission_id)) { |
||
| 168 | return strpos($this->get('permission_ids'), $permission_id) !== false; |
||
| 169 | } elseif (!is_null($permission_name)) { |
||
| 170 | if (method_exists($this, $permission_name) && $this->$permission_name() == true) { |
||
| 171 | return true; |
||
| 172 | } elseif (property_exists($this, $permission_name) && $this->get('$permission_name') == true) { |
||
| 173 | return true; |
||
| 174 | } elseif (ACL::match($this, $permission_name) === true) { |
||
| 175 | return true; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | return false; |
||
| 180 | } |
||
| 181 | |||
| 182 | private function validate_password($string): string |
||
| 189 | } |
||
| 190 | } |
||
| 191 |