| Conditions | 8 |
| Paths | 36 |
| Total Lines | 59 |
| Code Lines | 39 |
| 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 |
||
| 134 | public function adminSavePage(Request $request, Response $response): Response |
||
| 135 | { |
||
| 136 | $data = $request->getParsedBody(); |
||
| 137 | $pagesDir = $this->app->get('pagesdir'); |
||
| 138 | |||
| 139 | if (!empty($data['title'])) { |
||
| 140 | $pageTitle = $data['title']; |
||
| 141 | } else { |
||
| 142 | $pageTitle = "No Title"; |
||
| 143 | } |
||
| 144 | |||
| 145 | if (!empty($data['mde'])) { |
||
| 146 | $pageContent = $data['mde']; |
||
| 147 | } else { |
||
| 148 | $pageContent = 'No content in this page'; |
||
| 149 | } |
||
| 150 | |||
| 151 | |||
| 152 | // Some functions to slugify title to create very cool URL |
||
| 153 | $acc = 'àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ'; |
||
| 154 | $noAcc = 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY'; |
||
| 155 | $title = mb_convert_encoding($pageTitle, 'UTF-8', mb_list_encodings()); |
||
| 156 | $acc = mb_convert_encoding($acc, 'UTF-8', mb_list_encodings()); |
||
| 157 | $slug = mb_strtolower(strtr($title, $acc, $noAcc)); |
||
| 158 | $slug = preg_replace('~[^\pL\d]+~u', '-', $slug); |
||
| 159 | $slug = preg_replace('~[^-\w]+~', '', $slug); |
||
| 160 | $slug = strtolower($slug); |
||
| 161 | $slug = preg_replace('~-+~', '-', $slug); |
||
| 162 | |||
| 163 | // apply Twig to the page to display with selected theme |
||
| 164 | $page = '{% extends settings.theme ~ "/page.html" %}'; |
||
| 165 | $page .= "\n{% block title %}" . $title . "{% endblock %}\n"; |
||
| 166 | $page .= "\n{% block page %}\n" . $pageContent . "\n{% endblock %}\n"; |
||
| 167 | |||
| 168 | $file = $pagesDir . $slug . '.html'; |
||
| 169 | |||
| 170 | if (file_put_contents($file, $page)) { |
||
| 171 | if (isset($_SERVER['HTTPS'])) { |
||
| 172 | $isSecure = $_SERVER['HTTPS']; |
||
| 173 | } |
||
| 174 | if (isset($isSecure) && $isSecure === 'on') { |
||
| 175 | $scheme = 'https'; |
||
| 176 | } else { |
||
| 177 | $scheme = 'http'; |
||
| 178 | } |
||
| 179 | |||
| 180 | if (isset($_SERVER['HTTP_HOST'])) { |
||
| 181 | $host = $_SERVER['HTTP_HOST']; |
||
| 182 | } else { |
||
| 183 | $host = "unknown_host.com"; |
||
| 184 | } |
||
| 185 | |||
| 186 | |||
| 187 | $pageUrl = $scheme . '://' . $host . '/pages/' . $slug; |
||
| 188 | $response->getBody()->write($pageUrl); |
||
| 189 | } else { |
||
| 190 | $response->getBody()->write('Error'); |
||
| 191 | } |
||
| 192 | return $response; |
||
| 193 | } |
||
| 195 |