| Conditions | 12 |
| Paths | 11 |
| Total Lines | 57 |
| Code Lines | 34 |
| 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 |
||
| 105 | public function saveInto(DataObjectInterface $record) |
||
| 106 | { |
||
| 107 | parent::saveInto($record); |
||
| 108 | |||
| 109 | // Check required relation details are available |
||
| 110 | $fieldname = $this->getName(); |
||
| 111 | if (!$fieldname || !is_array($this->rawSubmittal)) { |
||
| 112 | return $this; |
||
| 113 | } |
||
| 114 | |||
| 115 | // Check type of relation |
||
| 116 | $relation = $record->hasMethod($fieldname) ? $record->$fieldname() : null; |
||
| 117 | if ($relation) { |
||
| 118 | $idList = $this->getItemIDs(); |
||
| 119 | $rawList = $this->rawSubmittal; |
||
| 120 | $sortColumn = $this->getSortColumn(); |
||
| 121 | |||
| 122 | if ($relation instanceof ManyManyList) { |
||
| 123 | try { |
||
| 124 | DB::get_conn()->withTransaction(function () use ($relation, $idList, $rawList, $record, $sortColumn) { |
||
| 125 | $relation->getForeignID(); |
||
| 126 | $ownerIdField = $relation->getForeignKey(); |
||
| 127 | $fileIdField = $relation->getLocalKey(); |
||
| 128 | $joinTable = '"'. $relation->getJoinTable() .'"'; |
||
| 129 | |||
| 130 | $sort = 0; |
||
| 131 | foreach ($rawList as $id) { |
||
| 132 | if (in_array($id, $idList)) { |
||
| 133 | // Use SQLUpdate to update the data in the join-table. |
||
| 134 | // This is safe to do, since new records have already been written to the DB in the |
||
| 135 | // parent::saveInto call. |
||
| 136 | SQLUpdate::create($joinTable) |
||
| 137 | ->setWhere([ |
||
| 138 | "\"$ownerIdField\" = ?" => $record->ID, |
||
| 139 | "\"$fileIdField\" = ?" => $id |
||
| 140 | ]) |
||
| 141 | ->assign($sortColumn, $sort++) |
||
| 142 | ->execute(); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | }); |
||
| 146 | } catch (\Exception $ex) { |
||
| 147 | $this->logger->warning('Unable to sort files in sortable relation.', ['exception' => $ex]); |
||
| 148 | } |
||
| 149 | } elseif ($relation instanceof UnsavedRelationList) { |
||
| 150 | // With an unsaved relation list the items can just be removed and re-added |
||
| 151 | $sort = 0; |
||
| 152 | $relation->removeAll(); |
||
| 153 | foreach ($rawList as $id) { |
||
| 154 | if (in_array($id, $idList)) { |
||
| 155 | $relation->add($id, [$sortColumn => $sort++]); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | return $this; |
||
| 162 | } |
||
| 173 |