Passed
Branch master (b40cdb)
by Florent
14:05
created

CommonController::genPostsFile()   B

Complexity

Conditions 9
Paths 6

Size

Total Lines 59
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 39
nc 6
nop 0
dl 0
loc 59
rs 7.7404
c 1
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
/**
4
 * Common controller
5
 *
6
 * The file contains every function needed in many other controllers.
7
 *
8
 * @category   Controllers
9
 * @package    SuperHive
10
 * @author     Florent Kosmala <[email protected]>
11
 * @license    https://www.gnu.org/licenses/gpl-3.0.txt GPL-3.0
12
 */
13
14
namespace App\Controllers;
15
16
use DI\Container;
17
use Psr\Http\Message\ResponseInterface as Response;
18
use Psr\Http\Message\ServerRequestInterface as Request;
19
use Psr\Container\ContainerInterface;
20
use Slim\Factory\AppFactory;
21
use Hive\PhpLib\Hive\Condenser as HiveCondenser;
22
23
final class CommonController
24
{
25
    private $app;
26
27
    public function __construct(ContainerInterface $app)
28
    {
29
        $this->app = $app;
30
    }
31
    
32
    /**
33
     * getPosts function
34
     *
35
     * This function will query the blockchain to have posts
36
     * It will generate a json file stored in /resources/blog/ folder
37
     *
38
     * @param object $request
39
     * @param object $response
40
     * @param array $args
41
     *
42
     * @return object $response
43
     */
44
    public function genPostsFile()
45
    {
46
        $settings = $this->app->get('settings');
47
48
        // Hive API communication init
49
        $apiConfig = [
50
            "hiveNode" => $settings['api'],
51
            "debug" => false
52
        ];
53
        $api = new HiveCondenser($apiConfig);
54
        
55
        // The file with the latest posts.
56
        $file = $this->app->get('blogfile');
57
        
58
        // if the JSON file doesn't exist or if it's old, take it from API
59
        if (!file_exists($file) || (time() - filemtime($file) > 120)) {
60
            // Prepare API call according to displayed posts type
61
            $displayType = $settings['displayType']['type'];
62
            if ($displayType === 'author') {
63
                $dateNow = (new \DateTime())->format('Y-m-d\TH:i:s');
64
                $result = json_encode(
65
                    $api->getDiscussionsByAuthorBeforeDate(
66
                        $settings['author'],
67
                        "",
68
                        $dateNow,
69
                        100
70
                    ),
71
                    JSON_PRETTY_PRINT
72
                );
73
            } elseif (($displayType === 'tag')) {
74
                $displayTag = $settings['displayType']['tag'];
75
                $taggedPosts = array();
76
                $allPosts = json_encode(
77
                    $api->getDiscussionsByAuthorBeforeDate(
78
                        $settings['author'],
79
                        "",
80
                        "",
81
                        100
82
                    )
83
                );
84
                $allPosts = json_decode($allPosts, true);
85
                //print_r($allPosts);
86
                foreach ($allPosts as &$post) {
87
                    //$postTags = json_encode($post['json_metadata'], JSON_PRETTY_PRINT);
88
                    $postMeta = json_decode($post['json_metadata'], true);
89
                    $postTags = $postMeta['tags'];
90
                    if (in_array($displayTag, $postTags)) {
91
                        $taggedPosts[] = $post;
92
                    }
93
                }
94
                
95
                $result = json_encode($taggedPosts, JSON_PRETTY_PRINT);
96
                unset($taggedPosts);
97
            } elseif ($displayType === 'reblog') {
98
                $result = json_encode($api->getDiscussionsByBlog($settings['author']), JSON_PRETTY_PRINT);
99
            } elseif (strpos($settings['author'], "hive-") === 0) {
100
                $result = json_encode($api->getDiscussionsByCreated($settings['author']), JSON_PRETTY_PRINT);
101
            }
102
            file_put_contents($file, $result);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.
Loading history...
103
        }
104
    }
105
}
106