Fetcher   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 128
rs 10
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C getNewMembers() 0 39 7
B getFeed() 0 37 6
1
<?php
2
3
namespace PhpEarth\Stats;
4
5
use Symfony\Component\Console\Helper\ProgressBar;
6
use Facebook\Facebook;
7
use Facebook\Exceptions\FacebookSDKException;
8
use Facebook\Exceptions\FacebookResponseException;
9
10
/**
11
 * Class Fetcher.
12
 */
13
class Fetcher
14
{
15
    /**
16
     * @var Facebook
17
     */
18
    protected $fb;
19
20
    /**
21
     * @var Config
22
     */
23
    protected $config;
24
25
    /**
26
     * @var ProgressBar
27
     */
28
    protected $progress;
29
30
    /**
31
     * @var array
32
     */
33
    protected $feed = [];
34
35
    /**
36
     * Mapper constructor.
37
     *
38
     * @param Config $config
39
     * @param ProgressBar $progress
40
     * @param Facebook $fb
41
     */
42
    public function __construct(Config $config, ProgressBar $progress, Facebook $fb)
43
    {
44
        $this->config = $config;
45
        $this->progress = $progress;
46
        $this->fb = $fb;
47
    }
48
49
    /**
50
     * Fetch feed from API data.
51
     *
52
     * @return array
53
     *
54
     * @throws \Exception
55
     */
56
    public function getFeed()
57
    {
58
        $this->progress->setMessage('Fetching feed...');
59
        $this->progress->advance();
60
61
        $this->feed = [];
62
63
        try {
64
            $pagesCount = 0;
65
            $since = $this->config->getParameter('start_datetime')->getTimestamp() - $this->config->getParameter('offset');
66
            $until = $this->config->getParameter('end_datetime')->getTimestamp() + $this->config->getParameter('offset');
67
            $response = $this->fb->get('/'.$this->config->getParameter('group_id').'/feed?fields=comments.limit(200).summary(1){comment_count,from,created_time,message,can_comment,reactions.limit(0).summary(1),comments.limit(200).summary(1){comment_count,from,created_time,message,reactions.limit(0).summary(1)}},reactions.limit(0).summary(1),from,created_time,updated_time,message,type,attachments{type},shares&include_hidden=true&limit=50&since='.$since.'&until='.$until);
68
69
            $feedEdge = $response->getGraphEdge();
70
71
            if (count($feedEdge) > 0) {
72
                do {
73
                    ++$pagesCount;
74
                    $this->progress->setMessage('Fetching feed from API page '.$pagesCount.' and with the topic updated '.$feedEdge[0]->getField('updated_time')->format('Y-m-d H:i:s'));
75
                    $this->progress->advance();
76
                    foreach ($feedEdge as $topic) {
77
                        $this->feed[] = $topic;
78
                    }
79
                } while ($feedEdge = $this->fb->next($feedEdge));
80
            }
81
        } catch (FacebookResponseException $e) {
82
            // When Graph returns an error
83
            throw new \Exception('Graph returned an error: '.$e->getMessage());
84
        } catch (FacebookSDKException $e) {
85
            // When validation fails or other local issues
86
            throw new \Exception('Facebook SDK returned an error: '.$e->getMessage());
87
        }
88
89
        $this->progress->setMessage('Adding topics to collection...');
90
        $this->progress->advance();
91
92
        return $this->feed;
93
    }
94
95
    /**
96
     * Get number of new users since the user's name set in app configuration.
97
     *
98
     * @return int
99
     *
100
     * @throws \Exception
101
     */
102
    public function getNewMembers()
103
    {
104
        $this->progress->setMessage('Retrieving members...');
105
        $this->progress->advance();
106
        $newMembers = [];
107
        $pagesCount = 0;
108
109
        try {
110
            $response = $this->fb->get('/'.$this->config->getParameter('group_id').'/members?fields=id,name,joined&limit=1000');
111
112
            $feedEdge = $response->getGraphEdge();
113
            do {
114
                ++$pagesCount;
115
                $this->progress->setMessage('Retrieving members from API page '.$pagesCount);
116
                $this->progress->advance();
117
118
                foreach ($feedEdge as $status) {
119
                    $newMembers[] = [$status->asArray()['id'], $status->asArray()['name']];
120
121
                    if ($status->asArray()['joined'] <= $this->config->getParameter('start_datetime')->getTimestamp()) {
122
                        break 2;
123
                    }
124
                }
125
126
                if ($pagesCount == $this->config->getParameter('api_pages')) {
127
                    break;
128
                }
129
            } while ($feedEdge = $this->fb->next($feedEdge));
130
        } catch (FacebookResponseException $e) {
131
            // When Graph returns an error
132
            throw new \Exception('Graph returned an error: '.$e->getMessage());
133
        } catch (FacebookSDKException $e) {
134
            // When validation fails or other local issues
135
            throw new \Exception('Facebook SDK returned an error: '.$e->getMessage());
136
        }
137
138
        $this->progress->advance();
139
140
        return $newMembers;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $newMembers returns the type array|array<mixed,array> which is incompatible with the documented return type integer.
Loading history...
141
    }
142
}
143