-
Notifications
You must be signed in to change notification settings - Fork 31
/
StartAsyncTransactionOperation.php
143 lines (126 loc) · 5.33 KB
/
StartAsyncTransactionOperation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* This file is part of the prooph/event-store-client.
* (c) 2018-2018 prooph software GmbH <contact@prooph.de>
* (c) 2018-2018 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Prooph\EventStoreClient\ClientOperations;
use Amp\Deferred;
use Prooph\EventStoreClient\EventStoreAsyncTransaction;
use Prooph\EventStoreClient\Exception\AccessDeniedException;
use Prooph\EventStoreClient\Exception\InvalidTransactionException;
use Prooph\EventStoreClient\Exception\StreamDeletedException;
use Prooph\EventStoreClient\Exception\UnexpectedOperationResult;
use Prooph\EventStoreClient\Exception\WrongExpectedVersionException;
use Prooph\EventStoreClient\Internal\EventStoreAsyncTransactionConnection;
use Prooph\EventStoreClient\Messages\ClientMessages\OperationResult;
use Prooph\EventStoreClient\Messages\ClientMessages\TransactionStart;
use Prooph\EventStoreClient\Messages\ClientMessages\TransactionStartCompleted;
use Prooph\EventStoreClient\SystemData\InspectionDecision;
use Prooph\EventStoreClient\SystemData\InspectionResult;
use Prooph\EventStoreClient\SystemData\TcpCommand;
use Prooph\EventStoreClient\UserCredentials;
use ProtobufMessage;
use Psr\Log\LoggerInterface as Logger;
/** @internal */
class StartAsyncTransactionOperation extends AbstractOperation
{
/** @var bool */
private $requireMaster;
/** @var string */
private $stream;
/** @var int */
private $expectedVersion;
/** @var EventStoreAsyncTransactionConnection */
protected $parentConnection;
public function __construct(
Logger $logger,
Deferred $deferred,
bool $requireMaster,
string $stream,
int $expectedVersion,
EventStoreAsyncTransactionConnection $parentConnection,
?UserCredentials $userCredentials
) {
$this->requireMaster = $requireMaster;
$this->stream = $stream;
$this->expectedVersion = $expectedVersion;
$this->parentConnection = $parentConnection;
parent::__construct(
$logger,
$deferred,
$userCredentials,
TcpCommand::transactionStart(),
TcpCommand::transactionStartCompleted(),
TransactionStartCompleted::class
);
}
protected function createRequestDto(): ProtobufMessage
{
$message = new TransactionStart();
$message->setRequireMaster($this->requireMaster);
$message->setEventStreamId($this->stream);
$message->setExpectedVersion($this->expectedVersion);
return $message;
}
protected function inspectResponse(ProtobufMessage $response): InspectionResult
{
/** @var TransactionStartCompleted $response */
switch ($response->getResult()) {
case OperationResult::Success:
$this->succeed($response);
return new InspectionResult(InspectionDecision::endOperation(), 'Success');
case OperationResult::PrepareTimeout:
return new InspectionResult(InspectionDecision::retry(), 'PrepareTimeout');
case OperationResult::CommitTimeout:
return new InspectionResult(InspectionDecision::retry(), 'CommitTimeout');
case OperationResult::ForwardTimeout:
return new InspectionResult(InspectionDecision::retry(), 'ForwardTimeout');
case OperationResult::WrongExpectedVersion:
$exception = new WrongExpectedVersionException(\sprintf(
'Start transaction failed due to WrongExpectedVersion. Stream: \'%s\', Expected version: \'%s\'',
$this->stream,
$this->expectedVersion
));
$this->fail($exception);
return new InspectionResult(InspectionDecision::endOperation(), 'WrongExpectedVersion');
case OperationResult::StreamDeleted:
$this->fail(StreamDeletedException::with($this->stream));
return new InspectionResult(InspectionDecision::endOperation(), 'StreamDeleted');
case OperationResult::InvalidTransaction:
$this->fail(new InvalidTransactionException());
return new InspectionResult(InspectionDecision::endOperation(), 'InvalidTransaction');
case OperationResult::AccessDenied:
$this->fail(AccessDeniedException::toStream($this->stream));
return new InspectionResult(InspectionDecision::endOperation(), 'AccessDenied');
default:
throw new UnexpectedOperationResult();
}
}
protected function transformResponse(ProtobufMessage $response): EventStoreAsyncTransaction
{
/** @var TransactionStartCompleted $response */
return new EventStoreAsyncTransaction(
$response->getTransactionId(),
$this->credentials,
$this->parentConnection
);
}
public function name(): string
{
return 'StartAsyncTransaction';
}
public function __toString(): string
{
return \sprintf(
'Stream: %s, ExpectedVersion: %d, RequireMaster: %s',
$this->stream,
$this->expectedVersion,
$this->requireMaster ? 'yes' : 'no'
);
}
}