Conditions | 9 |
Paths | 7 |
Total Lines | 55 |
Code Lines | 33 |
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 |
||
30 | public function run($request) |
||
31 | { |
||
32 | $subsiteFromId = $request->getVar('from'); |
||
33 | if (!is_numeric($subsiteFromId)) { |
||
34 | throw new InvalidArgumentException('Missing "from" parameter'); |
||
35 | } |
||
36 | $subsiteFrom = DataObject::get_by_id(Subsite::class, $subsiteFromId); |
||
37 | if (!$subsiteFrom) { |
||
38 | throw new InvalidArgumentException('Subsite not found'); |
||
39 | } |
||
40 | |||
41 | $subsiteToId = $request->getVar('to'); |
||
42 | if (!is_numeric($subsiteToId)) { |
||
43 | throw new InvalidArgumentException('Missing "to" parameter'); |
||
44 | } |
||
45 | $subsiteTo = DataObject::get_by_id(Subsite::class, $subsiteToId); |
||
46 | if (!$subsiteTo) { |
||
47 | throw new InvalidArgumentException('Subsite not found'); |
||
48 | } |
||
49 | |||
50 | $useVirtualPages = (bool)$request->getVar('virtual'); |
||
51 | |||
52 | Subsite::changeSubsite($subsiteFrom); |
||
53 | |||
54 | // Copy data from this template to the given subsite. Does this using an iterative depth-first search. |
||
55 | // This will make sure that the new parents on the new subsite are correct, and there are no funny |
||
56 | // issues with having to check whether or not the new parents have been added to the site tree |
||
57 | // when a page, etc, is duplicated |
||
58 | $stack = [[0, 0]]; |
||
59 | while (count($stack) > 0) { |
||
60 | list($sourceParentID, $destParentID) = array_pop($stack); |
||
61 | |||
62 | $children = Versioned::get_by_stage(SiteTree::class, 'Live', "\"ParentID\" = $sourceParentID", ''); |
||
63 | |||
64 | if ($children) { |
||
65 | foreach ($children as $child) { |
||
66 | if ($useVirtualPages) { |
||
67 | $childClone = new SubsitesVirtualPage(); |
||
68 | $childClone->writeToStage('Stage'); |
||
69 | $childClone->CopyContentFromID = $child->ID; |
||
70 | $childClone->SubsiteID = $subsiteTo->ID; |
||
71 | } else { |
||
72 | $childClone = $child->duplicateToSubsite($subsiteTo->ID, true); |
||
73 | } |
||
74 | |||
75 | $childClone->ParentID = $destParentID; |
||
76 | $childClone->writeToStage('Stage'); |
||
77 | $childClone->copyVersionToStage('Stage', 'Live'); |
||
78 | array_push($stack, [$child->ID, $childClone->ID]); |
||
79 | |||
80 | $this->log(sprintf('Copied "%s" (#%d, %s)', $child->Title, $child->ID, $child->Link())); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | unset($children); |
||
85 | } |
||
93 |