GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

QueuedTaskRunner::index()   B
last analyzed

Complexity

Conditions 7
Paths 17

Size

Total Lines 82

Duplication

Lines 16
Ratio 19.51 %

Importance

Changes 0
Metric Value
dl 16
loc 82
rs 7.4593
c 0
b 0
f 0
cc 7
nc 17
nop 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
namespace Symbiote\QueuedJobs\Controllers;
4
5
use SilverStripe\Admin\AdminRootController;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Control\HTTPRequest;
8
use SilverStripe\Core\Convert;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Dev\BuildTask;
11
use SilverStripe\Dev\DebugView;
12
use SilverStripe\Dev\TaskRunner;
13
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
14
use Symbiote\QueuedJobs\Jobs\RunBuildTaskJob;
15
use Symbiote\QueuedJobs\Services\QueuedJobService;
16
use Symbiote\QueuedJobs\Tasks\CreateQueuedJobTask;
17
use Symbiote\QueuedJobs\Tasks\DeleteAllJobsTask;
18
use Symbiote\QueuedJobs\Tasks\ProcessJobQueueChildTask;
19
use Symbiote\QueuedJobs\Tasks\ProcessJobQueueTask;
20
21
/**
22
 * Class QueuedTaskRunner
23
 *
24
 * @package Symbiote\QueuedJobs\Controllers
25
 */
26
class QueuedTaskRunner extends TaskRunner
27
{
28
    /**
29
     * @var array
30
     */
31
    private static $url_handlers = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
32
        'queue/$TaskName' => 'queueTask',
33
    ];
34
35
    /**
36
     * @var array
37
     */
38
    private static $allowed_actions = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
39
        'queueTask',
40
    ];
41
42
    /**
43
     * Tasks on this list will be available to be run only via browser
44
     *
45
     * @config
46
     * @var array
47
     */
48
    private static $task_blacklist = [
49
        ProcessJobQueueTask::class,
50
        ProcessJobQueueChildTask::class,
51
        CreateQueuedJobTask::class,
52
        DeleteAllJobsTask::class,
53
    ];
54
55
    /**
56
     * Tasks on this list will be available to be run only via jobs queue
57
     *
58
     * @config
59
     * @var array
60
     */
61
    private static $queued_only_tasks = [];
62
63
    public function index()
64
    {
65
        $tasks = $this->getTasks();
66
67
        $blacklist = (array) $this->config()->get('task_blacklist');
68
        $queuedOnlyList = (array) $this->config()->get('queued_only_tasks');
69
        $backlistedTasks = [];
70
        $queuedOnlyTasks = [];
71
72
        // Web mode
73
        if (!Director::is_cli()) {
74
            $renderer = new DebugView();
75
            echo $renderer->renderHeader();
76
            echo $renderer->renderInfo(
77
                "SilverStripe Development Tools: Tasks (QueuedJobs version)",
78
                Director::absoluteBaseURL()
0 ignored issues
show
Security Bug introduced by
It seems like \SilverStripe\Control\Director::absoluteBaseURL() targeting SilverStripe\Control\Director::absoluteBaseURL() can also be of type false; however, SilverStripe\Dev\DebugView::renderInfo() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
79
            );
80
            $base = Director::absoluteBaseURL();
81
82
            echo "<div class=\"options\">";
83
            echo "<h2>Queueable jobs</h2>\n";
84
            echo "<p>By default these jobs will be added the job queue, rather than run immediately</p>\n";
85
            echo "<ul>";
86
            foreach ($tasks as $task) {
87
                if (in_array($task['class'], $blacklist)) {
88
                    $backlistedTasks[] = $task;
89
90
                    continue;
91
                }
92
93
                if (in_array($task['class'], $queuedOnlyList)) {
94
                    $queuedOnlyTasks[] = $task;
95
96
                    continue;
97
                }
98
99
                $queueLink = $base . "dev/tasks/queue/" . $task['segment'];
100
                $immediateLink = $base . "dev/tasks/" . $task['segment'];
101
102
                echo "<li><p>";
103
                echo "<a href=\"$queueLink\">" . $task['title'] . "</a> <a style=\"font-size: 80%; padding-left: 20px\""
104
                    . " href=\"$immediateLink\">[run immediately]</a><br />";
105
                echo "<span class=\"description\">" . $task['description'] . "</span>";
106
                echo "</p></li>\n";
107
            }
108
            echo "</ul></div>";
109
110
            echo "<div class=\"options\">";
111
            echo "<h2>Non-queueable tasks</h2>\n";
112
            echo "<p>These tasks shouldn't be added the queuejobs queue, but you can run them immediately.</p>\n";
113
            echo "<ul>";
114 View Code Duplication
            foreach ($backlistedTasks as $task) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
                $immediateLink = $base . "dev/tasks/" . $task['segment'];
116
117
                echo "<li><p>";
118
                echo "<a href=\"$immediateLink\">" . $task['title'] . "</a><br />";
119
                echo "<span class=\"description\">" . $task['description'] . "</span>";
120
                echo "</p></li>\n";
121
            }
122
            echo "</ul></div>";
123
124
            echo "<div class=\"options\">";
125
            echo "<h2>Queueable only tasks</h2>\n";
126
            echo "<p>These tasks must be be added the queuejobs queue, running it immediately is not allowed.</p>\n";
127
            echo "<ul>";
128 View Code Duplication
            foreach ($queuedOnlyTasks as $task) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
                $queueLink = $base . "dev/tasks/queue/" . $task['segment'];
130
131
                echo "<li><p>";
132
                echo "<a href=\"$queueLink\">" . $task['title'] . "</a><br />";
133
                echo "<span class=\"description\">" . $task['description'] . "</span>";
134
                echo "</p></li>\n";
135
            }
136
            echo "</ul></div>";
137
138
            echo $renderer->renderFooter();
139
140
            // CLI mode - revert to default behaviour
141
        } else {
142
            return parent::index();
143
        }
144
    }
145
146
147
    /**
148
     * Adds a RunBuildTaskJob to the job queue for a given task
149
     *
150
     * @param HTTPRequest $request
151
     */
152
    public function queueTask($request)
153
    {
154
        $name = $request->param('TaskName');
155
        $tasks = $this->getTasks();
156
157
        $variables = $request->getVars();
158
        unset($variables['url']);
159
        unset($variables['flush']);
160
        unset($variables['flushtoken']);
161
        unset($variables['isDev']);
162
        $querystring = http_build_query($variables);
163
164
        $title = function ($content) {
165
            printf(Director::is_cli() ? "%s\n\n" : '<h1>%s</h1>', $content);
166
        };
167
168
        $message = function ($content) {
169
            printf(Director::is_cli() ? "%s\n" : '<p>%s</p>', $content);
170
        };
171
172
        foreach ($tasks as $task) {
173
            if ($task['segment'] == $name) {
174
                /** @var BuildTask $inst */
175
                $inst = Injector::inst()->create($task['class']);
176
                if (!$inst->isEnabled()) {
177
                    $message('The task is disabled');
178
                    return;
179
                }
180
181
                $title(sprintf('Queuing Task %s', $inst->getTitle()));
182
183
                $job = new RunBuildTaskJob($task['class'], $querystring);
184
                $jobID = Injector::inst()->get(QueuedJobService::class)->queueJob($job);
185
186
                $message('Done: queued with job ID ' . $jobID);
187
                $adminUrl = Director::baseURL() . AdminRootController::config()->get('url_base');
188
                $adminLink = $adminUrl . "/queuedjobs/" . str_replace('\\', '-', QueuedJobDescriptor::class);
189
                $message("Visit <a href=\"$adminLink\">queued jobs admin</a> to see job status");
190
                return;
191
            }
192
        }
193
194
        $message(sprintf('The build task "%s" could not be found', Convert::raw2xml($name)));
195
    }
196
}
197