Passed
Push — feature/code-analysis ( e964aa...4fe35d )
by Jonathan
14:33
created

TasksList::handle()   B

Complexity

Conditions 8
Paths 2

Size

Total Lines 60
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 46
nc 2
nop 1
dl 0
loc 60
rs 7.9337
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\Http\Exceptions\HttpNotFoundException;
20
use Fisharebest\Webtrees\Services\ModuleService;
21
use MyArtJaub\Webtrees\Common\Tasks\TaskSchedule;
22
use MyArtJaub\Webtrees\Module\AdminTasks\AdminTasksModule;
23
use MyArtJaub\Webtrees\Module\AdminTasks\Services\TaskScheduleService;
24
use Psr\Http\Message\ResponseInterface;
25
use Psr\Http\Message\ServerRequestInterface;
26
use Psr\Http\Server\RequestHandlerInterface;
27
28
/**
29
 * Request handler for listing task schedules
30
 *
31
 */
32
class TasksList implements RequestHandlerInterface
33
{
34
    private ?AdminTasksModule $module;
35
    private TaskScheduleService $taskschedules_service;
36
37
    /**
38
     * Constructor for TasksList Request Handler
39
     *
40
     * @param ModuleService $module_service
41
     * @param TaskScheduleService $taskschedules_service
42
     */
43
    public function __construct(
44
        ModuleService $module_service,
45
        TaskScheduleService $taskschedules_service
46
    ) {
47
        $this->module = $module_service->findByInterface(AdminTasksModule::class)->first();
48
        $this->taskschedules_service = $taskschedules_service;
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
54
     */
55
    public function handle(ServerRequestInterface $request): ResponseInterface
56
    {
57
        if ($this->module === null) {
58
            throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
59
        }
60
61
        $module = $this->module;
62
        $module_name = $this->module->name();
63
        return response(['data' => $this->taskschedules_service->all(true, true)
64
            ->map(function (TaskSchedule $schedule) use ($module, $module_name): array {
65
                $task = $this->taskschedules_service->findTask($schedule->taskId());
66
                $task_name = $task !== null ? $task->name() : I18N::translate('Task not found');
67
68
                return [
69
                    'edit' =>   view($module_name . '::admin/tasks-table-options', [
70
                        'task_sched_id' => $schedule->id(),
71
                        'task_sched_enabled' => $schedule->isEnabled(),
72
                        'task_edit_route' => route(TaskEditPage::class, ['task' => $schedule->id()]),
73
                        'task_status_route' => route(TaskStatusAction::class, [
74
                            'task' => $schedule->id(),
75
                            'enable' => $schedule->isEnabled() ? 0 : 1
76
                        ])
77
                    ]),
78
                    'status'    =>  [
79
                        'display'   =>  view($module_name . '::components/yes-no-icons', [
80
                            'yes' => $schedule->isEnabled()
81
                        ]),
82
                        'raw'       =>  $schedule->isEnabled() ? 1 : 0
83
                    ],
84
                    'task_name' =>  [
85
                        'display'   =>  '<bdi>' . e($task_name) . '</bdi>',
86
                        'raw'       =>  $task_name
87
                    ],
88
                    'last_run'  =>  [
89
                        'display'   =>  $schedule->lastRunTime()->timestamp() === 0 ?
90
                            view('components/datetime', ['timestamp' => $schedule->lastRunTime()]) :
91
                            view('components/datetime-diff', ['timestamp' => $schedule->lastRunTime()]),
92
                        'raw'       =>  $schedule->lastRunTime()->timestamp()
93
                    ],
94
                    'last_result'   =>  [
95
                        'display'   => view($module_name . '::components/yes-no-icons', [
96
                            'yes' => $schedule->wasLastRunSuccess()
97
                        ]),
98
                        'raw'       =>  $schedule->wasLastRunSuccess() ? 1 : 0
99
                    ],
100
                    'frequency' =>
101
                        '<bdi>' . e(CarbonInterval::minutes($schedule->frequency())->cascade()->forHumans()) . '</bdi>',
102
                    'nb_occurrences'    =>  $schedule->remainingOccurrences() > 0 ?
103
                        I18N::number($schedule->remainingOccurrences()) :
104
                        I18N::translate('Unlimited'),
105
                    'running'   =>  view($module_name . '::components/yes-no-icons', [
106
                        'yes' => $schedule->isRunning(),
107
                        'text_yes' => I18N::translate('Running'),
108
                        'text_no' => I18N::translate('Not running')
109
                    ]),
110
                    'run'       =>  view($module_name . '::admin/tasks-table-run', [
111
                        'task_sched_id' => $schedule->id(),
112
                        'run_route' => route(TaskTrigger::class, [
113
                            'task'  =>  $schedule->taskId(),
114
                            'force' =>  $module->getPreference('MAJ_AT_FORCE_EXEC_TOKEN')
115
                        ])
116
                    ])
117
                ];
118
            })
119
        ]);
120
    }
121
}
122