| Conditions | 13 |
| Paths | 183 |
| Total Lines | 90 |
| 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 |
||
| 136 | public function endTemplateCache(string $key, $flags = null, bool $global, string $duration = null, $expiration, string $body) |
||
| 137 | { |
||
| 138 | // Make sure template caching is enabled |
||
| 139 | if ($this->_isTemplateCachingEnabled() === false) { |
||
| 140 | return; |
||
| 141 | } |
||
| 142 | |||
| 143 | // If there are any transform generation URLs in the body, don't cache it. |
||
| 144 | // stripslashes($body) in case the URL has been JS-encoded or something. |
||
| 145 | if (StringHelper::contains(stripslashes($body), 'assets/generate-transform')) { |
||
| 146 | return; |
||
| 147 | } |
||
| 148 | |||
| 149 | if (!$global && (strlen($path = $this->_getPath()) > 255)) { |
||
| 150 | Craft::warning('Skipped adding ' . $key . ' to template cache table because the path is > 255 characters: ' . $path, __METHOD__); |
||
| 151 | |||
| 152 | return; |
||
| 153 | } |
||
| 154 | |||
| 155 | if (Craft::$app->getDb()->getIsMysql()) { |
||
| 156 | // Encode any 4-byte UTF-8 characters |
||
| 157 | $body = StringHelper::encodeMb4($body); |
||
| 158 | } |
||
| 159 | |||
| 160 | // Figure out the expiration date |
||
| 161 | if ($duration !== null) { |
||
| 162 | $expiration = new DateTime($duration); |
||
| 163 | } |
||
| 164 | |||
| 165 | if (!$expiration) { |
||
| 166 | $cacheDuration = Craft::$app->getConfig()->getGeneral()->cacheDuration; |
||
| 167 | |||
| 168 | if ($cacheDuration <= 0) { |
||
| 169 | $cacheDuration = 31536000; // 1 year |
||
| 170 | } |
||
| 171 | |||
| 172 | $cacheDuration += time(); |
||
| 173 | |||
| 174 | $expiration = new DateTime('@' . $cacheDuration); |
||
| 175 | } |
||
| 176 | |||
| 177 | // Save it |
||
| 178 | $transaction = Craft::$app->getDb()->beginTransaction(); |
||
| 179 | |||
| 180 | try { |
||
| 181 | Craft::$app->getDb()->createCommand() |
||
| 182 | ->insert( |
||
| 183 | self::$_templateCachesTable, |
||
| 184 | [ |
||
| 185 | 'cacheKey' => $key, |
||
| 186 | 'siteId' => Craft::$app->getSites()->getCurrentSite()->id, |
||
| 187 | 'path' => $global ? null : $this->_getPath(), |
||
| 188 | 'expiryDate' => Db::prepareDateForDb($expiration), |
||
| 189 | 'body' => $body |
||
| 190 | ], |
||
| 191 | false) |
||
| 192 | ->execute(); |
||
| 193 | |||
| 194 | if ($flags) { |
||
| 195 | |||
| 196 | $cacheId = Craft::$app->getDb()->getLastInsertID(self::$_templateCachesTable); |
||
| 197 | |||
| 198 | // Sanitize flags |
||
| 199 | if (\is_array($flags)) { |
||
| 200 | $flags = \implode(',', \array_map(function ($flag) { |
||
| 201 | return \preg_replace('/\s+/', '', $flag); |
||
| 202 | }, $flags)); |
||
| 203 | } else { |
||
| 204 | $flags = \preg_replace('/\s+/', '', $flags); |
||
| 205 | } |
||
| 206 | |||
| 207 | $flags = \implode(',', \explode('|', $flags)); |
||
| 208 | |||
| 209 | Craft::$app->getDb()->createCommand() |
||
| 210 | ->insert( |
||
| 211 | Flagged::tableName(), |
||
| 212 | [ |
||
| 213 | 'cacheId' => $cacheId, |
||
| 214 | 'flags' => $flags |
||
| 215 | ], |
||
| 216 | false |
||
| 217 | ) |
||
| 218 | ->execute(); |
||
| 219 | } |
||
| 220 | |||
| 221 | $transaction->commit(); |
||
| 222 | } catch (\Throwable $e) { |
||
| 223 | $transaction->rollBack(); |
||
| 224 | |||
| 225 | throw $e; |
||
| 226 | } |
||
| 297 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.