• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

prooph / event-store-client / 9555551525

17 Jun 2024 10:16PM UTC coverage: 70.262% (-1.1%) from 71.395%
9555551525

push

github

prolic
update coveralls repo token

3466 of 4933 relevant lines covered (70.26%)

67.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.55
/src/ClientOperations/CommitTransactionOperation.php
1
<?php
2

3
/**
4
 * This file is part of `prooph/event-store-client`.
5
 * (c) 2018-2024 Alexander Miertsch <kontakt@codeliner.ws>
6
 * (c) 2018-2024 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace Prooph\EventStoreClient\ClientOperations;
15

16
use Amp\DeferredFuture;
17
use Google\Protobuf\Internal\Message;
18
use Prooph\EventStore\Exception\AccessDenied;
19
use Prooph\EventStore\Exception\InvalidTransaction;
20
use Prooph\EventStore\Exception\StreamDeleted;
21
use Prooph\EventStore\Exception\UnexpectedOperationResult;
22
use Prooph\EventStore\Exception\WrongExpectedVersion;
23
use Prooph\EventStore\Position;
24
use Prooph\EventStore\UserCredentials;
25
use Prooph\EventStore\WriteResult;
26
use Prooph\EventStoreClient\Messages\ClientMessages\OperationResult;
27
use Prooph\EventStoreClient\Messages\ClientMessages\TransactionCommit;
28
use Prooph\EventStoreClient\Messages\ClientMessages\TransactionCommitCompleted;
29
use Prooph\EventStoreClient\SystemData\InspectionDecision;
30
use Prooph\EventStoreClient\SystemData\InspectionResult;
31
use Prooph\EventStoreClient\SystemData\TcpCommand;
32
use Psr\Log\LoggerInterface as Logger;
33

34
/**
35
 * @internal
36
 * @extends AbstractOperation<TransactionCommitCompleted, WriteResult>
37
 */
38
class CommitTransactionOperation extends AbstractOperation
39
{
40
    public function __construct(
41
        Logger $logger,
42
        DeferredFuture $deferred,
43
        private readonly bool $requireMaster,
44
        private readonly int $transactionId,
45
        ?UserCredentials $userCredentials
46
    ) {
47
        parent::__construct(
39✔
48
            $logger,
39✔
49
            $deferred,
39✔
50
            $userCredentials,
39✔
51
            TcpCommand::TransactionCommit,
39✔
52
            TcpCommand::TransactionCommitCompleted,
39✔
53
            TransactionCommitCompleted::class
39✔
54
        );
39✔
55
    }
56

57
    protected function createRequestDto(): Message
58
    {
59
        $message = new TransactionCommit();
39✔
60
        $message->setRequireMaster($this->requireMaster);
39✔
61
        $message->setTransactionId($this->transactionId);
39✔
62

63
        return $message;
39✔
64
    }
65

66
    /**
67
     * @param TransactionCommitCompleted $response
68
     * @return InspectionResult
69
     */
70
    protected function inspectResponse(Message $response): InspectionResult
71
    {
72
        switch ($response->getResult()) {
39✔
73
            case OperationResult::Success:
74
                $this->succeed($response);
35✔
75

76
                return new InspectionResult(InspectionDecision::EndOperation, 'Success');
35✔
77
            case OperationResult::PrepareTimeout:
78
                return new InspectionResult(InspectionDecision::Retry, 'PrepareTimeout');
×
79
            case OperationResult::ForwardTimeout:
80
                return new InspectionResult(InspectionDecision::Retry, 'ForwardTimeout');
×
81
            case OperationResult::CommitTimeout:
82
                return new InspectionResult(InspectionDecision::Retry, 'CommitTimeout');
×
83
            case OperationResult::WrongExpectedVersion:
84
                $this->fail(new WrongExpectedVersion(\sprintf(
6✔
85
                    'Commit transaction failed due to WrongExpectedVersion. Transaction id: \'%s\'',
6✔
86
                    $this->transactionId
6✔
87
                )));
6✔
88

89
                return new InspectionResult(InspectionDecision::EndOperation, 'WrongExpectedVersion');
6✔
90
            case OperationResult::StreamDeleted:
91
                $this->fail(new StreamDeleted());
1✔
92

93
                return new InspectionResult(InspectionDecision::EndOperation, 'StreamDeleted');
1✔
94
            case OperationResult::InvalidTransaction:
95
                $this->fail(new InvalidTransaction());
×
96

97
                return new InspectionResult(InspectionDecision::EndOperation, 'InvalidTransaction');
×
98
            case OperationResult::AccessDenied:
99
                $exception = new AccessDenied('Write access denied');
×
100
                $this->fail($exception);
×
101

102
                return new InspectionResult(InspectionDecision::EndOperation, 'AccessDenied');
×
103
            default:
104
                throw new UnexpectedOperationResult();
×
105
        }
106
    }
107

108
    /**
109
     * @param TransactionCommitCompleted $response
110
     * @return WriteResult
111
     */
112
    protected function transformResponse(Message $response): WriteResult
113
    {
114
        /** @psalm-suppress DocblockTypeContradiction */
115
        return new WriteResult(
35✔
116
            (int) $response->getLastEventNumber(),
35✔
117
            new Position(
35✔
118
                (int) ($response->getCommitPosition() ?? -1),
35✔
119
                (int) ($response->getPreparePosition() ?? -1)
35✔
120
            )
35✔
121
        );
35✔
122
    }
123

124
    public function name(): string
125
    {
126
        return 'CommitTransaction';
39✔
127
    }
128

129
    public function __toString(): string
130
    {
131
        return \sprintf(
39✔
132
            'TransactionId: %s, RequireMaster: %s',
39✔
133
            $this->transactionId,
39✔
134
            $this->requireMaster ? 'yes' : 'no'
39✔
135
        );
39✔
136
    }
137
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc