• 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.41
/src/ClientOperations/TransactionalWriteOperation.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\EventData;
19
use Prooph\EventStore\Exception\AccessDenied;
20
use Prooph\EventStore\Exception\UnexpectedOperationResult;
21
use Prooph\EventStore\UserCredentials;
22
use Prooph\EventStoreClient\Internal\NewEventConverter;
23
use Prooph\EventStoreClient\Messages\ClientMessages\NewEvent;
24
use Prooph\EventStoreClient\Messages\ClientMessages\OperationResult;
25
use Prooph\EventStoreClient\Messages\ClientMessages\TransactionWrite;
26
use Prooph\EventStoreClient\Messages\ClientMessages\TransactionWriteCompleted;
27
use Prooph\EventStoreClient\SystemData\InspectionDecision;
28
use Prooph\EventStoreClient\SystemData\InspectionResult;
29
use Prooph\EventStoreClient\SystemData\TcpCommand;
30
use Psr\Log\LoggerInterface as Logger;
31

32
/**
33
 * @internal
34
 * @extends AbstractOperation<TransactionWriteCompleted, null>
35
 */
36
class TransactionalWriteOperation extends AbstractOperation
37
{
38
    /**
39
     * @param Logger $logger
40
     * @param DeferredFuture $deferred
41
     * @param bool $requireMaster
42
     * @param int $transactionId
43
     * @param list<EventData> $events
44
     * @param ?UserCredentials $userCredentials
45
     */
46
    public function __construct(
47
        Logger $logger,
48
        DeferredFuture $deferred,
49
        private readonly bool $requireMaster,
50
        private readonly int $transactionId,
51
        private readonly array $events,
52
        ?UserCredentials $userCredentials
53
    ) {
54
        parent::__construct(
34✔
55
            $logger,
34✔
56
            $deferred,
34✔
57
            $userCredentials,
34✔
58
            TcpCommand::TransactionWrite,
34✔
59
            TcpCommand::TransactionWriteCompleted,
34✔
60
            TransactionWriteCompleted::class
34✔
61
        );
34✔
62
    }
63

64
    protected function createRequestDto(): Message
65
    {
66
        $events = \array_map(
34✔
67
            fn (EventData $event): NewEvent => NewEventConverter::convert($event),
34✔
68
            $this->events
34✔
69
        );
34✔
70

71
        $message = new TransactionWrite();
34✔
72
        $message->setRequireMaster($this->requireMaster);
34✔
73
        $message->setTransactionId($this->transactionId);
34✔
74
        $message->setEvents($events);
34✔
75

76
        return $message;
34✔
77
    }
78

79
    /**
80
     * @param TransactionWriteCompleted $response
81
     * @return InspectionResult
82
     */
83
    protected function inspectResponse(Message $response): InspectionResult
84
    {
85
        switch ($response->getResult()) {
34✔
86
            case OperationResult::Success:
87
                $this->succeed($response);
34✔
88

89
                return new InspectionResult(InspectionDecision::EndOperation, 'Success');
34✔
90
            case OperationResult::PrepareTimeout:
91
                return new InspectionResult(InspectionDecision::Retry, 'PrepareTimeout');
×
92
            case OperationResult::ForwardTimeout:
93
                return new InspectionResult(InspectionDecision::Retry, 'ForwardTimeout');
×
94
            case OperationResult::CommitTimeout:
95
                return new InspectionResult(InspectionDecision::Retry, 'CommitTimeout');
×
96
            case OperationResult::AccessDenied:
97
                $exception = new AccessDenied('Write access denied');
×
98
                $this->fail($exception);
×
99

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

106
    /**
107
     * @param TransactionWriteCompleted $response
108
     * @return null
109
     */
110
    protected function transformResponse(Message $response)
111
    {
112
        return null;
34✔
113
    }
114

115
    public function name(): string
116
    {
117
        return 'TransactionalWrite';
34✔
118
    }
119

120
    public function __toString(): string
121
    {
122
        return \sprintf(
34✔
123
            'TransactionId: %s, RequireMaster: %s',
34✔
124
            $this->transactionId,
34✔
125
            $this->requireMaster ? 'yes' : 'no'
34✔
126
        );
34✔
127
    }
128
}
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