Passed
Branch feature/2.0 (ef99fd)
by Jonathan
11:25
created

TasksList::handle()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 68
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 47
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 68
rs 8.5341

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-2020, 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 Fisharebest\Webtrees\I18N;
18
use Fisharebest\Webtrees\Exceptions\HttpNotFoundException;
19
use Fisharebest\Webtrees\Services\DatatablesService;
20
use Fisharebest\Webtrees\Services\ModuleService;
21
use MyArtJaub\Webtrees\Module\AdminTasks\AdminTasksModule;
22
use MyArtJaub\Webtrees\Module\AdminTasks\Model\TaskSchedule;
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
    /**
35
     * @var AdminTasksModule $module
36
     */
37
    private $module;
38
    
39
    /**
40
     * @var TaskScheduleService $taskschedules_service
41
     */
42
    private $taskschedules_service;
43
    
44
    /**
45
     * @var DatatablesService $datatables_service
46
     */
47
    private $datatables_service;
48
    
49
    /**
50
     * Constructor for TasksList Request Handler
51
     *
52
     * @param ModuleService $module_service
53
     * @param TaskScheduleService $taskschedules_service
54
     * @param DatatablesService $datatables_service
55
     */
56
    public function __construct(
57
        ModuleService $module_service,
58
        TaskScheduleService $taskschedules_service,
59
        DatatablesService $datatables_service
60
    ) {
61
        $this->module = $module_service->findByInterface(AdminTasksModule::class)->first();
62
        $this->taskschedules_service = $taskschedules_service;
63
        $this->datatables_service = $datatables_service;
64
    }
65
    
66
    /**
67
     * {@inheritDoc}
68
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
69
     */
70
    public function handle(ServerRequestInterface $request): ResponseInterface
71
    {
72
        if ($this->module === null) {
73
            throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
74
        }
75
        
76
        $task_schedules = $this->taskschedules_service->all(true, true)
77
            ->map(function (TaskSchedule $value) {
78
79
                $row = $value->toArray();
80
                $task = $this->taskschedules_service->findTask($row['task_id']);
81
                $row['task_name'] = $task !== null ? $task->name() : I18N::translate('Task not found');
82
                return $row;
83
            });
84
            
85
        $search_columns = ['task_name'];
86
        $sort_columns   = ['task_name', 'enabled', 'last_run'];
87
        $module_name = $this->module->name();
88
        
89
        $callback = function (array $row) use ($module_name): array {
90
91
            $row['frequency']->setLocale(I18N::locale()->code());
92
            
93
            $task_options_params = [
94
                'task_sched_id' => $row['id'],
95
                'task_sched_enabled' => $row['enabled'],
96
                'task_edit_route' => route(TaskEditPage::class, ['task' => $row['id']]),
97
                'task_status_route' => route(TaskStatusAction::class, [
98
                    'task' => $row['id'],
99
                    'enable' => $row['enabled'] ? 0 : 1
100
                ])
101
            ];
102
            
103
            $task_run_params = [
104
                'task_sched_id' => $row['id'],
105
                'run_route' => route(TaskTrigger::class, [
106
                    'task'  =>  $row['task_id'],
107
                    'force' =>  $this->module->getPreference('MAJ_AT_FORCE_EXEC_TOKEN')
108
                ])
109
            ];
110
            
111
            $datum = [
112
                view($module_name . '::admin/tasks-table-options', $task_options_params),
113
                view($module_name . '::components/yes-no-icons', ['yes' => $row['enabled']]),
114
                '<span dir="auto">' . e($row['task_name']) . '</span>',
115
                $row['last_run']->unix() === 0 ?
116
                view('components/datetime', ['timestamp' => $row['last_run']]) :
117
                view('components/datetime-diff', ['timestamp' => $row['last_run']]),
118
                view($module_name . '::components/yes-no-icons', ['yes' => $row['last_result']]),
119
                '<span dir="auto">' . e($row['frequency']->cascade()->forHumans()) . '</span>',
120
                $row['nb_occurrences'] > 0 ? I18N::number($row['nb_occurrences']) : I18N::translate('Unlimited'),
121
                view($module_name . '::components/yes-no-icons', [
122
                    'yes' => $row['is_running'],
123
                    'text_yes' => I18N::translate('Running'),
124
                    'text_no' => I18N::translate('Not running')
125
                ]),
126
                view($module_name . '::admin/tasks-table-run', $task_run_params)
127
            ];
128
            
129
            return $datum;
130
        };
131
        
132
        return $this->datatables_service->handleCollection(
133
            $request,
134
            $task_schedules,
135
            $search_columns,
136
            $sort_columns,
137
            $callback
138
        );
139
    }
140
}
141