| Total Complexity | 40 |
| Total Lines | 244 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
Complex classes like CronJob often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CronJob, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class CronJob extends DataObject |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private static $singular_name = 'Scheduled Job'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | private static $plural_name = 'Scheduled Jobs'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | private static $table_name = 'CronJob'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array<string, string> |
||
| 52 | */ |
||
| 53 | private static $db = [ |
||
| 54 | 'TaskClass' => 'Varchar(255)', |
||
| 55 | 'Title' => 'Varchar(255)', |
||
| 56 | 'Category' => 'Varchar(255)', |
||
| 57 | 'Description' => 'Varchar(255)', |
||
| 58 | 'Disabled' => 'Boolean', |
||
| 59 | ]; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | private static $default_sort = 'TaskClass ASC'; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array<string, string> |
||
| 68 | */ |
||
| 69 | private static $summary_fields = [ |
||
| 70 | 'Title' => 'Title', |
||
| 71 | 'Category' => 'Category', |
||
| 72 | 'IsDisabled' => 'IsDisabled', |
||
| 73 | 'Description' => 'Description', |
||
| 74 | 'LastResult.Created' => 'Last Run', |
||
| 75 | 'NextRun' => 'Next Run', |
||
| 76 | ]; |
||
| 77 | |||
| 78 | public function canView($member = null, $context = []) |
||
| 79 | { |
||
| 80 | return Permission::check('CMS_ACCESS_SimpleJobsAdmin', 'any', $member); |
||
| 81 | } |
||
| 82 | |||
| 83 | public function canEdit($member = null, $context = []) |
||
| 84 | { |
||
| 85 | return Permission::check('CMS_ACCESS_SimpleJobsAdmin', 'any', $member); |
||
| 86 | } |
||
| 87 | |||
| 88 | public function canCreate($member = null, $context = []) |
||
| 89 | { |
||
| 90 | return Permission::check('CMS_ACCESS_SimpleJobsAdmin', 'any', $member); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param Member $member |
||
| 95 | * @return boolean |
||
| 96 | */ |
||
| 97 | public function canDelete($member = null) |
||
| 98 | { |
||
| 99 | return false; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return array<class-string> |
||
| 104 | */ |
||
| 105 | public static function allTasks(): array |
||
| 106 | { |
||
| 107 | return ClassInfo::implementorsOf(CronTask::class); |
||
| 108 | } |
||
| 109 | |||
| 110 | public function triggerManually(): string |
||
| 111 | { |
||
| 112 | $inst = $this->TaskInstance(); |
||
| 113 | /** @var void|null|string|bool $result */ |
||
| 114 | $result = $inst->process(); |
||
| 115 | if ($result !== null) { |
||
| 116 | if ($result === false) { |
||
| 117 | return 'Task failed'; |
||
| 118 | } |
||
| 119 | if (is_string($result)) { |
||
| 120 | return $result; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | return "Task has been triggered"; |
||
| 124 | } |
||
| 125 | |||
| 126 | public function getCMSActions() |
||
| 127 | { |
||
| 128 | $actions = parent::getCMSActions(); |
||
| 129 | if (class_exists(CustomAction::class) && Permission::check('ADMIN')) { |
||
| 130 | $triggerManually = new CustomAction('triggerManually', 'Trigger manually'); |
||
| 131 | $triggerManually->setConfirmation("Are you sure you want to trigger the task?"); |
||
| 132 | $actions->push($triggerManually); |
||
| 133 | } |
||
| 134 | return $actions; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @return FieldList |
||
| 139 | */ |
||
| 140 | public function getCMSFields() |
||
| 141 | { |
||
| 142 | $fields = parent::getCMSFields(); |
||
| 143 | $fields->makeFieldReadonly('Title'); |
||
| 144 | $fields->makeFieldReadonly('Category'); |
||
| 145 | $fields->makeFieldReadonly('Description'); |
||
| 146 | $fields->makeFieldReadonly('TaskClass'); |
||
| 147 | if ($this->IsSystemDisabled()) { |
||
| 148 | $fields->makeFieldReadonly('Disabled'); |
||
| 149 | } |
||
| 150 | |||
| 151 | // Display results |
||
| 152 | $resultsGridConfig = GridFieldConfig_RecordViewer::create(); |
||
| 153 | $resultsGrid = new GridField('Results', 'Results', $this->AllResults(), $resultsGridConfig); |
||
| 154 | $fields->addFieldToTab('Root.Results', $resultsGrid); |
||
| 155 | |||
| 156 | // Optionally provide a list of to be affected items |
||
| 157 | $taskClass = $this->TaskClass; |
||
| 158 | if (method_exists($taskClass, 'getJobRecords')) { |
||
| 159 | $records = $taskClass::getJobRecords(); |
||
| 160 | if ($records) { |
||
| 161 | $recordsGridConfig = GridFieldConfig_RecordViewer::create(); |
||
| 162 | $recordsGrid = new GridField('Records', 'Records', $records, $recordsGridConfig); |
||
| 163 | $fields->addFieldToTab('Root.Records', $recordsGrid); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | return $fields; |
||
| 168 | } |
||
| 169 | |||
| 170 | public static function regenerateFromClasses(bool $update = false): void |
||
| 171 | { |
||
| 172 | $list = self::allTasks(); |
||
| 173 | foreach ($list as $class) { |
||
| 174 | $obj = self::getByTaskClass($class); |
||
| 175 | if ($obj && $update === false) { |
||
| 176 | continue; |
||
| 177 | } |
||
| 178 | if (!$obj) { |
||
| 179 | $obj = new CronJob(); |
||
| 180 | } |
||
| 181 | $obj->TaskClass = $class; |
||
| 182 | $obj->Title = method_exists($class, 'getJobTitle') ? $class::getJobTitle() : $class; |
||
| 183 | $obj->Category = method_exists($class, 'getJobCategory') ? $class::getJobCategory() : 'general'; |
||
| 184 | $obj->Description = method_exists($class, 'getJobDescription') ? $class::getJobDescription() : null; |
||
| 185 | $obj->write(); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | public static function getByTaskClass(string $class): ?CronJob |
||
| 190 | { |
||
| 191 | /** @var CronJob|null $obj */ |
||
| 192 | $obj = self::get()->filter('TaskClass', $class)->first(); |
||
| 193 | return $obj; |
||
| 194 | } |
||
| 195 | |||
| 196 | public function IsDisabled(): bool |
||
| 202 | } |
||
| 203 | |||
| 204 | public function IsTaskDisabled(): bool |
||
| 205 | { |
||
| 206 | $taskClass = $this->TaskClass; |
||
| 207 | if ($taskClass && method_exists($taskClass, 'IsDisabled')) { |
||
| 208 | return $taskClass::IsDisabled(); |
||
| 209 | } |
||
| 210 | return false; |
||
| 211 | } |
||
| 212 | |||
| 213 | public function IsSystemDisabled(): bool |
||
| 214 | { |
||
| 215 | if ($this->IsTaskDisabled()) { |
||
| 216 | return true; |
||
| 217 | } |
||
| 218 | $disabledTask = Config::inst()->get(SimpleJobsController::class, 'disabled_tasks'); |
||
| 219 | if (in_array($this->TaskClass, $disabledTask)) { |
||
| 220 | return true; |
||
| 221 | } |
||
| 222 | return false; |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @return CronTask |
||
| 227 | */ |
||
| 228 | public function TaskInstance(): CronTask |
||
| 237 | } |
||
| 238 | |||
| 239 | public function NextRun(): string |
||
| 240 | { |
||
| 241 | if ($this->IsDisabled()) { |
||
| 242 | return ''; |
||
| 243 | } |
||
| 244 | $task = $this->TaskInstance(); |
||
| 245 | $cron = new CronExpression($task->getSchedule()); |
||
| 246 | return $cron->getNextRunDate()->format('Y-m-d H:i:s'); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @return DataList |
||
| 251 | */ |
||
| 252 | public function AllResults() |
||
| 258 | } |
||
| 259 | |||
| 260 | public function LastResult(): ?CronTaskResult |
||
| 261 | { |
||
| 262 | /** @var CronTaskResult|null $result */ |
||
| 263 | $result = CronTaskResult::get()->filter([ |
||
| 264 | 'TaskClass' => $this->TaskClass |
||
| 265 | ])->Sort('Created DESC')->first(); |
||
| 266 | return $result; |
||
| 267 | } |
||
| 268 | |||
| 269 | public function LastFailedResult(): ?CronTaskResult |
||
| 277 | } |
||
| 278 | } |
||
| 279 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths