Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
78.26% |
18 / 23 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
RecordingAnnotationsCrud | |
78.26% |
18 / 23 |
|
75.00% |
3 / 4 |
6.37 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
instanceFactory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
serialize | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
deserialize | |
61.54% |
8 / 13 |
|
0.00% |
0 / 1 |
3.51 |
1 | <?php |
2 | |
3 | namespace MediaWiki\WikispeechSpeechDataCollector\Crud\Mcr; |
4 | |
5 | /** |
6 | * @file |
7 | * @ingroup Extensions |
8 | * @license GPL-2.0-or-later |
9 | */ |
10 | |
11 | use JsonContent; |
12 | use MediaWiki\Revision\RevisionRecord; |
13 | use MediaWiki\Revision\SlotRecord; |
14 | use MediaWiki\Storage\PageUpdater; |
15 | use MediaWiki\WikispeechSpeechDataCollector\Crud\CrudContext; |
16 | use MediaWiki\WikispeechSpeechDataCollector\Domain\Persistent; |
17 | use MediaWiki\WikispeechSpeechDataCollector\Domain\PersistentJsonDeserializer; |
18 | use MediaWiki\WikispeechSpeechDataCollector\Domain\PersistentJsonSerializer; |
19 | use MediaWiki\WikispeechSpeechDataCollector\Domain\RecordingAnnotations; |
20 | use MWException; |
21 | |
22 | /** |
23 | * Multi Content Revision mapping and access for {@link RecordingAnnotations} |
24 | * |
25 | * @since 0.1.0 |
26 | */ |
27 | class RecordingAnnotationsCrud extends AbstractCompositePartMcrCrud { |
28 | |
29 | public const SLOT_ROLE = 'ws_sdc_recording_annotations'; |
30 | |
31 | /** |
32 | * @param CrudContext $context |
33 | * @since 0.1.0 |
34 | */ |
35 | public function __construct( |
36 | CrudContext $context |
37 | ) { |
38 | parent::__construct( |
39 | $context, |
40 | new AbstractMcrCrudUuidIdentityStrategy() |
41 | ); |
42 | } |
43 | |
44 | /** |
45 | * @inheritDoc |
46 | * @since 0.1.0 |
47 | */ |
48 | public function instanceFactory(): Persistent { |
49 | return new RecordingAnnotations(); |
50 | } |
51 | |
52 | /** |
53 | * @inheritDoc |
54 | * @since 0.1.0 |
55 | */ |
56 | public function serialize( Persistent $instance, PageUpdater $pageUpdater ): void { |
57 | $slotRecord = SlotRecord::newUnsaved( |
58 | self::SLOT_ROLE, |
59 | // @todo JsonContent is not @newable. See https://phabricator.wikimedia.org/T275578 |
60 | new JsonContent( $instance->accept( new PersistentJsonSerializer() ) ) |
61 | ); |
62 | $pageUpdater->setSlot( $slotRecord ); |
63 | } |
64 | |
65 | /** |
66 | * @param Persistent $instance |
67 | * @param RevisionRecord $revisionRecord |
68 | * @return bool |
69 | * @throws MWException If MCR content is not an instance of {@link JsonContent}. |
70 | * @since 0.1.0 |
71 | */ |
72 | public function deserialize( Persistent $instance, RevisionRecord $revisionRecord ): bool { |
73 | if ( !$revisionRecord->hasSlot( self::SLOT_ROLE ) ) { |
74 | return false; |
75 | } |
76 | $slotRecord = $revisionRecord->getSlot( |
77 | self::SLOT_ROLE, |
78 | RevisionRecord::FOR_PUBLIC |
79 | ); |
80 | $content = $slotRecord->getContent(); |
81 | if ( !( $content instanceof JsonContent ) ) { |
82 | throw new MWException( |
83 | 'Expected slot content to be an instance of JsonContent but got ' . |
84 | get_class( $content ) . ' instead.' |
85 | ); |
86 | } |
87 | return $instance->accept( new PersistentJsonDeserializer( $content->getText() ) ) !== null; |
88 | } |
89 | |
90 | } |