Conditions | 29 |
Paths | 66 |
Total Lines | 83 |
Code Lines | 48 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
86 | public function revertEntityToTimestamp(AbstractDBElement $element, DateTime $timestamp, array $reverted_elements = []): void |
||
87 | { |
||
88 | if (!$element instanceof TimeStampableInterface) { |
||
89 | throw new InvalidArgumentException('$element must have a Timestamp!'); |
||
90 | } |
||
91 | |||
92 | if ($timestamp > new DateTime('now')) { |
||
93 | throw new InvalidArgumentException('You can not travel to the future (yet)...'); |
||
94 | } |
||
95 | |||
96 | //Skip this process if already were reverted... |
||
97 | if (in_array($element, $reverted_elements, true)) { |
||
98 | return; |
||
99 | } |
||
100 | $reverted_elements[] = $element; |
||
101 | |||
102 | $history = $this->repo->getTimetravelDataForElement($element, $timestamp); |
||
103 | |||
104 | /* |
||
105 | if (!$this->repo->getElementExistedAtTimestamp($element, $timestamp)) { |
||
106 | $element = null; |
||
107 | return; |
||
108 | }*/ |
||
109 | |||
110 | foreach ($history as $logEntry) { |
||
111 | if ($logEntry instanceof ElementEditedLogEntry) { |
||
112 | $this->applyEntry($element, $logEntry); |
||
113 | } |
||
114 | if ($logEntry instanceof CollectionElementDeleted) { |
||
115 | //Undelete element and add it to collection again |
||
116 | $undeleted = $this->undeleteEntity( |
||
117 | $logEntry->getDeletedElementClass(), |
||
118 | $logEntry->getDeletedElementID() |
||
119 | ); |
||
120 | if ($this->repo->getElementExistedAtTimestamp($undeleted, $timestamp)) { |
||
121 | $this->revertEntityToTimestamp($undeleted, $timestamp, $reverted_elements); |
||
122 | $collection = $this->getField($element, $logEntry->getCollectionName()); |
||
123 | if ($collection instanceof Collection) { |
||
124 | $collection->add($undeleted); |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | |||
130 | // Revert any of the associated elements |
||
131 | $metadata = $this->em->getClassMetadata(get_class($element)); |
||
132 | $associations = $metadata->getAssociationMappings(); |
||
133 | foreach ($associations as $field => $mapping) { |
||
134 | if ( |
||
135 | ($element instanceof AbstractStructuralDBElement && ('parts' === $field || 'children' === $field)) |
||
|
|||
136 | || ($element instanceof AttachmentType && 'attachments' === $field) |
||
137 | ) { |
||
138 | continue; |
||
139 | } |
||
140 | |||
141 | //Revert many to one association (one element in property) |
||
142 | if ( |
||
143 | ClassMetadataInfo::MANY_TO_ONE === $mapping['type'] |
||
144 | || ClassMetadataInfo::ONE_TO_ONE === $mapping['type'] |
||
145 | ) { |
||
146 | $target_element = $this->getField($element, $field); |
||
147 | if (null !== $target_element && $element->getLastModified() > $timestamp) { |
||
148 | $this->revertEntityToTimestamp($target_element, $timestamp, $reverted_elements); |
||
149 | } |
||
150 | } elseif ( //Revert *_TO_MANY associations (collection properties) |
||
151 | (ClassMetadataInfo::MANY_TO_MANY === $mapping['type'] |
||
152 | || ClassMetadataInfo::ONE_TO_MANY === $mapping['type']) |
||
153 | && false === $mapping['isOwningSide'] |
||
154 | ) { |
||
155 | $target_elements = $this->getField($element, $field); |
||
156 | if (null === $target_elements || count($target_elements) > 10) { |
||
157 | continue; |
||
158 | } |
||
159 | foreach ($target_elements as $target_element) { |
||
160 | if (null !== $target_element && $element->getLastModified() >= $timestamp) { |
||
161 | //Remove the element from collection, if it did not existed at $timestamp |
||
162 | if (!$this->repo->getElementExistedAtTimestamp( |
||
163 | $target_element, |
||
164 | $timestamp |
||
165 | ) && $target_elements instanceof Collection) { |
||
166 | $target_elements->removeElement($target_element); |
||
167 | } |
||
168 | $this->revertEntityToTimestamp($target_element, $timestamp, $reverted_elements); |
||
169 | } |
||
256 |