• 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

94.64
/src/ClientOperations/ReadEventOperation.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\EventReadResult;
19
use Prooph\EventStore\EventReadStatus;
20
use Prooph\EventStore\Exception\AccessDenied;
21
use Prooph\EventStore\Exception\ServerError;
22
use Prooph\EventStore\ResolvedEvent;
23
use Prooph\EventStore\UserCredentials;
24
use Prooph\EventStoreClient\Internal\EventMessageConverter;
25
use Prooph\EventStoreClient\Messages\ClientMessages\ReadEvent;
26
use Prooph\EventStoreClient\Messages\ClientMessages\ReadEventCompleted;
27
use Prooph\EventStoreClient\Messages\ClientMessages\ReadEventCompleted\ReadEventResult;
28
use Prooph\EventStoreClient\SystemData\InspectionDecision;
29
use Prooph\EventStoreClient\SystemData\InspectionResult;
30
use Prooph\EventStoreClient\SystemData\TcpCommand;
31
use Psr\Log\LoggerInterface as Logger;
32

33
/**
34
 * @internal
35
 * @extends AbstractOperation<ReadEventCompleted, EventReadResult>
36
 */
37
class ReadEventOperation extends AbstractOperation
38
{
39
    public function __construct(
40
        Logger $logger,
41
        DeferredFuture $deferred,
42
        private readonly bool $requireMaster,
43
        private readonly string $stream,
44
        private readonly int $eventNumber,
45
        private readonly bool $resolveLinkTos,
46
        ?UserCredentials $userCredentials
47
    ) {
48
        parent::__construct(
96✔
49
            $logger,
96✔
50
            $deferred,
96✔
51
            $userCredentials,
96✔
52
            TcpCommand::ReadEvent,
96✔
53
            TcpCommand::ReadEventCompleted,
96✔
54
            ReadEventCompleted::class
96✔
55
        );
96✔
56
    }
57

58
    protected function createRequestDto(): Message
59
    {
60
        $message = new ReadEvent();
96✔
61
        $message->setEventStreamId($this->stream);
96✔
62
        $message->setEventNumber($this->eventNumber);
96✔
63
        $message->setResolveLinkTos($this->resolveLinkTos);
96✔
64
        $message->setRequireMaster($this->requireMaster);
96✔
65

66
        return $message;
96✔
67
    }
68

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

79
                return new InspectionResult(InspectionDecision::EndOperation, 'Success');
61✔
80
            case ReadEventResult::NotFound:
48✔
81
                $this->succeed($response);
6✔
82

83
                return new InspectionResult(InspectionDecision::EndOperation, 'NotFound');
6✔
84
            case ReadEventResult::NoStream:
42✔
85
                $this->succeed($response);
25✔
86

87
                return new InspectionResult(InspectionDecision::EndOperation, 'NoStream');
25✔
88
            case ReadEventResult::StreamDeleted:
17✔
89
                $this->succeed($response);
4✔
90

91
                return new InspectionResult(InspectionDecision::EndOperation, 'StreamDeleted');
4✔
92
            case ReadEventResult::Error:
13✔
93
                $this->fail(new ServerError($response->getError()));
×
94

95
                return new InspectionResult(InspectionDecision::EndOperation, 'Error');
×
96
            case ReadEventResult::AccessDenied:
13✔
97
                $this->fail(AccessDenied::toStream($this->stream));
13✔
98

99
                return new InspectionResult(InspectionDecision::EndOperation, 'AccessDenied');
13✔
100
            default:
101
                throw new ServerError('Unexpected ReadEventResult');
×
102
        }
103
    }
104

105
    /**
106
     * @param ReadEventCompleted $response
107
     * @return EventReadResult
108
     */
109
    protected function transformResponse(Message $response): EventReadResult
110
    {
111
        $eventMessage = $response->getEvent();
78✔
112

113
        if (EventReadStatus::Success->value === $response->getResult()) {
78✔
114
            /** @psalm-suppress PossiblyInvalidArgument */
115
            $resolvedEvent = new ResolvedEvent(
61✔
116
                EventMessageConverter::convertEventRecordMessageToEventRecord($eventMessage->getEvent()),
61✔
117
                EventMessageConverter::convertEventRecordMessageToEventRecord($eventMessage->getLink()),
61✔
118
                null
61✔
119
            );
61✔
120
        } else {
121
            $resolvedEvent = null;
35✔
122
        }
123

124
        return new EventReadResult(
78✔
125
            EventReadStatus::from($response->getResult()),
78✔
126
            $this->stream,
78✔
127
            $this->eventNumber,
78✔
128
            $resolvedEvent
78✔
129
        );
78✔
130
    }
131

132
    public function name(): string
133
    {
134
        return 'ReadEvent';
96✔
135
    }
136

137
    public function __toString(): string
138
    {
139
        return \sprintf(
96✔
140
            'Stream: %s, EventNumber: %d, ResolveLinkTo: %s, RequireMaster: %s',
96✔
141
            $this->stream,
96✔
142
            $this->eventNumber,
96✔
143
            $this->resolveLinkTos ? 'yes' : 'no',
96✔
144
            $this->requireMaster ? 'yes' : 'no'
96✔
145
        );
96✔
146
    }
147
}
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