Conditions | 29 |
Paths | 2376 |
Total Lines | 152 |
Code Lines | 115 |
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 |
||
52 | |||
53 | /** |
||
54 | * @var HealthCheckService $healthcheck_service; |
||
55 | */ |
||
56 | private $healthcheck_service; |
||
57 | |||
58 | /** |
||
59 | * @var EmailService $email_service; |
||
60 | */ |
||
61 | private $email_service; |
||
62 | |||
63 | /** |
||
64 | * @var UserService $user_service |
||
65 | */ |
||
66 | private $user_service; |
||
67 | |||
68 | /** |
||
69 | * @var TreeService $tree_service |
||
70 | */ |
||
71 | private $tree_service; |
||
72 | |||
73 | /** |
||
74 | * @var UpgradeService $upgrade_service |
||
75 | */ |
||
76 | private $upgrade_service; |
||
77 | |||
78 | /** |
||
79 | * Constructor for HealthCheckTask |
||
80 | * |
||
81 | * @param ModuleService $module_service |
||
82 | * @param HealthCheckService $healthcheck_service |
||
83 | * @param EmailService $email_service |
||
84 | * @param UserService $user_service |
||
85 | * @param TreeService $tree_service |
||
86 | * @param UpgradeService $upgrade_service |
||
87 | */ |
||
88 | public function __construct( |
||
89 | ModuleService $module_service, |
||
90 | HealthCheckService $healthcheck_service, |
||
91 | EmailService $email_service, |
||
92 | UserService $user_service, |
||
93 | TreeService $tree_service, |
||
94 | UpgradeService $upgrade_service |
||
95 | ) { |
||
96 | $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
||
97 | $this->healthcheck_service = $healthcheck_service; |
||
98 | $this->email_service = $email_service; |
||
99 | $this->user_service = $user_service; |
||
100 | $this->tree_service = $tree_service; |
||
101 | $this->upgrade_service = $upgrade_service; |
||
102 | } |
||
103 | |||
104 | |||
105 | /** |
||
106 | * {@inheritDoc} |
||
107 | * @see \MyArtJaub\Webtrees\Module\AdminTasks\Model\TaskInterface::name() |
||
108 | */ |
||
109 | public function name(): string |
||
110 | { |
||
111 | return I18N::translate('Healthcheck Email'); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * {@inheritDoc} |
||
116 | * @see \MyArtJaub\Webtrees\Module\AdminTasks\Model\TaskInterface::defaultFrequency() |
||
117 | */ |
||
118 | public function defaultFrequency(): int |
||
119 | { |
||
120 | return 10080; // = 1 week = 7 * 24 * 60 min |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * {@inheritDoc} |
||
125 | * @see \MyArtJaub\Webtrees\Module\AdminTasks\Model\TaskInterface::run() |
||
126 | */ |
||
127 | public function run(TaskSchedule $task_schedule): bool |
||
128 | { |
||
129 | if ($this->module === null) { |
||
130 | return false; |
||
131 | } |
||
132 | |||
133 | $res = true; |
||
134 | |||
135 | // Compute the number of days to compute |
||
136 | $interval_lastrun = $task_schedule->lastRunTime()->diffAsCarbonInterval(Carbon::now()); |
||
137 | //@phpcs:ignore Generic.Files.LineLength.TooLong |
||
138 | $interval = $interval_lastrun->greaterThan($task_schedule->frequency()) ? $interval_lastrun : $task_schedule->frequency(); |
||
139 | $nb_days = (int) $interval->ceilDay()->totalDays; |
||
140 | |||
141 | $view_params_site = [ |
||
142 | 'nb_days' => $nb_days, |
||
143 | 'upgrade_available' => $this->upgrade_service->isUpgradeAvailable(), |
||
144 | 'latest_version' => $this->upgrade_service->latestVersion(), |
||
145 | 'download_url' => $this->upgrade_service->downloadUrl(), |
||
146 | 'all_users' => $this->user_service->all(), |
||
147 | 'unapproved' => $this->user_service->unapproved(), |
||
148 | 'unverified' => $this->user_service->unverified(), |
||
149 | ]; |
||
150 | |||
151 | foreach ($this->tree_service->all() as $tree) { |
||
152 | /** @var Tree $tree */ |
||
153 | |||
154 | if ($tree->getPreference(self::TREE_PREFERENCE_NAME) !== '1') { |
||
155 | continue; |
||
156 | } |
||
157 | |||
158 | $webmaster = $this->user_service->find((int) $tree->getPreference('WEBMASTER_USER_ID')); |
||
159 | if ($webmaster === null) { |
||
160 | continue; |
||
161 | } |
||
162 | I18N::init($webmaster->getPreference('language')); |
||
163 | |||
164 | $error_logs = $this->healthcheck_service->errorLogs($tree, $nb_days); |
||
165 | $nb_errors = $error_logs->sum('nblogs'); |
||
166 | |||
167 | $view_params = array_merge($view_params_site, [ |
||
168 | 'tree' => $tree, |
||
169 | 'total_by_type' => $this->healthcheck_service->countByRecordType($tree), |
||
170 | 'change_by_type' => $this->healthcheck_service->changesByRecordType($tree, $nb_days), |
||
171 | 'error_logs' => $error_logs, |
||
172 | 'nb_errors' => $nb_errors |
||
173 | ]); |
||
174 | |||
175 | $res = $res && $this->email_service->send( |
||
176 | new TreeUser($tree), |
||
177 | $webmaster, |
||
178 | new NoReplyUser(), |
||
179 | I18N::translate('Health Check Report') . ' - ' . I18N::translate('Tree %s', $tree->name()), |
||
180 | view($this->module->name() . '::tasks/healthcheck/email-healthcheck-text', $view_params), |
||
181 | view($this->module->name() . '::tasks/healthcheck/email-healthcheck-html', $view_params) |
||
182 | ); |
||
183 | } |
||
184 | |||
185 | return $res; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * {@inheritDoc} |
||
190 | * @see \MyArtJaub\Webtrees\Module\AdminTasks\Model\ConfigurableTaskInterface::configView() |
||
191 | */ |
||
192 | public function configView(ServerRequestInterface $request): string |
||
193 | { |
||
194 | return view($this->module->name() . '::tasks/healthcheck/config', [ |
||
195 | 'all_trees' => $this->tree_service->all() |
||
196 | ]); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * {@inheritDoc} |
||
201 | * @see \MyArtJaub\Webtrees\Module\AdminTasks\Model\ConfigurableTaskInterface::updateConfig() |
||
202 | */ |
||
203 | public function updateConfig(ServerRequestInterface $request, TaskSchedule $task_schedule): bool |
||
204 | { |
||
227 |