TasksList::handle()   B
last analyzed

Complexity

Conditions 8
Paths 2

Size

Total Lines 61
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 47
nc 2
nop 1
dl 0
loc 61
rs 7.9119
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
/**
4
 * webtrees-lib: MyArtJaub library for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees
7
 * @subpackage AdminTasks
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2012-2022, Jonathan Jaubart
10
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
11
 */
12
13
declare(strict_types=1);
14
15
namespace MyArtJaub\Webtrees\Module\AdminTasks\Http\RequestHandlers;
16
17
use Carbon\CarbonInterval;
18
use Fisharebest\Webtrees\I18N;
19
use Fisharebest\Webtrees\Registry;
20
use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
21
use Fisharebest\Webtrees\Services\ModuleService;
22
use MyArtJaub\Webtrees\Common\Tasks\TaskSchedule;
23
use MyArtJaub\Webtrees\Module\AdminTasks\AdminTasksModule;
24
use MyArtJaub\Webtrees\Module\AdminTasks\Services\TaskScheduleService;
25
use Psr\Http\Message\ResponseInterface;
26
use Psr\Http\Message\ServerRequestInterface;
27
use Psr\Http\Server\RequestHandlerInterface;
28
29
/**
30
 * Request handler for listing task schedules
31
 *
32
 */
33
class TasksList implements RequestHandlerInterface
34
{
35
    private ?AdminTasksModule $module;
36
    private TaskScheduleService $taskschedules_service;
37
38
    /**
39
     * Constructor for TasksList Request Handler
40
     *
41
     * @param ModuleService $module_service
42
     * @param TaskScheduleService $taskschedules_service
43
     */
44
    public function __construct(
45
        ModuleService $module_service,
46
        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
        if ($this->module === null) {
59
            throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
60
        }
61
62
        $module = $this->module;
63
        $module_name = $this->module->name();
64
        return Registry::responseFactory()->response(['data' => $this->taskschedules_service->all(true, true)
65
            ->map(function (TaskSchedule $schedule) use ($module, $module_name): array {
66
                $task = $this->taskschedules_service->findTask($schedule->taskId());
67
                $task_name = $task !== null ? $task->name() : I18N::translate('Task not found');
68
                $last_run_timestamp = Registry::timestampFactory()->make($schedule->lastRunTime()->getTimestamp());
69
70
                return [
71
                    'edit' =>   view($module_name . '::admin/tasks-table-options', [
72
                        'task_sched_id' => $schedule->id(),
73
                        'task_sched_enabled' => $schedule->isEnabled(),
74
                        'task_edit_route' => route(TaskEditPage::class, ['task' => $schedule->id()]),
75
                        'task_status_route' => route(TaskStatusAction::class, [
76
                            'task' => $schedule->id(),
77
                            'enable' => $schedule->isEnabled() ? 0 : 1
78
                        ])
79
                    ]),
80
                    'status'    =>  [
81
                        'display'   =>  view($module_name . '::components/yes-no-icons', [
82
                            'yes' => $schedule->isEnabled()
83
                        ]),
84
                        'raw'       =>  $schedule->isEnabled() ? 1 : 0
85
                    ],
86
                    'task_name' =>  [
87
                        'display'   =>  '<bdi>' . e($task_name) . '</bdi>',
88
                        'raw'       =>  $task_name
89
                    ],
90
                    'last_run'  =>  [
91
                        'display'   =>  $last_run_timestamp->timestamp() === 0 ?
92
                            view('components/datetime', ['timestamp' => $last_run_timestamp]) :
93
                            view('components/datetime-diff', ['timestamp' => $last_run_timestamp]),
94
                        'raw'       =>  $schedule->lastRunTime()->getTimestamp()
95
                    ],
96
                    'last_result'   =>  [
97
                        'display'   => view($module_name . '::components/yes-no-icons', [
98
                            'yes' => $schedule->wasLastRunSuccess()
99
                        ]),
100
                        'raw'       =>  $schedule->wasLastRunSuccess() ? 1 : 0
101
                    ],
102
                    'frequency' =>
103
                        '<bdi>' . e(CarbonInterval::minutes($schedule->frequency())->cascade()->forHumans()) . '</bdi>',
104
                    'nb_occurrences'    =>  $schedule->remainingOccurrences() > 0 ?
105
                        I18N::number($schedule->remainingOccurrences()) :
106
                        I18N::translate('Unlimited'),
107
                    'running'   =>  view($module_name . '::components/yes-no-icons', [
108
                        'yes' => $schedule->isRunning(),
109
                        'text_yes' => I18N::translate('Running'),
110
                        'text_no' => I18N::translate('Not running')
111
                    ]),
112
                    'run'       =>  view($module_name . '::admin/tasks-table-run', [
113
                        'task_sched_id' => $schedule->id(),
114
                        'run_route' => route(TaskTrigger::class, [
115
                            'task'  =>  $schedule->taskId(),
116
                            'force' =>  $module->getPreference('MAJ_AT_FORCE_EXEC_TOKEN')
117
                        ])
118
                    ])
119
                ];
120
            })
121
        ]);
122
    }
123
}
124