Passed
Push — main ( 195893...a2ce2d )
by Jonathan
06:26 queued 01:03
created
app/Module/AdminTasks/Http/RequestHandlers/TaskEditAction.php 2 patches
Indentation   +144 added lines, -144 removed lines patch added patch discarded remove patch
@@ -34,148 +34,148 @@
 block discarded – undo
34 34
  */
35 35
 class TaskEditAction implements RequestHandlerInterface
36 36
 {
37
-    private ?AdminTasksModule $module;
38
-    private TaskScheduleService $taskschedules_service;
39
-
40
-    /**
41
-     * Constructor for TaskEditAction Request Handler
42
-     *
43
-     * @param ModuleService $module_service
44
-     * @param TaskScheduleService $taskschedules_service
45
-     */
46
-    public function __construct(ModuleService $module_service, TaskScheduleService $taskschedules_service)
47
-    {
48
-        $this->module = $module_service->findByInterface(AdminTasksModule::class)->first();
49
-        $this->taskschedules_service = $taskschedules_service;
50
-    }
51
-
52
-    /**
53
-     * {@inheritDoc}
54
-     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
55
-     */
56
-    public function handle(ServerRequestInterface $request): ResponseInterface
57
-    {
58
-        $admin_config_route = route(AdminConfigPage::class);
59
-
60
-        if ($this->module === null) {
61
-            FlashMessages::addMessage(
62
-                I18N::translate('The attached module could not be found.'),
63
-                'danger'
64
-            );
65
-            return redirect($admin_config_route);
66
-        }
67
-
68
-        $task_sched_id = Validator::attributes($request)->integer('task', -1);
69
-        $task_schedule = $this->taskschedules_service->find($task_sched_id);
70
-
71
-        if ($task_schedule === null) {
72
-            FlashMessages::addMessage(
73
-                I18N::translate('The task shedule with ID “%s” does not exist.', I18N::number($task_sched_id)),
74
-                'danger'
75
-            );
76
-            return redirect($admin_config_route);
77
-        }
78
-
79
-        $success = $this->updateGeneralSettings($task_schedule, $request);
80
-        $success = $success && $this->updateSpecificSettings($task_schedule, $request);
81
-
82
-        if ($success) {
83
-            FlashMessages::addMessage(
84
-                I18N::translate('The scheduled task has been successfully updated.'),
85
-                'success'
86
-            );
87
-            //phpcs:ignore Generic.Files.LineLength.TooLong
88
-            Log::addConfigurationLog('Module ' . $this->module->title() . ' : Task Schedule “' . $task_schedule->id() . '” has been updated.');
89
-        }
90
-
91
-        return redirect($admin_config_route);
92
-    }
93
-
94
-    /**
95
-     * Update general settings for the task, based on the request parameters
96
-     *
97
-     * @param TaskSchedule $task_schedule
98
-     * @param ServerRequestInterface $request
99
-     * @return bool
100
-     */
101
-    private function updateGeneralSettings(TaskSchedule $task_schedule, ServerRequestInterface $request): bool
102
-    {
103
-        if ($this->module === null) {
104
-            return false;
105
-        }
106
-
107
-        $frequency = Validator::parsedBody($request)->integer('frequency', 0);
108
-        if ($frequency > 0) {
109
-            $task_schedule->setFrequency($frequency);
110
-        } else {
111
-            FlashMessages::addMessage(I18N::translate('The frequency is not in a valid format.'), 'danger');
112
-        }
113
-
114
-        $is_limited = Validator::parsedBody($request)->boolean('is_limited', false);
115
-        $nb_occur = Validator::parsedBody($request)->integer('nb_occur', 1);
116
-        if ($is_limited) {
117
-            if ($nb_occur > 0) {
118
-                $task_schedule->setRemainingOccurrences($nb_occur);
119
-            } else {
120
-                FlashMessages::addMessage(
121
-                    I18N::translate('The number of remaining occurrences is not in a valid format.'),
122
-                    'danger'
123
-                );
124
-            }
125
-        } else {
126
-            $task_schedule->setRemainingOccurrences(0);
127
-        }
128
-
129
-        try {
130
-            $this->taskschedules_service->update($task_schedule);
131
-            return true;
132
-        } catch (Throwable $ex) {
133
-            Log::addErrorLog(
134
-                sprintf(
135
-                    'Error while updating the Task Schedule "%s". Exception: %s',
136
-                    $task_schedule->id(),
137
-                    $ex->getMessage()
138
-                )
139
-            );
140
-        }
141
-
142
-        FlashMessages::addMessage(I18N::translate('An error occured while updating the scheduled task.'), 'danger');
143
-        //@phpcs:ignore Generic.Files.LineLength.TooLong
144
-        Log::addConfigurationLog('Module ' . $this->module->title() . ' : Task Schedule “' . $task_schedule->id() . '” could not be updated. See error log.');
145
-        return false;
146
-    }
147
-
148
-    /**
149
-     * Update general settings for the task, based on the request parameters
150
-     *
151
-     * @param TaskSchedule $task_schedule
152
-     * @param ServerRequestInterface $request
153
-     * @return bool
154
-     */
155
-    private function updateSpecificSettings(TaskSchedule $task_schedule, ServerRequestInterface $request): bool
156
-    {
157
-        if ($this->module === null) {
158
-            return false;
159
-        }
160
-
161
-        $task = $this->taskschedules_service->findTask($task_schedule->taskId());
162
-        if ($task === null || !($task instanceof ConfigurableTaskInterface)) {
163
-            return true;
164
-        }
165
-
166
-        /** @var \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface&\MyArtJaub\Webtrees\Contracts\Tasks\ConfigurableTaskInterface $task */
167
-        if (!$task->updateConfig($request, $task_schedule)) {
168
-            FlashMessages::addMessage(
169
-                I18N::translate(
170
-                    'An error occured while updating the specific settings of administrative task “%s”.',
171
-                    $task->name()
172
-                ),
173
-                'danger'
174
-            );
175
-            //phpcs:ignore Generic.Files.LineLength.TooLong
176
-            Log::addConfigurationLog('Module ' . $this->module->title() . ' : AdminTask “' . $task->name() . '” specific settings could not be updated. See error log.');
177
-        }
178
-
179
-        return true;
180
-    }
37
+	private ?AdminTasksModule $module;
38
+	private TaskScheduleService $taskschedules_service;
39
+
40
+	/**
41
+	 * Constructor for TaskEditAction Request Handler
42
+	 *
43
+	 * @param ModuleService $module_service
44
+	 * @param TaskScheduleService $taskschedules_service
45
+	 */
46
+	public function __construct(ModuleService $module_service, TaskScheduleService $taskschedules_service)
47
+	{
48
+		$this->module = $module_service->findByInterface(AdminTasksModule::class)->first();
49
+		$this->taskschedules_service = $taskschedules_service;
50
+	}
51
+
52
+	/**
53
+	 * {@inheritDoc}
54
+	 * @see \Psr\Http\Server\RequestHandlerInterface::handle()
55
+	 */
56
+	public function handle(ServerRequestInterface $request): ResponseInterface
57
+	{
58
+		$admin_config_route = route(AdminConfigPage::class);
59
+
60
+		if ($this->module === null) {
61
+			FlashMessages::addMessage(
62
+				I18N::translate('The attached module could not be found.'),
63
+				'danger'
64
+			);
65
+			return redirect($admin_config_route);
66
+		}
67
+
68
+		$task_sched_id = Validator::attributes($request)->integer('task', -1);
69
+		$task_schedule = $this->taskschedules_service->find($task_sched_id);
70
+
71
+		if ($task_schedule === null) {
72
+			FlashMessages::addMessage(
73
+				I18N::translate('The task shedule with ID “%s” does not exist.', I18N::number($task_sched_id)),
74
+				'danger'
75
+			);
76
+			return redirect($admin_config_route);
77
+		}
78
+
79
+		$success = $this->updateGeneralSettings($task_schedule, $request);
80
+		$success = $success && $this->updateSpecificSettings($task_schedule, $request);
81
+
82
+		if ($success) {
83
+			FlashMessages::addMessage(
84
+				I18N::translate('The scheduled task has been successfully updated.'),
85
+				'success'
86
+			);
87
+			//phpcs:ignore Generic.Files.LineLength.TooLong
88
+			Log::addConfigurationLog('Module ' . $this->module->title() . ' : Task Schedule “' . $task_schedule->id() . '” has been updated.');
89
+		}
90
+
91
+		return redirect($admin_config_route);
92
+	}
93
+
94
+	/**
95
+	 * Update general settings for the task, based on the request parameters
96
+	 *
97
+	 * @param TaskSchedule $task_schedule
98
+	 * @param ServerRequestInterface $request
99
+	 * @return bool
100
+	 */
101
+	private function updateGeneralSettings(TaskSchedule $task_schedule, ServerRequestInterface $request): bool
102
+	{
103
+		if ($this->module === null) {
104
+			return false;
105
+		}
106
+
107
+		$frequency = Validator::parsedBody($request)->integer('frequency', 0);
108
+		if ($frequency > 0) {
109
+			$task_schedule->setFrequency($frequency);
110
+		} else {
111
+			FlashMessages::addMessage(I18N::translate('The frequency is not in a valid format.'), 'danger');
112
+		}
113
+
114
+		$is_limited = Validator::parsedBody($request)->boolean('is_limited', false);
115
+		$nb_occur = Validator::parsedBody($request)->integer('nb_occur', 1);
116
+		if ($is_limited) {
117
+			if ($nb_occur > 0) {
118
+				$task_schedule->setRemainingOccurrences($nb_occur);
119
+			} else {
120
+				FlashMessages::addMessage(
121
+					I18N::translate('The number of remaining occurrences is not in a valid format.'),
122
+					'danger'
123
+				);
124
+			}
125
+		} else {
126
+			$task_schedule->setRemainingOccurrences(0);
127
+		}
128
+
129
+		try {
130
+			$this->taskschedules_service->update($task_schedule);
131
+			return true;
132
+		} catch (Throwable $ex) {
133
+			Log::addErrorLog(
134
+				sprintf(
135
+					'Error while updating the Task Schedule "%s". Exception: %s',
136
+					$task_schedule->id(),
137
+					$ex->getMessage()
138
+				)
139
+			);
140
+		}
141
+
142
+		FlashMessages::addMessage(I18N::translate('An error occured while updating the scheduled task.'), 'danger');
143
+		//@phpcs:ignore Generic.Files.LineLength.TooLong
144
+		Log::addConfigurationLog('Module ' . $this->module->title() . ' : Task Schedule “' . $task_schedule->id() . '” could not be updated. See error log.');
145
+		return false;
146
+	}
147
+
148
+	/**
149
+	 * Update general settings for the task, based on the request parameters
150
+	 *
151
+	 * @param TaskSchedule $task_schedule
152
+	 * @param ServerRequestInterface $request
153
+	 * @return bool
154
+	 */
155
+	private function updateSpecificSettings(TaskSchedule $task_schedule, ServerRequestInterface $request): bool
156
+	{
157
+		if ($this->module === null) {
158
+			return false;
159
+		}
160
+
161
+		$task = $this->taskschedules_service->findTask($task_schedule->taskId());
162
+		if ($task === null || !($task instanceof ConfigurableTaskInterface)) {
163
+			return true;
164
+		}
165
+
166
+		/** @var \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface&\MyArtJaub\Webtrees\Contracts\Tasks\ConfigurableTaskInterface $task */
167
+		if (!$task->updateConfig($request, $task_schedule)) {
168
+			FlashMessages::addMessage(
169
+				I18N::translate(
170
+					'An error occured while updating the specific settings of administrative task “%s”.',
171
+					$task->name()
172
+				),
173
+				'danger'
174
+			);
175
+			//phpcs:ignore Generic.Files.LineLength.TooLong
176
+			Log::addConfigurationLog('Module ' . $this->module->title() . ' : AdminTask “' . $task->name() . '” specific settings could not be updated. See error log.');
177
+		}
178
+
179
+		return true;
180
+	}
181 181
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -85,7 +85,7 @@  discard block
 block discarded – undo
85 85
                 'success'
86 86
             );
