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