Complex classes like WorkflowApplicable 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 WorkflowApplicable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class WorkflowApplicable extends DataExtension { |
||
|
|
|||
| 12 | |||
| 13 | private static $has_one = array( |
||
| 14 | 'WorkflowDefinition' => 'WorkflowDefinition', |
||
| 15 | ); |
||
| 16 | |||
| 17 | private static $many_many = array( |
||
| 18 | 'AdditionalWorkflowDefinitions' => 'WorkflowDefinition' |
||
| 19 | ); |
||
| 20 | |||
| 21 | private static $dependencies = array( |
||
| 22 | 'workflowService' => '%$WorkflowService', |
||
| 23 | ); |
||
| 24 | |||
| 25 | /** |
||
| 26 | * |
||
| 27 | * Used to flag to this extension if there's a WorkflowPublishTargetJob running. |
||
| 28 | * @var boolean |
||
| 29 | */ |
||
| 30 | public $isPublishJobRunning = false; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * |
||
| 34 | * @param boolean $truth |
||
| 35 | */ |
||
| 36 | public function setIsPublishJobRunning($truth) { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * |
||
| 42 | * @return boolean |
||
| 43 | */ |
||
| 44 | public function getIsPublishJobRunning() { |
||
| 47 | |||
| 48 | /** |
||
| 49 | * |
||
| 50 | * @see {@link $this->isPublishJobRunning} |
||
| 51 | * @return boolean |
||
| 52 | */ |
||
| 53 | public function isPublishJobRunning() { |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var WorkflowService |
||
| 60 | */ |
||
| 61 | public $workflowService; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * |
||
| 65 | * A cache var for the current workflow instance |
||
| 66 | * |
||
| 67 | * @var WorkflowInstance |
||
| 68 | */ |
||
| 69 | protected $currentInstance; |
||
| 70 | |||
| 71 | public function updateSettingsFields(FieldList $fields) { |
||
| 74 | |||
| 75 | public function updateCMSFields(FieldList $fields) { |
||
| 76 | if(!$this->owner->hasMethod('getSettingsFields')) $this->updateFields($fields); |
||
| 77 | |||
| 78 | // Instantiate a hidden form field to pass the triggered workflow definition through, allowing a dynamic form action. |
||
| 79 | |||
| 80 | $fields->push(HiddenField::create( |
||
| 81 | 'TriggeredWorkflowID' |
||
| 82 | )); |
||
| 83 | } |
||
| 84 | |||
| 85 | public function updateFields(FieldList $fields) { |
||
| 86 | if (!$this->owner->ID) { |
||
| 87 | return $fields; |
||
| 88 | } |
||
| 89 | |||
| 90 | $tab = $fields->fieldByName('Root') ? $fields->findOrMakeTab('Root.Workflow') : $fields; |
||
| 91 | |||
| 92 | if(Permission::check('APPLY_WORKFLOW')) { |
||
| 93 | $definition = new DropdownField('WorkflowDefinitionID', _t('WorkflowApplicable.DEFINITION', 'Applied Workflow')); |
||
| 94 | $definitions = $this->workflowService->getDefinitions()->map()->toArray(); |
||
| 95 | $definition->setSource($definitions); |
||
| 96 | $definition->setEmptyString(_t('WorkflowApplicable.INHERIT', 'Inherit from parent')); |
||
| 97 | $tab->push($definition); |
||
| 98 | |||
| 99 | // Allow an optional selection of additional workflow definitions. |
||
| 100 | |||
| 101 | if($this->owner->WorkflowDefinitionID) { |
||
| 102 | $fields->removeByName('AdditionalWorkflowDefinitions'); |
||
| 103 | unset($definitions[$this->owner->WorkflowDefinitionID]); |
||
| 104 | $tab->push($additional = ListboxField::create( |
||
| 105 | 'AdditionalWorkflowDefinitions', |
||
| 106 | _t('WorkflowApplicable.ADDITIONAL_WORKFLOW_DEFINITIONS', 'Additional Workflows') |
||
| 107 | )); |
||
| 108 | $additional->setSource($definitions); |
||
| 109 | $additional->setMultiple(true); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | // Display the effective workflow definition. |
||
| 114 | |||
| 115 | if($effective = $this->getWorkflowInstance()) { |
||
| 116 | $title = $effective->Definition()->Title; |
||
| 117 | $tab->push(ReadonlyField::create( |
||
| 118 | 'EffectiveWorkflow', |
||
| 119 | _t('WorkflowApplicable.EFFECTIVE_WORKFLOW', 'Effective Workflow'), |
||
| 120 | $title |
||
| 121 | )); |
||
| 122 | } |
||
| 123 | |||
| 124 | if($this->owner->ID) { |
||
| 125 | $config = new GridFieldConfig_Base(); |
||
| 126 | $config->addComponent(new GridFieldEditButton()); |
||
| 127 | $config->addComponent(new GridFieldDetailForm()); |
||
| 128 | |||
| 129 | $insts = $this->owner->WorkflowInstances(); |
||
| 130 | $log = new GridField('WorkflowLog', _t('WorkflowApplicable.WORKFLOWLOG', 'Workflow Log'), $insts, $config); |
||
| 131 | |||
| 132 | $tab->push($log); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | public function updateCMSActions(FieldList $actions) { |
||
| 137 | $active = $this->workflowService->getWorkflowFor($this->owner); |
||
| 138 | $c = Controller::curr(); |
||
| 139 | if ($c && $c->hasExtension('AdvancedWorkflowExtension')) { |
||
| 140 | if ($active) { |
||
| 141 | if ($this->canEditWorkflow()) { |
||
| 142 | $workflowOptions = new Tab( |
||
| 143 | 'WorkflowOptions', |
||
| 144 | _t('SiteTree.WorkflowOptions', 'Workflow options', 'Expands a view for workflow specific buttons') |
||
| 145 | ); |
||
| 146 | |||
| 147 | $menu = $actions->fieldByName('ActionMenus'); |
||
| 148 | if (!$menu) { |
||
| 149 | // create the menu for adding to any arbitrary non-sitetree object |
||
| 150 | $menu = $this->createActionMenu(); |
||
| 151 | $actions->push($menu); |
||
| 152 | } |
||
| 153 | |||
| 154 | $menu->push($workflowOptions); |
||
| 155 | |||
| 156 | $transitions = $active->CurrentAction()->getValidTransitions(); |
||
| 157 | |||
| 158 | foreach ($transitions as $transition) { |
||
| 159 | if ($transition->canExecute($active)) { |
||
| 160 | $action = FormAction::create('updateworkflow-' . $transition->ID, $transition->Title) |
||
| 161 | ->setAttribute('data-transitionid', $transition->ID); |
||
| 162 | $workflowOptions->push($action); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | // $action = FormAction::create('updateworkflow', $active->CurrentAction() ? $active->CurrentAction()->Title : _t('WorkflowApplicable.UPDATE_WORKFLOW', 'Update Workflow')) |
||
| 167 | // ->setAttribute('data-icon', 'navigation'); |
||
| 168 | // $actions->fieldByName('MajorActions') ? $actions->fieldByName('MajorActions')->push($action) : $actions->push($action); |
||
| 169 | } |
||
| 170 | } else { |
||
| 171 | // Instantiate the workflow definition initial actions. |
||
| 172 | $definitions = $this->workflowService->getDefinitionsFor($this->owner); |
||
| 173 | if($definitions) { |
||
| 174 | $menu = $actions->fieldByName('ActionMenus'); |
||
| 175 | if(is_null($menu)) { |
||
| 176 | |||
| 177 | // Instantiate a new action menu for any data objects. |
||
| 178 | |||
| 179 | $menu = $this->createActionMenu(); |
||
| 180 | $actions->push($menu); |
||
| 181 | } |
||
| 182 | $tab = Tab::create( |
||
| 183 | 'AdditionalWorkflows' |
||
| 184 | ); |
||
| 185 | $menu->insertBefore($tab, 'MoreOptions'); |
||
| 186 | $addedFirst = false; |
||
| 187 | foreach($definitions as $definition) { |
||
| 188 | if($definition->getInitialAction()) { |
||
| 189 | $action = FormAction::create( |
||
| 190 | "startworkflow-{$definition->ID}", |
||
| 191 | $definition->InitialActionButtonText ? $definition->InitialActionButtonText : $definition->getInitialAction()->Title |
||
| 192 | )->addExtraClass('start-workflow')->setAttribute('data-workflow', $definition->ID); |
||
| 193 | |||
| 194 | // The first element is the main workflow definition, and will be displayed as a major action. |
||
| 195 | |||
| 196 | if(!$addedFirst) { |
||
| 197 | $addedFirst = true; |
||
| 198 | $action->setAttribute('data-icon', 'navigation'); |
||
| 199 | $majorActions = $actions->fieldByName('MajorActions'); |
||
| 200 | $majorActions ? $majorActions->push($action) : $actions->push($action); |
||
| 201 | } else { |
||
| 202 | $tab->push($action); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | } |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | protected function createActionMenu() { |
||
| 213 | $rootTabSet = new TabSet('ActionMenus'); |
||
| 214 | $rootTabSet->addExtraClass('ss-ui-action-tabset action-menus'); |
||
| 215 | return $rootTabSet; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Included in CMS-generated email templates for a NotifyUsersWorkflowAction. |
||
| 220 | * Returns an absolute link to the CMS UI for a Page object |
||
| 221 | * |
||
| 222 | * @return string | null |
||
| 223 | */ |
||
| 224 | public function AbsoluteEditLink() { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Included in CMS-generated email templates for a NotifyUsersWorkflowAction. |
||
| 242 | * Allows users to select a link in an email for direct access to the transition-selection dropdown in the CMS UI. |
||
| 243 | * |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | public function LinkToPendingItems() { |
||
| 247 | $urlBase = Director::absoluteBaseURL(); |
||
| 248 | $urlFrag = 'admin/workflows/WorkflowDefinition/EditForm/field'; |
||
| 249 | $urlInst = $this->getWorkflowInstance(); |
||
| 250 | return Controller::join_links($urlBase, $urlFrag, 'PendingObjects', 'item', $urlInst->ID, 'edit'); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * After a workflow item is written, we notify the |
||
| 255 | * workflow so that it can take action if needbe |
||
| 256 | */ |
||
| 257 | public function onAfterWrite() { |
||
| 258 | $instance = $this->getWorkflowInstance(); |
||
| 259 | if ($instance && $instance->CurrentActionID) { |
||
| 260 | $action = $instance->CurrentAction()->BaseAction()->targetUpdated($instance); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | public function WorkflowInstances() { |
||
| 265 | return WorkflowInstance::get()->filter(array( |
||
| 266 | 'TargetClass' => $this->ownerBaseClass, |
||
| 267 | 'TargetID' => $this->owner->ID |
||
| 268 | )); |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Gets the current instance of workflow |
||
| 273 | * |
||
| 274 | * @return WorkflowInstance |
||
| 275 | */ |
||
| 276 | public function getWorkflowInstance() { |
||
| 277 | if (!$this->currentInstance) { |
||
| 278 | $this->currentInstance = $this->workflowService->getWorkflowFor($this->owner); |
||
| 279 | } |
||
| 280 | |||
| 281 | return $this->currentInstance; |
||
| 282 | } |
||
| 283 | |||
| 284 | |||
| 285 | /** |
||
| 286 | * Gets the history of a workflow instance |
||
| 287 | * |
||
| 288 | * @return DataObjectSet |
||
| 289 | */ |
||
| 290 | public function getWorkflowHistory($limit = null) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Check all recent WorkflowActionIntances and return the most recent one with a Comment |
||
| 296 | * |
||
| 297 | * @return WorkflowActionInstance |
||
| 298 | */ |
||
| 299 | public function RecentWorkflowComment($limit = 10){ |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Content can never be directly publishable if there's a workflow applied. |
||
| 311 | * |
||
| 312 | * If there's an active instance, then it 'might' be publishable |
||
| 313 | */ |
||
| 314 | public function canPublish() { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Can only edit content that's NOT in another person's content changeset |
||
| 333 | */ |
||
| 334 | public function canEdit($member) { |
||
| 335 | // Override any default behaviour, to allow queuedjobs to complete |
||
| 336 | if($this->isPublishJobRunning()) { |
||
| 337 | return true; |
||
| 338 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Can a user edit the current workflow attached to this item? |
||
| 347 | */ |
||
| 348 | public function canEditWorkflow() { |
||
| 355 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.