| Conditions | 8 |
| Paths | 6 |
| Total Lines | 73 |
| Code Lines | 50 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 60 | public function post(string $permlink, Response $response): Response |
||
| 61 | { |
||
| 62 | $settings = $this->app->get('settings'); |
||
| 63 | |||
| 64 | $apiConfig = [ |
||
| 65 | 'hiveNode' => $settings['api'], |
||
| 66 | 'debug' => false, |
||
| 67 | ]; |
||
| 68 | |||
| 69 | if (!empty($permlink)) { |
||
| 70 | $embedLibrary = new Embed(); |
||
| 71 | $embedLibrary->setSettings([ |
||
| 72 | 'oembed:query_parameters' => [ |
||
| 73 | 'maxwidth' => 800, |
||
| 74 | 'maxheight' => 600, |
||
| 75 | ] |
||
| 76 | ]); |
||
| 77 | |||
| 78 | $markdownConfig = [ |
||
| 79 | 'embed' => [ |
||
| 80 | 'adapter' => new OscaroteroEmbedAdapter($embedLibrary), |
||
| 81 | 'fallback' => 'link', |
||
| 82 | ], |
||
| 83 | 'heading_permalink' => [ |
||
| 84 | 'html_class' => 'heading-permalink', |
||
| 85 | 'insert' => 'before', |
||
| 86 | 'symbol' => '# ', |
||
| 87 | 'title' => "Permalink", |
||
| 88 | 'id_prefix' => '' |
||
| 89 | ] |
||
| 90 | ]; |
||
| 91 | |||
| 92 | $environment = new Environment($markdownConfig); |
||
| 93 | $environment->addExtension(new CommonMarkCoreExtension()); |
||
| 94 | $environment->addExtension(new GithubFlavoredMarkdownExtension()); |
||
| 95 | $environment->addExtension(new HeadingPermalinkExtension()); |
||
| 96 | $environment->addExtension(new EmbedExtension()); |
||
| 97 | $converter = new MarkdownConverter($environment); |
||
| 98 | |||
| 99 | $parsedReplies = []; |
||
| 100 | $file = $this->app->get('blogfile'); |
||
| 101 | $articles = json_decode(file_get_contents($file), true); |
||
| 102 | foreach ($articles as $index => $article) { |
||
| 103 | if ($article['permlink'] === $permlink) { |
||
| 104 | $metadata = json_decode($article['json_metadata'], true); |
||
| 105 | $article['body'] = $converter->convert($article['body']); |
||
| 106 | |||
| 107 | // Check if comments exists for this post |
||
| 108 | $cmts = $this->app->get('commentsdir') . $permlink . '.comments'; |
||
| 109 | $delay = $settings['delay']; |
||
| 110 | if ((!file_exists($cmts)) || (file_exists($cmts)) && (time() - filemtime($cmts) > $delay)) { |
||
| 111 | $api = new HiveCondenser($apiConfig); |
||
| 112 | $replies = $api->getContentReplies($article['author'], $permlink); |
||
| 113 | $result = json_encode($replies, JSON_PRETTY_PRINT); |
||
| 114 | file_put_contents($cmts, $result); |
||
| 115 | } |
||
| 116 | $replies = json_decode(file_get_contents($cmts), true); |
||
| 117 | |||
| 118 | foreach ($replies as $reply) { |
||
| 119 | $reply['body'] = $converter->convert($reply['body']); |
||
| 120 | $parsedReplies[] = $reply; |
||
| 121 | } |
||
| 122 | |||
| 123 | return $this->app->get('view')->render($response, $settings['theme'] . '/post.html', [ |
||
| 124 | 'settings' => $settings, |
||
| 125 | 'article' => $article, |
||
| 126 | 'metadata' => $metadata, |
||
| 127 | 'replies' => $parsedReplies, |
||
| 128 | ]); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } |
||
| 132 | return $response; |
||
| 133 | } |
||
| 277 |