Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
CrudTransactionExecutor | |
0.00% |
0 / 29 |
|
0.00% |
0 / 2 |
210 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
182 |
1 | <?php |
2 | |
3 | namespace MediaWiki\WikispeechSpeechDataCollector\Crud\Transaction; |
4 | |
5 | /** |
6 | * @file |
7 | * @ingroup Extensions |
8 | * @license GPL-2.0-or-later |
9 | */ |
10 | |
11 | use MediaWiki\WikispeechSpeechDataCollector\Crud\Clud; |
12 | use MediaWiki\WikispeechSpeechDataCollector\Crud\CrudContext; |
13 | use MediaWiki\WikispeechSpeechDataCollector\Crud\CrudFactory; |
14 | use MediaWiki\WikispeechSpeechDataCollector\Crud\PersistentRootGraphLoader; |
15 | |
16 | /** |
17 | * @todo This is part of an internal development API. It should be removed before deployment. |
18 | * |
19 | * A facade against {@link Clud} to access and modify a set of |
20 | * {@link Persistent} objects. |
21 | * |
22 | * The name <b>transaction</b> might be a bit misleading, |
23 | * as this is not an ACID transaction with rollback on errors etc. |
24 | * If your execution fails halfway through, the parts executed |
25 | * will have been committed. |
26 | * |
27 | * Consider single object (atomic) calls in case you are uncertain |
28 | * about the validity of the object you pass to the executor. |
29 | * |
30 | * @since 0.1.0 |
31 | */ |
32 | class CrudTransactionExecutor { |
33 | |
34 | /** @var CrudContext */ |
35 | private $context; |
36 | |
37 | /** @var Clud */ |
38 | private $clud; |
39 | |
40 | /** |
41 | * @param CrudContext $context |
42 | * @since 0.1.0 |
43 | */ |
44 | public function __construct( CrudContext $context ) { |
45 | $this->context = $context; |
46 | $this->clud = new Clud( new CrudFactory( $context ) ); |
47 | } |
48 | |
49 | /** |
50 | * @param CrudTransactionRequest $request |
51 | * @return CrudTransactionResponse |
52 | * @throws CrudTransactionException |
53 | * @since 0.1.0 |
54 | */ |
55 | public function execute( |
56 | CrudTransactionRequest $request |
57 | ): CrudTransactionResponse { |
58 | $response = new CrudTransactionResponse(); |
59 | $response->setReference( $request->getReference() ); |
60 | |
61 | if ( $request->getCreate() ) { |
62 | foreach ( $request->getCreate() as $instance ) { |
63 | $this->clud->create( $instance ); |
64 | $response->addCreated( $instance ); |
65 | } |
66 | } |
67 | |
68 | if ( $request->getRead() ) { |
69 | foreach ( $request->getRead() as $instance ) { |
70 | if ( $this->clud->load( $instance ) ) { |
71 | $response->addRead( $instance ); |
72 | } else { |
73 | throw new CrudTransactionException( "Unable to read $instance" ); |
74 | } |
75 | } |
76 | if ( $request->isReadGraph() ) { |
77 | $graphLoader = new PersistentRootGraphLoader( $this->context ); |
78 | foreach ( $response->getRead() as $instance ) { |
79 | $instance->accept( $graphLoader ); |
80 | } |
81 | $graphLoader->getLoadedInstances()->removeAll( $response->getRead() ); |
82 | foreach ( $graphLoader->getLoadedInstances()->toArray() as $instance ) { |
83 | $response->addRead( $instance ); |
84 | } |
85 | } |
86 | } |
87 | |
88 | if ( $request->getUpdate() ) { |
89 | foreach ( $request->getUpdate() as $instance ) { |
90 | $this->clud->update( $instance ); |
91 | $response->addUpdated( $instance ); |
92 | } |
93 | } |
94 | |
95 | if ( $request->getDelete() ) { |
96 | foreach ( $request->getDelete() as $instance ) { |
97 | $this->clud->delete( $instance ); |
98 | $response->addDeleted( $instance ); |
99 | } |
100 | } |
101 | |
102 | return $response; |
103 | } |
104 | } |