@@ -95,24 +95,19 @@ private function mapFeed($feed)
9595 {
9696 $ startDate = $ this ->config ->getParameter ('start_datetime ' );
9797 $ endDate = $ this ->config ->getParameter ('end_datetime ' );
98-
9998 foreach ($ feed as $ topic ) {
100- if ($ topic[ 'created_time ' ] >= $ startDate && $ topic[ 'created_time ' ] <= $ endDate ) {
99+ if ($ topic-> getField ( 'created_time ' ) >= $ startDate && $ topic-> getField ( 'created_time ' ) <= $ endDate ) {
101100 $ this ->mapTopic ($ topic );
102101 }
103102
104- if (isset ($ topic ['comments ' ])) {
105- foreach ($ topic ['comments ' ] as $ comment ) {
106- if ($ comment ['created_time ' ] >= $ startDate && $ comment ['created_time ' ] <= $ endDate ) {
107- $ this ->mapComment ($ comment );
108- }
103+ foreach ($ topic ->getField ('comments ' , []) as $ i => $ comment ) {
104+ if ($ comment ->getField ('created_time ' ) >= $ startDate && $ comment ->getField ('created_time ' ) <= $ endDate ) {
105+ $ this ->mapComment ($ comment );
106+ }
109107
110- if (isset ($ comment ['comments ' ])) {
111- foreach ($ comment ['comments ' ] as $ reply ) {
112- if ($ reply ['created_time ' ] >= $ startDate && $ reply ['created_time ' ] <= $ endDate ) {
113- $ this ->mapReply ($ reply );
114- }
115- }
108+ foreach ($ comment ->getField ('comments ' , []) as $ j =>$ reply ) {
109+ if ($ reply ->getField ('created_time ' ) >= $ startDate && $ reply ->getField ('created_time ' ) <= $ endDate ) {
110+ $ this ->mapReply ($ reply );
116111 }
117112 }
118113 }
@@ -122,112 +117,115 @@ private function mapFeed($feed)
122117 /**
123118 * Maps topic data from API feed to Topic object.
124119 *
125- * @param array $topic
120+ * @param array $data
126121 *
127122 * @return Topic
128123 *
129124 * @throws \Exception
130125 */
131- private function mapTopic ($ topic )
126+ private function mapTopic ($ data )
132127 {
133- $ newTopic = new Topic ();
134- $ commentsCount = $ topic ['commentsCount ' ];
135- if (array_key_exists ('comments ' , $ topic )) {
136- foreach ($ topic ['comments ' ] as $ comment ) {
137- if (isset ($ comment ['comment_count ' ])) {
138- $ commentsCount += $ comment ['comment_count ' ];
139- }
140- }
141- }
142- $ newTopic ->setCommentsCount ($ commentsCount );
143- $ newTopic ->setId ($ topic ['id ' ]);
144- $ newTopic ->setCreatedTime ($ topic ['created_time ' ]);
145- if (array_key_exists ('message ' , $ topic )) {
146- $ newTopic ->setMessage ($ topic ['message ' ]);
128+ $ topic = new Topic ();
129+
130+ // Count comments and replies
131+ $ commentsCount = $ data ->getField ('comments ' )->getMetaData ()['summary ' ]['total_count ' ];
132+ foreach ($ data ->getField ('comments ' , []) as $ comment ) {
133+ $ commentsCount += $ comment ->getField ('comment_count ' , 0 );
147134 }
148- $ newTopic ->setReactionsCount ($ topic ['reactionsCount ' ]);
149- $ newTopic ->setCanComment ($ topic ['canComment ' ]);
150- $ newTopic ->setType ($ topic ['type ' ]);
151- if ($ newTopic ->getType () == 'link ' && isset ($ topic ['attachments ' ][0 ]['type ' ]) && $ topic ['attachments ' ][0 ]['type ' ] == 'animated_image_share ' ) {
152- $ newTopic ->setType ('animated_image_share ' );
135+ $ topic ->setCommentsCount ($ commentsCount );
136+
137+ $ topic ->setId ($ data ->getField ('id ' ));
138+ $ topic ->setCreatedTime ($ data ->getField ('created_time ' ));
139+ $ topic ->setMessage ($ data ->getField ('message ' ));
140+
141+ $ topic ->setReactionsCount ($ data ->getField ('reactions ' )->getMetaData ()['summary ' ]['total_count ' ]);
142+ $ topic ->setCanComment ($ data ->getField ('comments ' )->getMetaData ()['summary ' ]['can_comment ' ]);
143+ $ topic ->setType ($ data ->getField ('type ' ));
144+
145+ $ dataArray = $ data ->asArray ();
146+
147+ if ($ topic ->getType () == 'link ' && isset ($ dataArray ['attachments ' ][0 ]['type ' ]) && $ dataArray ['attachments ' ][0 ]['type ' ] == 'animated_image_share ' ) {
148+ $ topic ->setType ('animated_image_share ' );
153149 }
154150
155- if (array_key_exists ('from ' , $ topic )) {
156- $ user = $ this ->mapUser ($ topic ['from ' ]);
157- $ user ->addTopic ($ newTopic );
158- $ newTopic ->setUser ($ user );
151+ if (array_key_exists ('from ' , $ dataArray )) {
152+ $ user = $ this ->mapUser ($ dataArray ['from ' ]);
153+ $ user ->addTopic ($ topic );
154+ $ topic ->setUser ($ user );
159155 }
160156
161- if (array_key_exists ('shares ' , $ topic )) {
162- $ newTopic ->setSharesCount ($ topic ['shares ' ]['count ' ]);
157+ if (array_key_exists ('shares ' , $ dataArray )) {
158+ $ topic ->setSharesCount ($ dataArray ['shares ' ]['count ' ]);
163159 }
164160
165161 // Add topic to collection
166- $ this ->topics ->add ($ newTopic , $ newTopic ->getId ());
162+ $ this ->topics ->add ($ topic , $ topic ->getId ());
167163
168164 // Log topic
169- $ log = $ newTopic ->getId ()."\t" ;
170- $ log .= ' Reactions: ' .$ newTopic ->getReactionsCount ()."\t" ;
171- $ log .= ' Comments: ' .$ newTopic ->getCommentsCount ()."\n" ;
165+ $ log = $ topic ->getId ()."\t" ;
166+ $ log .= ' Reactions: ' .$ topic ->getReactionsCount ()."\t" ;
167+ $ log .= ' Comments: ' .$ topic ->getCommentsCount ()."\n" ;
172168 $ this ->log ->logTopic ($ log );
173169
174- return $ newTopic ;
170+ return $ topic ;
175171 }
176172
177173 /**
178174 * Maps comment data from API feed to Comment object.
179175 *
180- * @param array $comment
176+ * @param array $data
181177 *
182178 * @return Comment
183179 *
184180 * @throws \Exception
185181 */
186- private function mapComment ($ comment )
182+ private function mapComment ($ data )
187183 {
188- $ newComment = new Comment ();
189- $ newComment ->setId ($ comment [ 'id ' ] );
190- $ newComment ->setMessage ($ comment [ 'message ' ] );
191- $ newComment -> setLikesCount ( $ comment [ ' like_count ' ]);
184+ $ comment = new Comment ();
185+ $ comment ->setId ($ data -> getField ( 'id ' ) );
186+ $ comment ->setMessage ($ data -> getField ( 'message ' ) );
187+ $ comment -> setReactionsCount ( $ data -> getField ( ' reactions ' )-> getMetaData ()[ ' summary ' ][ ' total_count ' ]);
192188
193- if (array_key_exists ('from ' , $ comment )) {
194- $ user = $ this ->mapUser ($ comment ['from ' ]);
195- $ user ->addComment ($ newComment );
189+ $ dataArray = $ data ->asArray ();
190+ if (array_key_exists ('from ' , $ dataArray )) {
191+ $ user = $ this ->mapUser ($ dataArray ['from ' ]);
192+ $ user ->addComment ($ comment );
196193
197- $ newComment ->setUser ($ user );
194+ $ comment ->setUser ($ user );
198195 }
199196
200- $ this ->comments ->add ($ newComment , $ newComment ->getId ());
197+ $ this ->comments ->add ($ comment , $ comment ->getId ());
201198
202- return $ newComment ;
199+ return $ comment ;
203200 }
204201
205202 /**
206203 * Map reply data from API feed to Reply object.
207204 *
208- * @param array $reply
205+ * @param array $data
209206 *
210207 * @return Reply
211208 *
212209 * @throws \Exception
213210 */
214- private function mapReply ($ reply )
211+ private function mapReply ($ data )
215212 {
216- $ newReply = new Reply ();
217- $ newReply ->setId ($ reply [ 'id ' ] );
218- $ newReply ->setMessage ($ reply [ 'message ' ] );
219- $ newReply -> setLikesCount ( $ reply [ ' like_count ' ]);
213+ $ reply = new Reply ();
214+ $ reply ->setId ($ data -> getField ( 'id ' ) );
215+ $ reply ->setMessage ($ data -> getField ( 'message ' ) );
216+ $ reply -> setReactionsCount ( $ data -> getField ( ' reactions ' )-> getMetaData ()[ ' summary ' ][ ' total_count ' ]);
220217
221- if (array_key_exists ('from ' , $ reply )) {
222- $ user = $ this ->mapUser ($ reply ['from ' ]);
223- $ user ->addReply ($ newReply );
218+ $ dataArray = $ data ->asArray ();
219+ if (array_key_exists ('from ' , $ dataArray )) {
220+ $ user = $ this ->mapUser ($ dataArray ['from ' ]);
221+ $ user ->addReply ($ reply );
224222
225- $ newReply ->setUser ($ user );
223+ $ reply ->setUser ($ user );
226224 }
227225
228- $ this ->replies ->add ($ newReply , $ newReply ->getId ());
226+ $ this ->replies ->add ($ reply , $ reply ->getId ());
229227
230- return $ newReply ;
228+ return $ reply ;
231229 }
232230
233231 /**
0 commit comments