Passed
Push — master ( b561fd...9fd469 )
by Florent
03:06
created

PostsController::tag()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 10
nop 3
dl 0
loc 31
rs 9.0444
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Post controller
5
 *
6
 * The file contains all the functions used for posts.
7
 * Display / Save / update / ...
8
 *
9
 * @category   Controllers
10
 * @package    SuperHive
11
 * @author     Florent Kosmala <[email protected]>
12
 * @license    https://www.gnu.org/licenses/gpl-3.0.txt GPL-3.0
13
 */
14
15
namespace App\Controllers;
16
17
use DI\Container;
18
use Psr\Http\Message\ResponseInterface as Response;
19
use Psr\Http\Message\ServerRequestInterface as Request;
20
use Psr\Container\ContainerInterface;
21
use Slim\Factory\AppFactory;
22
use Slim\Routing\RouteContext;
23
use Hive\PhpLib\Hive\Condenser as HiveCondenser;
24
use League\CommonMark\CommonMarkConverter;
25
use App\Controllers\CommonController as Common;
26
27
final class PostsController
28
{
29
    private $app;
30
31
    public function __construct(ContainerInterface $app)
32
    {
33
        $this->app = $app;
34
        $genPosts = new Common($this->app);
35
        $genPosts->genPostsFile();
36
    }
37
  
38
    /**
39
     * Post function
40
     *
41
     * This function display the selected post in the blog.json (from Blockchain).
42
     * It also take all comments to display them in the end of post
43
     *
44
     * @param object $request
45
     * @param object $response
46
     * @param array $args
47
     *
48
     * @return object $response
49
     */
50
    public function post(Request $request, Response $response, $args): Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

50
    public function post(/** @scrutinizer ignore-unused */ Request $request, Response $response, $args): Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
    {
52
        $settings = $this->app->get('settings');
53
        
54
        $apiConfig = [
55
            "hiveNode" => $settings['api'],
56
            "debug" => false
57
        ];
58
59
        if (isset($args['permlink'])) {
60
            $permlink = $args['permlink'];
61
            
62
            $converter = new CommonMarkConverter();
63
            $parsedReplies = array();
64
            
65
            $file = $this->app->get('blogfile');
66
            $articles = json_decode(file_get_contents($file), true);
67
            foreach ($articles as $index => $article) {
68
                if ($article['permlink'] == $permlink) {
69
                    $metadata = json_decode($article['json_metadata'], true);
70
                    $article['body'] = $converter->convert($article['body']);
71
                    
72
                    // Check if comments exists for this post
73
                    $cmts = $this->app->get('commentsdir') . $permlink . '.comments';
74
                    if ((!file_exists($cmts)) || (file_exists($cmts)) && (time() - filemtime($cmts) > 120)) {
75
                        $api = new HiveCondenser($apiConfig);
76
                        $replies = $api->getContentReplies($article['author'], $permlink);
77
                        $result = json_encode($replies, JSON_PRETTY_PRINT);
78
                        file_put_contents($cmts, $result);
79
                    }
80
                    $replies = json_decode(file_get_contents($cmts), true);
81
                    
82
                    foreach ($replies as $reply) {
83
                        $reply['body'] = $converter->convert($reply['body']);
84
                        $parsedReplies[] = $reply;
85
                    }
86
            
87
                    return $this->app->get('view')->render($response, $settings['theme'] . '/post.html', [
88
                        'settings' => $settings,
89
                        'article' => $article,
90
                        'metadata' => $metadata,
91
                        'replies' => $parsedReplies
92
                    ]);
93
                }
94
            }
95
        }
96
        return $response;
97
    }
98
    
99
    /**
100
     * Post function
101
     *
102
     * This function display the selected post in the blog.json (from Blockchain).
103
     * It also take all comments to display them in the end of post
104
     *
105
     * @param object $request
106
     * @param object $response
107
     * @param array $args
108
     *
109
     * @return object $response
110
     */
111
    public function tag(Request $request, Response $response, $args): Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

111
    public function tag(/** @scrutinizer ignore-unused */ Request $request, Response $response, $args): Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
    {
113
        $settings = $this->app->get('settings');
114
        
115
        if (isset($args['tag'])) {
116
            $tag = $args['tag'];
117
            
118
            $file = $this->app->get('blogfile');
119
            $articles = json_decode(file_get_contents($file), true);
120
            
121
            foreach ($articles as $article) {
122
                //Check in Tags
123
                $metadata = json_decode($article['json_metadata'], true);
124
                $tags = implode(",", $metadata['tags']);
125
                if (preg_match("/\b$tag\b/i", $tags)) {
126
                    $matches[] = $article['title'];
127
                }
128
            }
129
            
130
            $result = array_unique($matches);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $matches does not seem to be defined for all execution paths leading up to this point.
Loading history...
131
            
132
            foreach ($articles as $article) {
133
                if (in_array($article['title'], $result)) {
134
                    $posts[] = $article;
135
                }
136
            }
137
            
138
            return $this->app->get('view')->render($response, $settings['theme'] . '/tag.html', [
139
                'tag' => $tag,
140
                'posts' => $posts,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $posts does not seem to be defined for all execution paths leading up to this point.
Loading history...
141
                'settings' => $settings
142
            ]);
143
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 115 is false. This is incompatible with the type-hinted return Psr\Http\Message\ResponseInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
144
    }
145
    
146
    /**
147
     * Administration posts function
148
     *
149
     * This function display the post page in admin panel.
150
     * Contains every posts in blog.json file (from blockchain)
151
     *
152
     * @param object $request
153
     * @param object $response
154
     * @param array $args
155
     *
156
     * @return object $response
157
     */
158
    public function adminPosts(Request $request, Response $response): Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

158
    public function adminPosts(/** @scrutinizer ignore-unused */ Request $request, Response $response): Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
159
    {
160
        $settings = $this->app->get('settings');
161
        
162
        $file = $this->app->get('blogfile');
163
        $blog = json_decode(file_get_contents($file), true);
164
        
165
        return $this->app->get('view')->render($response, '/admin/admin-posts.html', [
166
                'settings' => $settings,
167
                'posts' => $blog
168
        ]);
169
    }
170
    
171
    /**
172
     * Administration new post function
173
     *
174
     * This function just display the new post page to write and send post.
175
     *
176
     * @param object $request
177
     * @param object $response
178
     * @param array $args
179
     *
180
     * @return object $response
181
     */
182
    public function adminNewPost(Request $request, Response $response): Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

182
    public function adminNewPost(/** @scrutinizer ignore-unused */ Request $request, Response $response): Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
183
    {
184
        $settings = $this->app->get('settings');
185
        
186
        return $this->app->get('view')->render($response, '/admin/admin-newpost.html', [
187
                'settings' => $settings
188
        ]);
189
    }
190
    
191
    /**
192
     * Administration edit post function
193
     *
194
     * Same as adminNewPost but with already written content from an old post.
195
     *
196
     * @param object $request
197
     * @param object $response
198
     * @param array $args
199
     *
200
     * @return object $response
201
     */
202
    public function adminEditPost(Request $request, Response $response, array $args): Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

202
    public function adminEditPost(/** @scrutinizer ignore-unused */ Request $request, Response $response, array $args): Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
203
    {
204
        $posted = $args['post'];
205
        
206
        $file = $this->app->get('blogfile');
207
        $settings = $this->app->get('settings');
208
209
        $posts = json_decode(file_get_contents($file), true);
210
        
211
        $permlinks = array();
212
        
213
        foreach ($posts as $post) {
214
            $permlinks[] = $post["permlink"];
215
        }
216
        
217
        $column = array_column($posts, 'permlink');
218
        $postIndex = array_search($posted, $column);
219
        
220
        if (is_numeric($postIndex)) {
221
            $post = $posts[$postIndex];
222
            $postTitle = $post['title'];
223
            $permlink = $post['permlink'];
224
            $content = $post['body'];
225
            $metadata = json_decode($post['json_metadata']);
226
            
227
            return $this->app->get('view')->render($response, '/admin/admin-newpost.html', [
228
                'settings' => $settings,
229
                'postTitle' => $postTitle,
230
                'postContent' => $content,
231
                'postPermlink' => $permlink,
232
                'postTags' => $metadata->tags
233
            ]);
234
        } else {
235
            $response->getBody()->write("No Post Found");
236
            return $response;
237
        }
238
    }
239
}
240