Mapper::mapReply()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 3
dl 21
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
3
namespace PhpEarth\Stats;
4
5
use PhpEarth\Stats\Collection\CommentCollection;
6
use PhpEarth\Stats\Collection\ReplyCollection;
7
use PhpEarth\Stats\Collection\TopicCollection;
8
use PhpEarth\Stats\Collection\UserCollection;
9
use PhpEarth\Stats\Model\Comment;
10
use PhpEarth\Stats\Model\Reply;
11
use PhpEarth\Stats\Model\Topic;
12
use PhpEarth\Stats\Model\User;
13
14
/**
15
 * Class Mapper
16
 */
17
class Mapper
18
{
19
    /**
20
     * @var Config
21
     */
22
    private $config;
23
24
    /**
25
     * @var TopicCollection
26
     */
27
    private $topics;
28
29
    /**
30
     * @var CommentCollection
31
     */
32
    private $comments;
33
34
    /**
35
     * @var ReplyCollection
36
     */
37
    private $replies;
38
39
    /**
40
     * @var UserCollection
41
     */
42
    private $users;
43
44
    /**
45
     * @var Points
46
     */
47
    private $points;
48
49
    /**
50
     * Mapper constructor.
51
     *
52
     * @param Config $config
53
     * @param array $feed
54
     */
55
    public function __construct($config, $feed)
56
    {
57
        $this->config = $config;
58
        $this->topics = new TopicCollection();
59
        $this->comments = new CommentCollection();
60
        $this->replies = new ReplyCollection();
61
        $this->users = new UserCollection();
62
        $this->points = new Points();
63
        $this->points->setPoints($this->config->get('points'));
64
        $this->points->setAdmins($this->config->getParameter('admins'));
65
        $this->points->setOffensiveWords($this->config->get('offensive_words'));
66
67
        $this->mapFeed($feed);
68
    }
69
70
    /**
71
     * Fill the collections from captured API data.
72
     *
73
     * @param array $feed All captured API data as array.
74
     *
75
     * @throws \Exception
76
     */
77
    private function mapFeed($feed)
78
    {
79
        $startDate = $this->config->getParameter('start_datetime');
80
        $endDate = $this->config->getParameter('end_datetime');
81
        foreach ($feed as $topic) {
82
            if ($topic->getField('created_time') >= $startDate && $topic->getField('created_time') <= $endDate) {
83
                $this->mapTopic($topic);
84
            }
85
86
            foreach($topic->getField('comments', []) as $i => $comment) {
87 View Code Duplication
                if ($comment->getField('created_time') >= $startDate && $comment->getField('created_time') <= $endDate) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
                    $this->mapComment($comment, $topic);
89
                }
90
91
                foreach($comment->getField('comments', []) as $j=>$reply) {
92 View Code Duplication
                    if ($reply->getField('created_time') >= $startDate && $reply->getField('created_time') <= $endDate) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
                        $this->mapReply($reply, $topic, $comment);
94
                    }
95
                }
96
            }
97
        }
98
    }
99
100
    /**
101
     * Maps topic data from API feed to Topic object.
102
     *
103
     * @param array $data
104
     *
105
     * @return Topic
106
     *
107
     * @throws \Exception
108
     */
109
    private function mapTopic($data)
110
    {
111
        $topic = new Topic();
112
113
        // Count comments and replies
114
        $commentsCount = $data->getField('comments')->getMetaData()['summary']['total_count'];
115
        foreach ($data->getField('comments', []) as $comment) {
116
            $commentsCount += $comment->getField('comment_count', 0);
117
        }
118
        $topic->setCommentsCount($commentsCount);
119
120
        $topic->setId($data->getField('id'));
121
        $topic->setCreatedTime($data->getField('created_time'));
122
        $topic->setMessage($data->getField('message'));
123
124
        $topic->setReactionsCount($data->getField('reactions')->getMetaData()['summary']['total_count']);
125
        $topic->setCanComment($data->getField('comments')->getMetaData()['summary']['can_comment']);
126
        $topic->setType($data->getField('type'));
127
128
        $dataArray = $data->asArray();
129
130
        if ($topic->getType() == 'link' && isset($dataArray['attachments'][0]['type']) && $dataArray['attachments'][0]['type'] == 'animated_image_share') {
131
            $topic->setType('animated_image_share');
132
        }
133
134
        if (array_key_exists('from', $dataArray)) {
135
            $user = $this->mapUser($dataArray['from']);
136
            $user->addTopic($topic);
137
            $topic->setUser($user);
138
        }
139
140
        if (array_key_exists('shares', $dataArray)) {
141
            $topic->setSharesCount($dataArray['shares']['count']);
142
        }
143
144
        // Add topic to collection
145
        $this->topics->add($topic, $topic->getId());
146
147
        return $topic;
148
    }
