| Conditions | 7 |
| Paths | 1 |
| Total Lines | 82 |
| Code Lines | 61 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 74 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 75 | { |
||
| 76 | $user = $request->getAttribute('user'); |
||
| 77 | |||
| 78 | $languages = $this->module_service->findByInterface(ModuleLanguageInterface::class, true) |
||
| 79 | ->mapWithKeys(static function (ModuleLanguageInterface $module): array { |
||
| 80 | $locale = $module->locale(); |
||
| 81 | |||
| 82 | return [$locale->languageTag() => $locale->endonym()]; |
||
| 83 | }); |
||
| 84 | |||
| 85 | $query = DB::table('user') |
||
| 86 | ->leftJoin('user_setting AS us1', static function (JoinClause $join): void { |
||
| 87 | $join |
||
| 88 | ->on('us1.user_id', '=', 'user.user_id') |
||
| 89 | ->where('us1.setting_name', '=', 'language'); |
||
| 90 | }) |
||
| 91 | ->leftJoin('user_setting AS us2', static function (JoinClause $join): void { |
||
| 92 | $join |
||
| 93 | ->on('us2.user_id', '=', 'user.user_id') |
||
| 94 | ->where('us2.setting_name', '=', UserInterface::PREF_TIMESTAMP_REGISTERED); |
||
| 95 | }) |
||
| 96 | ->leftJoin('user_setting AS us3', static function (JoinClause $join): void { |
||
| 97 | $join |
||
| 98 | ->on('us3.user_id', '=', 'user.user_id') |
||
| 99 | ->where('us3.setting_name', '=', UserInterface::PREF_TIMESTAMP_ACTIVE); |
||
| 100 | }) |
||
| 101 | ->leftJoin('user_setting AS us4', static function (JoinClause $join): void { |
||
| 102 | $join |
||
| 103 | ->on('us4.user_id', '=', 'user.user_id') |
||
| 104 | ->where('us4.setting_name', '=', UserInterface::PREF_IS_EMAIL_VERIFIED); |
||
| 105 | }) |
||
| 106 | ->leftJoin('user_setting AS us5', static function (JoinClause $join): void { |
||
| 107 | $join |
||
| 108 | ->on('us5.user_id', '=', 'user.user_id') |
||
| 109 | ->where('us5.setting_name', '=', UserInterface::PREF_IS_ACCOUNT_APPROVED); |
||
| 110 | }) |
||
| 111 | ->where('user.user_id', '>', '0') |
||
| 112 | ->select([ |
||
| 113 | 'user.user_id AS edit_menu', // Hidden column |
||
| 114 | 'user.user_id', |
||
| 115 | 'user_name', |
||
| 116 | 'real_name', |
||
| 117 | 'email', |
||
| 118 | 'us1.setting_value AS language', |
||
| 119 | 'us2.setting_value AS registered_at_sort', // Hidden column |
||
| 120 | 'us2.setting_value AS registered_at', |
||
| 121 | 'us3.setting_value AS active_at_sort', // Hidden column |
||
| 122 | 'us3.setting_value AS active_at', |
||
| 123 | 'us4.setting_value AS verified', |
||
| 124 | 'us5.setting_value AS verified_by_admin', |
||
| 125 | ]); |
||
| 126 | |||
| 127 | $search_columns = ['user_name', 'real_name', 'email']; |
||
| 128 | $sort_columns = []; |
||
| 129 | |||
| 130 | $callback = function (stdClass $row) use ($languages, $user): array { |
||
| 131 | $row_user = $this->user_service->find($row->user_id); |
||
| 132 | $datum = [ |
||
| 133 | view('admin/users-table-options', ['row' => $row, 'self' => $user, 'user' => $row_user]), |
||
| 134 | $row->user_id, |
||
| 135 | '<span dir="auto">' . e($row->user_name) . '</span>', |
||
| 136 | '<span dir="auto">' . e($row->real_name) . '</span>', |
||
| 137 | '<a href="mailto:' . e($row->email) . '">' . e($row->email) . '</a>', |
||
| 138 | $languages->get($row->language, $row->language), |
||
| 139 | $row->registered_at, |
||
| 140 | $row->registered_at ? view('components/datetime-diff', ['timestamp' => Carbon::createFromTimestamp((int) $row->registered_at)]) : '', |
||
| 141 | $row->active_at, |
||
| 142 | $row->active_at ? view('components/datetime-diff', ['timestamp' => Carbon::createFromTimestamp((int) $row->active_at)]) : I18N::translate('Never'), |
||
| 143 | $row->verified ? I18N::translate('yes') : I18N::translate('no'), |
||
| 144 | $row->verified_by_admin ? I18N::translate('yes') : I18N::translate('no'), |
||
| 145 | ]; |
||
| 146 | |||
| 147 | // Highlight old registrations. |
||
| 148 | if (!$datum[10] && date('U') - $datum[6] > 604800) { |
||
| 149 | $datum[7] = '<span class="text-danger">' . $datum[7] . '</span>'; |
||
| 150 | } |
||
| 151 | |||
| 152 | return $datum; |
||
| 153 | }; |
||
| 154 | |||
| 155 | return $this->datatables_service->handleQuery($request, $query, $search_columns, $sort_columns, $callback); |
||
| 156 | } |
||
| 158 |