Conditions | 12 |
Paths | 62 |
Total Lines | 69 |
Code Lines | 49 |
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 |
||
83 | public function handleElementUriChange(Element $element): void |
||
84 | { |
||
85 | if (array_key_exists($element->id, $this->elementsWithCreatedRedirects)) { |
||
86 | return; |
||
87 | } |
||
88 | $this->elementsWithCreatedRedirects[] = $element->id; |
||
89 | |||
90 | $uris = $this->getAllElementUris($element); |
||
91 | if (!empty($this->oldElementUris[$element->id])) { |
||
92 | $oldElementUris = $this->oldElementUris[$element->id]; |
||
93 | foreach ($uris as $siteId => $newUri) { |
||
94 | if (!empty($oldElementUris[$siteId])) { |
||
95 | $oldUri = $oldElementUris[$siteId]; |
||
96 | Craft::debug( |
||
97 | Craft::t( |
||
98 | 'retour', |
||
99 | 'Comparing old: {oldUri} to new: {newUri}', |
||
100 | ['oldUri' => print_r($oldUri, true), 'newUri' => print_r($newUri, true)] |
||
101 | ), |
||
102 | __METHOD__ |
||
103 | ); |
||
104 | // Handle the siteId |
||
105 | $redirectSiteId = null; |
||
106 | if (Craft::$app->getIsMultiSite()) { |
||
107 | $redirectSiteId = $siteId; |
||
108 | } |
||
109 | // Make sure the URIs are not the same |
||
110 | if (strcmp($oldUri, $newUri) !== 0) { |
||
111 | // Handle trailing slash config setting |
||
112 | if (Craft::$app->config->general->addTrailingSlashesToUrls) { |
||
113 | $oldUri = rtrim($oldUri, '/') . '/'; |
||
114 | $newUri = rtrim($newUri, '/') . '/'; |
||
115 | } |
||
116 | // Handle the URL match type |
||
117 | if (Retour::$settings->uriChangeRedirectSrcMatch === 'fullurl') { |
||
118 | try { |
||
119 | if ($redirectSiteId !== null) { |
||
120 | $redirectSiteId = (int)$redirectSiteId; |
||
121 | } |
||
122 | $oldUri = \craft\helpers\UrlHelper::siteUrl($oldUri, null, null, $redirectSiteId); |
||
123 | $newUri = UrlHelper::siteUrl($newUri, null, null, $redirectSiteId); |
||
124 | } catch (Exception $e) { |
||
125 | // That's fine |
||
126 | } |
||
127 | } |
||
128 | $redirectConfig = [ |
||
129 | 'id' => 0, |
||
130 | 'redirectMatchType' => 'exactmatch', |
||
131 | 'redirectHttpCode' => 301, |
||
132 | 'redirectSrcMatch' => Retour::$settings->uriChangeRedirectSrcMatch, |
||
133 | 'redirectSrcUrl' => $oldUri, |
||
134 | 'redirectDestUrl' => $newUri, |
||
135 | 'siteId' => $redirectSiteId, |
||
136 | ]; |
||
137 | // Trigger a 'beforeSaveEntryRedirect' event |
||
138 | $isNew = (int)$redirectConfig['id'] === 0; |
||
139 | $event = new RedirectEvent([ |
||
140 | 'isNew' => $isNew, |
||
141 | 'legacyUrl' => $redirectConfig['redirectSrcUrl'], |
||
142 | 'destinationUrl' => $redirectConfig['redirectDestUrl'], |
||
143 | 'matchType' => $redirectConfig['redirectSrcMatch'], |
||
144 | 'redirectType' => $redirectConfig['redirectHttpCode'], |
||
145 | 'siteId' => $redirectConfig['siteId'], |
||
146 | ]); |
||
147 | $this->trigger(self::EVENT_BEFORE_SAVE_ENTRY_REDIRECT, $event); |
||
148 | if (!$event->isValid) { |
||
149 | return; |
||
150 | } |
||
151 | Retour::$plugin->redirects->saveRedirect($redirectConfig); |
||
152 | } |
||
190 |