6
6
use Symfony \Component \HttpFoundation \Request ;
7
7
use Symfony \Component \HttpFoundation \Response ;
8
8
use Symfony \Component \Routing \Annotation \Route ;
9
+ use Symfony \Contracts \HttpClient \Exception \ClientExceptionInterface ;
10
+ use Symfony \Contracts \HttpClient \Exception \DecodingExceptionInterface ;
11
+ use Symfony \Contracts \HttpClient \Exception \RedirectionExceptionInterface ;
12
+ use Symfony \Contracts \HttpClient \Exception \ServerExceptionInterface ;
13
+ use Symfony \Contracts \HttpClient \Exception \TransportExceptionInterface ;
9
14
use Symfony \Contracts \HttpClient \HttpClientInterface ;
10
15
use Symfony \UX \Chartjs \Builder \ChartBuilderInterface ;
11
16
use Symfony \UX \Chartjs \Model \Chart ;
@@ -77,6 +82,9 @@ public function __invoke(Request $request): Response
77
82
78
83
private function transformFollowerStats (array $ elasticStats ): array
79
84
{
85
+ if (!isset ($ elasticStats ['aggregations ' ]) || count ($ elasticStats ['aggregations ' ]) <= 0 ) {
86
+ return [];
87
+ }
80
88
$ latestData = end ($ elasticStats ['aggregations ' ][0 ]['buckets ' ]);
81
89
82
90
$ stats = ['total_followers ' => 0 ];
@@ -126,14 +134,15 @@ private function transformFollowerStats(array $elasticStats): array
126
134
return $ stats ;
127
135
}
128
136
129
- private function getStatsFromElastic (): array
137
+ private function getStatsFromElastic (): ? array
130
138
{
131
- $ response = $ this ->elasticsearchClient ->request ('GET ' , '/*user_stats/_search ' , [
132
- 'headers ' => [
133
- 'Content-Type ' => 'application/json ' ,
134
- 'Accept ' => 'application/json ' ,
135
- ],
136
- 'body ' => '
139
+ try {
140
+ $ response = $ this ->elasticsearchClient ->request ('GET ' , '/*user_stats/_search ' , [
141
+ 'headers ' => [
142
+ 'Content-Type ' => 'application/json ' ,
143
+ 'Accept ' => 'application/json ' ,
144
+ ],
145
+ 'body ' => '
137
146
{
138
147
"aggs": {
139
148
"0": {
@@ -194,7 +203,7 @@ private function getStatsFromElastic(): array
194
203
"should": [
195
204
{
196
205
"match": {
197
- "userId": ' . $ this ->getUser ()->getId () . '
206
+ "userId": '. $ this ->getUser ()->getId (). '
198
207
}
199
208
}
200
209
],
@@ -219,8 +228,11 @@ private function getStatsFromElastic(): array
219
228
}
220
229
}
221
230
} ' ,
222
- ]);
231
+ ]);
223
232
224
- return $ response ->toArray ();
233
+ return $ response ->toArray ();
234
+ } catch (ClientExceptionInterface |DecodingExceptionInterface |RedirectionExceptionInterface |ServerExceptionInterface |TransportExceptionInterface $ e ) {
235
+ return null ;
236
+ }
225
237
}
226
238
}
0 commit comments