149
150
    /**
151
     * Maps comment data from API feed to Comment object.
152
     *
153
     * @param array $data
154
     *
155
     * @return Comment
156
     *
157
     * @throws \Exception
158
     */
159 View Code Duplication
    private function mapComment($data, $topic)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
    {
161
        $comment = new Comment();
162
        $comment->setId($data->getField('id'));
163
        $comment->setMessage($data->getField('message'));
164
        $comment->setReactionsCount($data->getField('reactions')->getMetaData()['summary']['total_count']);
165
        $comment->setCreatedTime($data->getField('created_time'));
166
        $comment->setTopicId($topic->getField('id'));
167
168
        $dataArray = $data->asArray();
169
        if (array_key_exists('from', $dataArray)) {
170
            $user = $this->mapUser($dataArray['from']);
171
            $user->addComment($comment);
172
173
            $comment->setUser($user);
174
        }
175
176
        $this->comments->add($comment, $comment->getId());
177
178
        return $comment;
179
    }
180
181
    /**
182
     * Map reply data from API feed to Reply object.
183
     *
184
     * @param array $data
185
     *
186
     * @return Reply
187
     *
188
     * @throws \Exception
189
     */
190 View Code Duplication
    private function mapReply($data, $topic, $comment)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
    {
192
        $reply = new Reply();
193
        $reply->setId($data->getField('id'));
194
        $reply->setMessage($data->getField('message'));
195
        $reply->setReactionsCount($data->getField('reactions')->getMetaData()['summary']['total_count']);
196
        $reply->setCreatedTime($data->getField('created_time'));
197
        $reply->setTopicId($topic->getField('id'));
198
        $reply->setCommentId($comment->getField('id'));
199
200
        $dataArray = $data->asArray();
201
        if (array_key_exists('from', $dataArray)) {
202
            $user = $this->mapUser($dataArray['from']);
203
            $user->addReply($reply);
204
205
            $reply->setUser($user);
206
        }
207
208
        $this->replies->add($reply, $reply->getId());
209
210
        return $reply;
211
    }
212
213
    /**
214
     * Map user's data from API feed to User object.
215
     *
216
     * @param array $userData
217
     *
218
     * @return User
219
     *
220
     * @throws \Exception
221
     */
222
    private function mapUser($userData)
223
    {
224
        if ($this->users->keyExists($userData['id'])) {
225
            $user = $this->users->get($userData['id']);
226
        } else {
227
            $user = new User($this->points);
228
            $user->setId($userData['id']);
229
            $user->setName($userData['name']);
230
            $user->setFeedComments($this->comments);
231
            $user->setFeedReplies($this->replies);
232
233
            $this->users->add($user, $user->getId());
234
        }
235
236
        return $user;
237
    }
238
239
    /**
240
     * Get mapped topics collection.
241
     *
242
     * @return TopicCollection
243
     */
244
    public function getTopics()
245
    {
246
        return $this->topics;
247
    }
248
249
    /**
250
     * Get mapped comments collection.
251
     *
252
     * @return CommentCollection
253
     */
254
    public function getComments()
255
    {
256
        return $this->comments;
257
    }
258
259
    /**
260
     * Get mapped replies collection.
261
     *
262
     * @return ReplyCollection
263
     */
264
    public function getReplies()
265
    {
266
        return $this->replies;
267
    }
268
269
    /**
270
     * Get mapped users collection.
271
     *
272
     * @return UserCollection
273
     */
274
    public function getUsers()
275
    {
276
        return $this->users;
277
    }
278
}
279