87 87
             //phpcs:ignore Generic.Files.LineLength.TooLong
88
-            Log::addConfigurationLog('Module ' . $this->module->title() . ' : Task Schedule “' . $task_schedule->id() . '” has been updated.');
88
+            Log::addConfigurationLog('Module '.$this->module->title().' : Task Schedule “'.$task_schedule->id().'” has been updated.');
89 89
         }
90 90
 
91 91
         return redirect($admin_config_route);
@@ -141,7 +141,7 @@  discard block
 block discarded – undo
141 141
 
142 142
         FlashMessages::addMessage(I18N::translate('An error occured while updating the scheduled task.'), 'danger');
143 143
         //@phpcs:ignore Generic.Files.LineLength.TooLong
144
-        Log::addConfigurationLog('Module ' . $this->module->title() . ' : Task Schedule “' . $task_schedule->id() . '” could not be updated. See error log.');
144
+        Log::addConfigurationLog('Module '.$this->module->title().' : Task Schedule “'.$task_schedule->id().'” could not be updated. See error log.');
145 145
         return false;
146 146
     }
147 147
 
@@ -173,7 +173,7 @@  discard block
 block discarded – undo
173 173
                 'danger'
174 174
             );
175 175
             //phpcs:ignore Generic.Files.LineLength.TooLong
176
-            Log::addConfigurationLog('Module ' . $this->module->title() . ' : AdminTask “' . $task->name() . '” specific settings could not be updated. See error log.');
176
+            Log::addConfigurationLog('Module '.$this->module->title().' : AdminTask “'.$task->name().'” specific settings could not be updated. See error log.');
177 177
         }
178 178
 
179 179
         return true;
Please login to merge, or discard this patch.