2
2
3
3
namespace App \Service ;
4
4
5
+ use App \DataTransferObject \ApiInfo ;
6
+ use App \DataTransferObject \Shadowing \ContestData ;
5
7
use App \Entity \BaseApiEntity ;
6
8
use App \Entity \Clarification ;
7
9
use App \Entity \Contest ;
24
26
use Doctrine \DBAL \Exception as DBALException ;
25
27
use Doctrine \ORM \EntityManagerInterface ;
26
28
use Doctrine \ORM \NonUniqueResultException ;
27
- use JsonException ;
29
+ use Exception ;
28
30
use LogicException ;
29
31
use Psr \Log \LoggerInterface ;
30
32
use Symfony \Component \DependencyInjection \Attribute \Autowire ;
33
35
use Symfony \Component \PropertyAccess \Exception \UnexpectedTypeException ;
34
36
use Symfony \Component \PropertyAccess \Exception \UninitializedPropertyException ;
35
37
use Symfony \Component \PropertyAccess \PropertyAccess ;
38
+ use Symfony \Component \Serializer \SerializerInterface ;
36
39
use Symfony \Contracts \HttpClient \Exception \DecodingExceptionInterface ;
37
40
use Symfony \Contracts \HttpClient \Exception \HttpExceptionInterface ;
38
41
use Symfony \Contracts \HttpClient \Exception \TransportExceptionInterface ;
@@ -46,19 +49,8 @@ class ExternalContestSourceService
46
49
protected ?ExternalContestSource $ source = null ;
47
50
48
51
protected bool $ contestLoaded = false ;
49
- /**
50
- * @var array{scoreboard_type: string, external_id: string, end_time: string, duration: string, name: string,
51
- * scoreboard_freeze_duration: string, id: string, allow_submit: bool, allow_submit: bool,
52
- * penalty_time: int, start_time?: string|null, formal_name?: string|null, shortname?: string|null,
53
- * scoreboard_thaw_time?: string, warning_message?: string|null} $cachedContestData
54
- */
55
- protected ?array $ cachedContestData = null ;
56
- /**
57
- * @var array{version: string, version_url: string, name: string,
58
- * provider?: array{name?: string, build_date?: string, version?: string},
59
- * domjudge?: array{apiversion: int, version: string, environment: string, doc_url: string}} $cachedApiInfoData
60
- */
61
- protected ?array $ cachedApiInfoData = null ;
52
+ protected ?ContestData $ cachedContestData = null ;
53
+ protected ?ApiInfo $ cachedApiInfoData = null ;
62
54
protected ?string $ loadingError = null ;
63
55
protected bool $ shouldStopReading = false ;
64
56
/** @var array<string, string> $verdicts */
@@ -109,6 +101,7 @@ public function __construct(
109
101
protected readonly EventLogService $ eventLog ,
110
102
protected readonly SubmissionService $ submissionService ,
111
103
protected readonly ScoreboardService $ scoreboardService ,
104
+ protected readonly SerializerInterface $ serializer ,
112
105
#[Autowire('%domjudge.version% ' )]
113
106
string $ domjudgeVersion
114
107
) {
@@ -159,7 +152,7 @@ public function getContestId(): string
159
152
throw new LogicException ('The contest source is not valid ' );
160
153
}
161
154
162
- return $ this ->cachedContestData [ ' id ' ] ;
155
+ return $ this ->cachedContestData -> id ;
163
156
}
164
157
165
158
public function getContestName (): string
@@ -168,16 +161,16 @@ public function getContestName(): string
168
161
throw new LogicException ('The contest source is not valid ' );
169
162
}
170
163
171
- return $ this ->cachedContestData [ ' name ' ] ;
164
+ return $ this ->cachedContestData -> name ;
172
165
}
173
166
174
167
public function getContestStartTime (): ?float
175
168
{
176
169
if (!$ this ->isValidContestSource ()) {
177
170
throw new LogicException ('The contest source is not valid ' );
178
171
}
179
- if (isset ($ this ->cachedContestData [ ' start_time ' ] )) {
180
- return Utils::toEpochFloat ($ this ->cachedContestData [ ' start_time ' ] );
172
+ if (isset ($ this ->cachedContestData -> startTime )) {
173
+ return Utils::toEpochFloat ($ this ->cachedContestData -> startTime );
181
174
} else {
182
175
$ this ->logger ->warning ('Contest has no start time, is the contest paused? ' );
183
176
return null ;
@@ -190,7 +183,7 @@ public function getContestDuration(): string
190
183
throw new LogicException ('The contest source is not valid ' );
191
184
}
192
185
193
- return $ this ->cachedContestData [ ' duration ' ] ;
186
+ return $ this ->cachedContestData -> duration ;
194
187
}
195
188
196
189
public function getApiVersion (): ?string
@@ -199,7 +192,7 @@ public function getApiVersion(): ?string
199
192
throw new LogicException ('The contest source is not valid ' );
200
193
}
201
194
202
- return $ this ->cachedApiInfoData [ ' version ' ] ?? null ;
195
+ return $ this ->cachedApiInfoData -> version ?? null ;
203
196
}
204
197
205
198
public function getApiVersionUrl (): ?string
@@ -208,7 +201,7 @@ public function getApiVersionUrl(): ?string
208
201
throw new LogicException ('The contest source is not valid ' );
209
202
}
210
203
211
- return $ this ->cachedApiInfoData [ ' version_url ' ] ?? null ;
204
+ return $ this ->cachedApiInfoData -> versionUrl ?? null ;
212
205
}
213
206
214
207
public function getApiProviderName (): ?string
@@ -217,7 +210,7 @@ public function getApiProviderName(): ?string
217
210
throw new LogicException ('The contest source is not valid ' );
218
211
}
219
212
220
- return $ this ->cachedApiInfoData [ ' provider ' ][ ' name ' ] ?? $ this ->cachedApiInfoData [ ' name ' ] ?? null ;
213
+ return $ this ->cachedApiInfoData -> provider ?-> name ?? $ this ->cachedApiInfoData -> name ;
221
214
}
222
215
223
216
public function getApiProviderVersion (): ?string
@@ -226,7 +219,7 @@ public function getApiProviderVersion(): ?string
226
219
throw new LogicException ('The contest source is not valid ' );
227
220
}
228
221
229
- return $ this ->cachedApiInfoData [ ' provider ' ][ ' version ' ] ?? null ;
222
+ return $ this ->cachedApiInfoData -> provider ?-> version ?? $ this -> cachedApiInfoData -> domjudge ?->version ;
230
223
}
231
224
232
225
public function getApiProviderBuildDate (): ?string
@@ -235,7 +228,7 @@ public function getApiProviderBuildDate(): ?string
235
228
throw new LogicException ('The contest source is not valid ' );
236
229
}
237
230
238
- return $ this ->cachedApiInfoData [ ' provider ' ][ ' build_date ' ] ?? null ;
231
+ return $ this ->cachedApiInfoData -> provider ?->buildDate ;
239
232
}
240
233
241
234
public function getLoadingError (): string
@@ -494,7 +487,6 @@ function (
494
487
* - A boolean that can be set to true (pass-by-reference) to stop processing
495
488
*
496
489
* @param resource $filePointer
497
- * @throws JsonException
498
490
*/
499
491
protected function readEventsFromFile ($ filePointer , callable $ callback ): void
500
492
{
@@ -555,10 +547,10 @@ protected function loadContest(): void
555
547
}
556
548
$ this ->httpClient = $ this ->httpClient ->withOptions ($ clientOptions );
557
549
$ contestResponse = $ this ->httpClient ->request ('GET ' , $ this ->source ->getSource ());
558
- $ this ->cachedContestData = $ contestResponse ->toArray ( );
550
+ $ this ->cachedContestData = $ this -> serializer -> deserialize ( $ contestResponse ->getContent (), ContestData::class, ' json ' );
559
551
560
552
$ apiInfoResponse = $ this ->httpClient ->request ('GET ' , '' );
561
- $ this ->cachedApiInfoData = $ apiInfoResponse ->toArray ( );
553
+ $ this ->cachedApiInfoData = $ this -> serializer -> deserialize ( $ apiInfoResponse ->getContent (), ApiInfo::class, ' json ' );
562
554
}
563
555
} catch (HttpExceptionInterface |DecodingExceptionInterface |TransportExceptionInterface $ e ) {
564
556
$ this ->cachedContestData = null ;
@@ -580,15 +572,15 @@ protected function loadContest(): void
580
572
$ this ->loadingError = 'event-feed.ndjson not found in archive ' ;
581
573
} else {
582
574
try {
583
- $ this ->cachedContestData = $ this ->dj -> jsonDecode (file_get_contents ($ contestFile ));
584
- } catch (JsonException $ e ) {
575
+ $ this ->cachedContestData = $ this ->serializer -> deserialize (file_get_contents ($ contestFile ), ContestData::class, ' json ' );
576
+ } catch (Exception $ e ) {
585
577
$ this ->loadingError = $ e ->getMessage ();
586
578
}
587
579
588
580
if (is_file ($ apiInfoFile )) {
589
581
try {
590
- $ this ->cachedApiInfoData = $ this ->dj -> jsonDecode (file_get_contents ($ apiInfoFile ));
591
- } catch (JsonException $ e ) {
582
+ $ this ->cachedApiInfoData = $ this ->serializer -> deserialize (file_get_contents ($ apiInfoFile ), ApiInfo::class, ' json ' );
583
+ } catch (Exception $ e ) {
592
584
$ this ->loadingError = $ e ->getMessage ();
593
585
}
594
586
}
0 commit comments