• 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

65.12
/src/ClientOperations/StartTransactionOperation.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\EventStoreTransaction;
19
use Prooph\EventStore\Exception\AccessDenied;
20
use Prooph\EventStore\Exception\InvalidTransaction;
21
use Prooph\EventStore\Exception\StreamDeleted;
22
use Prooph\EventStore\Exception\UnexpectedOperationResult;
23
use Prooph\EventStore\Exception\WrongExpectedVersion;
24
use Prooph\EventStore\Internal\EventStoreTransactionConnection;
25
use Prooph\EventStore\UserCredentials;
26
use Prooph\EventStoreClient\Messages\ClientMessages\OperationResult;
27
use Prooph\EventStoreClient\Messages\ClientMessages\TransactionStart;
28
use Prooph\EventStoreClient\Messages\ClientMessages\TransactionStartCompleted;
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<TransactionStartCompleted, EventStoreTransaction>
37
 */
38
class StartTransactionOperation extends AbstractOperation
39
{
40
    public function __construct(
41
        Logger $logger,
42
        DeferredFuture $deferred,
43
        private readonly bool $requireMaster,
44
        private readonly string $stream,
45
        private readonly int $expectedVersion,
46
        private readonly EventStoreTransactionConnection $parentConnection,
47
        ?UserCredentials $userCredentials
48
    ) {
49
        parent::__construct(
40✔
50
            $logger,
40✔
51
            $deferred,
40✔
52
            $userCredentials,
40✔
53
            TcpCommand::TransactionStart,
40✔
54
            TcpCommand::TransactionStartCompleted,
40✔
55
            TransactionStartCompleted::class
40✔
56
        );
40✔
57
    }
58

59
    protected function createRequestDto(): Message
60
    {
61
        $message = new TransactionStart();
39✔
62
        $message->setRequireMaster($this->requireMaster);
39✔
63
        $message->setEventStreamId($this->stream);
39✔
64
        $message->setExpectedVersion($this->expectedVersion);
39✔
65

66
        return $message;
39✔
67
    }
68

69
    /**
70
     * @param TransactionStartCompleted $response
71
     * @return InspectionResult
72
     */
73
    protected function inspectResponse(Message $response): InspectionResult
74
    {
75
        switch ($response->getResult()) {
39✔
76
            case OperationResult::Success:
77
                $this->succeed($response);
39✔
78

79
                return new InspectionResult(InspectionDecision::EndOperation, 'Success');
39✔
80
            case OperationResult::PrepareTimeout:
81
                return new InspectionResult(InspectionDecision::Retry, 'PrepareTimeout');
×
82
            case OperationResult::CommitTimeout:
83
                return new InspectionResult(InspectionDecision::Retry, 'CommitTimeout');
×
84
            case OperationResult::ForwardTimeout:
85
                return new InspectionResult(InspectionDecision::Retry, 'ForwardTimeout');
×
86
            case OperationResult::WrongExpectedVersion:
87
                $this->fail(WrongExpectedVersion::with(
×
88
                    $this->stream,
×
89
                    $this->expectedVersion
×
90
                ));
×
91

92
                return new InspectionResult(InspectionDecision::EndOperation, 'WrongExpectedVersion');
×
93
            case OperationResult::StreamDeleted:
94
                $this->fail(StreamDeleted::with($this->stream));
×
95

96
                return new InspectionResult(InspectionDecision::EndOperation, 'StreamDeleted');
×
97
            case OperationResult::InvalidTransaction:
98
                $this->fail(new InvalidTransaction());
×
99

100
                return new InspectionResult(InspectionDecision::EndOperation, 'InvalidTransaction');
×
101
            case OperationResult::AccessDenied:
102
                $this->fail(AccessDenied::toStream($this->stream));
×
103

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

110
    protected function transformResponse(Message $response): EventStoreTransaction
111
    {
112
        return new EventStoreTransaction(
39✔
113
            (int) $response->getTransactionId(),
39✔
114
            $this->credentials,
39✔
115
            $this->parentConnection
39✔
116
        );
39✔
117
    }
118

119
    public function name(): string
120
    {
121
        return 'StartTransaction';
39✔
122
    }
123

124
    public function __toString(): string
125
    {
126
        return \sprintf(
39✔
127
            'Stream: %s, ExpectedVersion: %d, RequireMaster: %s',
39✔
128
            $this->stream,
39✔
129
            $this->expectedVersion,
39✔
130
            $this->requireMaster ? 'yes' : 'no'
39✔
131
        );
39✔
132
    }
133
}
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