| Conditions | 6 |
| Paths | 2 |
| Total Lines | 81 |
| 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 |
||
| 93 | public function getEditForm($id = null, $fields = null) |
||
| 94 | { |
||
| 95 | $form = parent::getEditForm($id, $fields); |
||
| 96 | |||
| 97 | $filter = $this->jobQueue->getJobListFilter(null, self::config()->max_finished_jobs_age); |
||
| 98 | |||
| 99 | $list = QueuedJobDescriptor::get()->where($filter)->sort('Created', 'DESC'); |
||
| 100 | |||
| 101 | $gridFieldConfig = GridFieldConfig_RecordEditor::create() |
||
| 102 | ->addComponent(new GridFieldQueuedJobExecute('execute')) |
||
| 103 | ->addComponent(new GridFieldQueuedJobExecute('pause', function ($record) { |
||
| 104 | return $record->JobStatus == QueuedJob::STATUS_WAIT || $record->JobStatus == QueuedJob::STATUS_RUN; |
||
| 105 | })) |
||
| 106 | ->addComponent(new GridFieldQueuedJobExecute('resume', function ($record) { |
||
| 107 | return $record->JobStatus == QueuedJob::STATUS_PAUSED || $record->JobStatus == QueuedJob::STATUS_BROKEN; |
||
| 108 | })) |
||
| 109 | ->removeComponentsByType([ |
||
| 110 | GridFieldAddNewButton::class, |
||
| 111 | GridFieldPageCount::class, |
||
| 112 | GridFieldToolbarHeader::class, |
||
| 113 | ]); |
||
| 114 | |||
| 115 | // Set messages to HTML display format |
||
| 116 | $formatting = array( |
||
| 117 | 'Messages' => function ($val, $obj) { |
||
| 118 | return "<div style='max-width: 300px; max-height: 200px; overflow: auto;'>$obj->Messages</div>"; |
||
| 119 | }, |
||
| 120 | ); |
||
| 121 | $gridFieldConfig->getComponentByType(GridFieldDataColumns::class) |
||
| 122 | ->setFieldFormatting($formatting); |
||
| 123 | |||
| 124 | // Replace gridfield |
||
| 125 | /** @skipUpgrade */ |
||
| 126 | $grid = GridField::create( |
||
| 127 | 'QueuedJobDescriptor', |
||
| 128 | '', |
||
| 129 | $list, |
||
| 130 | $gridFieldConfig |
||
| 131 | ); |
||
| 132 | $grid->setForm($form); |
||
| 133 | /** @skipUpgrade */ |
||
| 134 | $form->Fields()->replaceField($this->sanitiseClassName(QueuedJobDescriptor::class), $grid); |
||
| 135 | |||
| 136 | if (Permission::check('ADMIN')) { |
||
| 137 | $types = ClassInfo::subclassesFor(AbstractQueuedJob::class); |
||
| 138 | $types = array_combine($types, $types); |
||
| 139 | foreach ($types as $class) { |
||
| 140 | $reflection = new ReflectionClass($class); |
||
| 141 | if (!$reflection->isInstantiable()) { |
||
| 142 | unset($types[$class]); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | $jobType = DropdownField::create( |
||
| 146 | 'JobType', |
||
| 147 | _t(__CLASS__ . '.CREATE_JOB_TYPE', 'Create job of type'), |
||
| 148 | $types |
||
| 149 | ); |
||
| 150 | $jobType->setEmptyString('(select job to create)'); |
||
| 151 | $form->Fields()->push($jobType); |
||
| 152 | |||
| 153 | $jobParams = TextareaField::create( |
||
| 154 | 'JobParams', |
||
| 155 | _t(__CLASS__ . '.JOB_TYPE_PARAMS', 'Constructor parameters for job creation (one per line)') |
||
| 156 | ); |
||
| 157 | $form->Fields()->push($jobParams); |
||
| 158 | |||
| 159 | $form->Fields()->push( |
||
| 160 | $dt = DatetimeField::create('JobStart', _t(__CLASS__ . '.START_JOB_TIME', 'Start job at')) |
||
| 161 | ); |
||
| 162 | |||
| 163 | $actions = $form->Actions(); |
||
| 164 | $actions->push( |
||
| 165 | FormAction::create('createjob', _t(__CLASS__ . '.CREATE_NEW_JOB', 'Create new job')) |
||
| 166 | ->addExtraClass('btn btn-primary') |
||
| 167 | ); |
||
| 168 | } |
||
| 169 | |||
| 170 | $this->extend('updateEditForm', $form); |
||
| 171 | |||
| 172 | return $form; |
||
| 173 | } |
||
| 174 | |||
| 217 |