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.
Completed
Pull Request — master (#290)
by
unknown
01:34
created

Task::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Queue\Management;
4
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Dev\BuildTask;
7
use SilverStripe\ORM\DB;
8
use SilverStripe\ORM\Queries\SQLSelect;
9
use SilverStripe\ORM\Queries\SQLUpdate;
10
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
11
use Symbiote\QueuedJobs\Services\QueuedJob;
12
13
/**
14
 * Class Task
15
 *
16
 * Tool for changing job statuses
17
 *
18
 * @package App\Queue\Management
19
 */
20
class Task extends BuildTask
21
{
22
23
    /**
24
     * @var string
25
     */
26
    private static $segment = 'queue-management-job-status';
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...
27
28
    /**
29
     * @return string
30
     */
31
    public function getDescription(): string
32
    {
33
        return 'Update job status for specific job type';
34
    }
35
36
    /**
37
     * @param HTTPRequest $request
38
     */
39
    public function run($request): void // phpcs:ignore SlevomatCodingStandard.TypeHints
40
    {
41
        /** @var QueuedJobDescriptor $job */
42
        $job = QueuedJobDescriptor::singleton();
43
        $statuses = $job->getJobStatusValues();
0 ignored issues
show
Documentation Bug introduced by
The method getJobStatusValues does not exist on object<Symbiote\QueuedJo...ts\QueuedJobDescriptor>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
44
        sort($statuses, SORT_STRING);
45
46
        $currentStatuses = array_diff($statuses, [
47
            QueuedJob::STATUS_COMPLETE,
48
        ]);
49
50
        // job implementations
51
        $query = SQLSelect::create(
52
            'DISTINCT `Implementation`',
53
            'QueuedJobDescriptor',
54
            ['`JobStatus` != ?' => QueuedJob::STATUS_COMPLETE],
55
            ['Implementation' => 'ASC']
56
        );
57
58
        $results = $query->execute();
59
60
        $implementations = [];
61
62
        // Add job types
63
        while ($result = $results->next()) {
64
            $implementation = $result['Implementation'];
65
66
            if (!$implementation) {
67
                continue;
68
            }
69
70
            $implementations[] = $result['Implementation'];
71
        }
72
73
        if (count($implementations) === 0) {
74
            echo 'No job implementations found.';
75
76
            return;
77
        }
78
79
        $implementation = $request->postVar('implementation');
80
        $currentStatus = $request->postVar('currentStatus');
81
        $status = $request->postVar('status');
82
83
        if ($implementation
84
            && $status
85
            && ($implementation === 'all' || in_array($implementation, $implementations))
86
            && ($currentStatus === 'any' || in_array($currentStatus, $currentStatuses))
87
            && in_array($status, $statuses)
88
        ) {
89
            $where = [
90
                ['`JobStatus` != ?' => QueuedJob::STATUS_COMPLETE],
91
            ];
92
93
            // Filter by implementation
94
            $where[] = $implementation === 'all'
95
                ? '`Implementation` IN ' . sprintf(
96
                    "('%s')",
97
                    str_replace('\\', '\\\\', implode("','", $implementations))
98
                )
99
                : ['`Implementation`' => $implementation];
100
101
            // Filter by status
102
            if ($currentStatus !== 'any') {
103
                $where[] = ['`JobStatus`' => $currentStatus];
104
            }
105
106
            // Assemble query
107
            $query = SQLUpdate::create(
108
                'QueuedJobDescriptor',
109
                [
110
                    'JobStatus' => $status,
111
                    // make sure to reset all data which is related to job management
112
                    // job lock
113
                    'Worker' => null,
114
                    'Expiry' => null,
115
                    // resume / pause
116
                    'ResumeCounts' => 0,
117
                    // broken job notification
118
                    'NotifiedBroken' => 0,
119
                ],
120
                $where
121
            );
122
123
            $query->execute();
124
125
            echo sprintf('Job status updated (%d rows affected).', DB::affected_rows());
126
127
            return;
128
        }
129
130
        echo '<form action="" method="post">';
131
132
        echo '<p>Job type</p>';
133
134
        echo '<select name="implementation">';
135
        echo '<option value="all">All job types</option>';
136
137
        foreach ($implementations as $item) {
138
            echo sprintf('<option value="%s">%s</option>', $item, $item);
139
        }
140
141
        echo '</select>';
142
143
        echo '<br />';
144
145
        echo '<p>Current status</p>';
146
147
        echo '<select name="currentStatus">';
148
        echo '<option value="any">Any status (except Complete)</option>';
149
150
        foreach ($currentStatuses as $item) {
151
            echo sprintf('<option value="%s">%s</option>', $item, $item);
152
        }
153
154
        echo '</select>';
155
156
        echo '<br />';
157
158
        echo '<p>Update status to</p>';
159
160
        echo '<select name="status">';
161
162
        foreach ($statuses as $item) {
163
            echo sprintf('<option value="%s">%s</option>', $item, $item);
164
        }
165
166
        echo '</select>';
167
168
        echo '<br />';
169
170
        echo '<br />';
171
172
        echo 'Submitting will apply change immediately:<br><br>';
173
        echo '<button type="submit">Update status</button>';
174
        echo '</form>';
175
    }
176
}
177