Conditions | 16 |
Paths | 39 |
Total Lines | 137 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
39 | public function run($request): void // phpcs:ignore SlevomatCodingStandard.TypeHints |
||
40 | { |
||
41 | /** @var QueuedJobDescriptor $job */ |
||
42 | $job = QueuedJobDescriptor::singleton(); |
||
43 | $statuses = $job->getJobStatusValues(); |
||
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 |