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

TaskEditPage::handle()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 18
c 2
b 0
f 0
nc 4
nop 1
dl 0
loc 31
rs 9.3554
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) 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\Http\ViewResponseTrait;
20
use Fisharebest\Webtrees\Services\ModuleService;
21
use MyArtJaub\Webtrees\Module\AdminTasks\AdminTasksModule;
22
use MyArtJaub\Webtrees\Module\AdminTasks\Model\ConfigurableTaskInterface;
23
use MyArtJaub\Webtrees\Module\AdminTasks\Model\TaskInterface;
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 editing task schedules
31
 */
32
class TaskEditPage implements RequestHandlerInterface
33
{
34
    use ViewResponseTrait;
35
36
    /**
37
     * @var AdminTasksModule $module
38
     */
39
    private $module;
40
    
41
    /**
42
     * @var TaskScheduleService $taskschedules_service
43
     */
44
    private $taskschedules_service;
45
    
46
    /**
47
     * Constructor for TaskEditPage Request Handler
48
     *
49
     * @param ModuleService $module_service
50
     * @param TaskScheduleService $taskschedules_service
51
     */
52
    public function __construct(ModuleService $module_service, TaskScheduleService $taskschedules_service)
53
    {
54
        $this->module = $module_service->findByInterface(AdminTasksModule::class)->first();
55
        $this->taskschedules_service = $taskschedules_service;
56
    }
57
    
58
    /**
59
     * {@inheritDoc}
60
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
61
     */
62
    public function handle(ServerRequestInterface $request): ResponseInterface
63
    {
64
        $this->layout = 'layouts/administration';
65
        
66
        if ($this->module === null) {
67
            throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
68
        }
69
        
70
        $task_sched_id = (int) $request->getAttribute('task');
71
        $task_schedule = $this->taskschedules_service->find($task_sched_id);
72
        
73
        if ($task_schedule === null) {
74
            throw new HttpNotFoundException(I18N::translate('The Task schedule could not be found.'));
75
        }
76
        
77
        $task = $this->taskschedules_service->findTask($task_schedule->taskId());
78
        
79
        if ($task === null) {
80
            throw new HttpNotFoundException(I18N::translate('The Task schedule could not be found.'));
81
        }
82
        
83
        /** @var TaskInterface|ConfigurableTaskInterface $task */
84
        $has_task_config = $task instanceof ConfigurableTaskInterface;
85
        
86
        return $this->viewResponse($this->module->name() . '::admin/tasks-edit', [
87
            'module'            =>  $this->module,
88
            'title'             =>  I18N::translate('Edit the administrative task') . ' - ' . $task->name(),
0 ignored issues
show
Bug introduced by
The method name() does not exist on MyArtJaub\Webtrees\Modul...nfigurableTaskInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to MyArtJaub\Webtrees\Modul...nfigurableTaskInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
            'title'             =>  I18N::translate('Edit the administrative task') . ' - ' . $task->/** @scrutinizer ignore-call */ name(),
Loading history...
89
            'task_schedule'     =>  $task_schedule,
90
            'task'              =>  $task,
91
            'has_task_config'   =>  $has_task_config,
92
            'task_config_view'  =>  $has_task_config ? '' : $task->configView($request)
0 ignored issues
show
Bug introduced by
The method configView() does not exist on MyArtJaub\Webtrees\Modul...sks\Model\TaskInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to MyArtJaub\Webtrees\Modul...sks\Model\TaskInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
            'task_config_view'  =>  $has_task_config ? '' : $task->/** @scrutinizer ignore-call */ configView($request)
Loading history...
93
        ]);
94
    }
95
}
96