| Total Complexity | 42 |
| Total Lines | 244 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like UserRepository 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 UserRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class UserRepository implements UserRepositoryInterface |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var Tree |
||
| 42 | */ |
||
| 43 | private $tree; |
||
| 44 | /** |
||
| 45 | * @var UserService |
||
| 46 | */ |
||
| 47 | private $user_service; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Constructor. |
||
| 51 | * |
||
| 52 | * @param Tree $tree |
||
| 53 | * @param UserService $user_service |
||
| 54 | */ |
||
| 55 | public function __construct(Tree $tree, UserService $user_service) |
||
| 56 | { |
||
| 57 | $this->tree = $tree; |
||
| 58 | $this->user_service = $user_service; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Who is currently logged in? |
||
| 63 | * |
||
| 64 | * @param string $type "list" or "nolist" |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | private function usersLoggedInQuery($type = 'nolist'): string |
||
| 69 | { |
||
| 70 | $content = ''; |
||
| 71 | $anonymous = 0; |
||
| 72 | $logged_in = []; |
||
| 73 | |||
| 74 | foreach ($this->user_service->allLoggedIn() as $user) { |
||
| 75 | if (Auth::isAdmin() || $user->getPreference(User::PREF_IS_VISIBLE_ONLINE) === '1') { |
||
| 76 | $logged_in[] = $user; |
||
| 77 | } else { |
||
| 78 | $anonymous++; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | $count_logged_in = count($logged_in); |
||
| 83 | |||
| 84 | if ($count_logged_in === 0 && $anonymous === 0) { |
||
| 85 | $content .= I18N::translate('No signed-in and no anonymous users'); |
||
| 86 | } |
||
| 87 | |||
| 88 | if ($anonymous > 0) { |
||
| 89 | $content .= '<b>' . I18N::plural('%s anonymous signed-in user', '%s anonymous signed-in users', $anonymous, I18N::number($anonymous)) . '</b>'; |
||
| 90 | } |
||
| 91 | |||
| 92 | if ($count_logged_in > 0) { |
||
| 93 | if ($anonymous) { |
||
| 94 | if ($type === 'list') { |
||
| 95 | $content .= '<br><br>'; |
||
| 96 | } else { |
||
| 97 | $content .= ' ' . I18N::translate('and') . ' '; |
||
| 98 | } |
||
| 99 | } |
||
| 100 | $content .= '<b>' . I18N::plural('%s signed-in user', '%s signed-in users', $count_logged_in, I18N::number($count_logged_in)) . '</b>'; |
||
| 101 | if ($type === 'list') { |
||
| 102 | $content .= '<ul>'; |
||
| 103 | } else { |
||
| 104 | $content .= ': '; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | if (Auth::check()) { |
||
| 109 | foreach ($logged_in as $user) { |
||
| 110 | if ($type === 'list') { |
||
| 111 | $content .= '<li>'; |
||
| 112 | } |
||
| 113 | |||
| 114 | $individual = Registry::individualFactory()->make($this->tree->getUserPreference($user, User::PREF_TREE_ACCOUNT_XREF), $this->tree); |
||
| 115 | |||
| 116 | if ($individual instanceof Individual && $individual->canShow()) { |
||
| 117 | $content .= '<a href="' . e($individual->url()) . '">' . e($user->realName()) . '</a>'; |
||
| 118 | } else { |
||
| 119 | $content .= e($user->realName()); |
||
| 120 | } |
||
| 121 | |||
| 122 | $content .= ' - ' . e($user->userName()); |
||
| 123 | |||
| 124 | if ($user->getPreference(User::PREF_CONTACT_METHOD) !== 'none' && Auth::id() !== $user->id()) { |
||
| 125 | $content .= '<a href="' . e(route(MessagePage::class, ['to' => $user->userName(), 'tree' => $this->tree->name()])) . '" class="btn btn-link" title="' . I18N::translate('Send a message') . '">' . view('icons/email') . '</a>'; |
||
| 126 | } |
||
| 127 | |||
| 128 | if ($type === 'list') { |
||
| 129 | $content .= '</li>'; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | if ($type === 'list') { |
||
| 135 | $content .= '</ul>'; |
||
| 136 | } |
||
| 137 | |||
| 138 | return $content; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | public function usersLoggedIn(): string |
||
| 145 | { |
||
| 146 | return $this->usersLoggedInQuery('nolist'); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | public function usersLoggedInList(): string |
||
| 153 | { |
||
| 154 | return $this->usersLoggedInQuery('list'); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Returns true if the given user is visible to others. |
||
| 159 | * |
||
| 160 | * @param UserInterface $user |
||
| 161 | * |
||
| 162 | * @return bool |
||
| 163 | */ |
||
| 164 | private function isUserVisible(UserInterface $user): bool |
||
| 165 | { |
||
| 166 | return Auth::isAdmin() || $user->getPreference(User::PREF_IS_VISIBLE_ONLINE) === '1'; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return int |
||
| 171 | */ |
||
| 172 | public function usersLoggedInTotal(): int |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return int |
||
| 179 | */ |
||
| 180 | public function usersLoggedInTotalAnon(): int |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return int |
||
| 195 | */ |
||
| 196 | public function usersLoggedInTotalVisible(): int |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | public function userId(): string |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param string $visitor_text |
||
| 219 | * |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | public function userName(string $visitor_text = ''): string |
||
| 223 | { |
||
| 224 | if (Auth::check()) { |
||
| 225 | return e(Auth::user()->userName()); |
||
| 226 | } |
||
| 227 | |||
| 228 | // if #username:visitor# was specified, then "visitor" will be returned when the user is not logged in |
||
| 229 | return e($visitor_text); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | public function userFullName(): string |
||
| 236 | { |
||
| 237 | return Auth::check() ? '<span dir="auto">' . e(Auth::user()->realName()) . '</span>' : ''; |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Returns the user count. |
||
| 242 | * |
||
| 243 | * @return int |
||
| 244 | */ |
||
| 245 | private function getUserCount(): int |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Returns the administrator count. |
||
| 252 | * |
||
| 253 | * @return int |
||
| 254 | */ |
||
| 255 | private function getAdminCount(): int |
||
| 256 | { |
||
| 257 | return count($this->user_service->administrators()); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | public function totalUsers(): string |
||
| 264 | { |
||
| 265 | return I18N::number($this->getUserCount()); |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | public function totalAdmins(): string |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | public function totalNonAdmins(): string |
||
| 282 | } |
||
| 283 | } |
||
| 284 |