• 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

87.93
/src/ClientOperations/ReadStreamEventsForwardOperation.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\ServerError;
20
use Prooph\EventStore\ReadDirection;
21
use Prooph\EventStore\ResolvedEvent;
22
use Prooph\EventStore\SliceReadStatus;
23
use Prooph\EventStore\StreamEventsSlice;
24
use Prooph\EventStore\UserCredentials;
25
use Prooph\EventStoreClient\Internal\EventMessageConverter;
26
use Prooph\EventStoreClient\Messages\ClientMessages\ReadStreamEvents;
27
use Prooph\EventStoreClient\Messages\ClientMessages\ReadStreamEventsCompleted;
28
use Prooph\EventStoreClient\Messages\ClientMessages\ReadStreamEventsCompleted\ReadStreamResult;
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<ReadStreamEventsCompleted, StreamEventsSlice>
37
 */
38
class ReadStreamEventsForwardOperation 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 $fromEventNumber,
46
        private readonly int $maxCount,
47
        private readonly bool $resolveLinkTos,
48
        ?UserCredentials $userCredentials
49
    ) {
50
        parent::__construct(
97✔
51
            $logger,
97✔
52
            $deferred,
97✔
53
            $userCredentials,
97✔
54
            TcpCommand::ReadStreamEventsForward,
97✔
55
            TcpCommand::ReadStreamEventsForwardCompleted,
97✔
56
            ReadStreamEventsCompleted::class
97✔
57
        );
97✔
58
    }
59

60
    protected function createRequestDto(): Message
61
    {
62
        $message = new ReadStreamEvents();
96✔
63
        $message->setRequireMaster($this->requireMaster);
96✔
64
        $message->setEventStreamId($this->stream);
96✔
65
        $message->setFromEventNumber($this->fromEventNumber);
96✔
66
        $message->setMaxCount($this->maxCount);
96✔
67
        $message->setResolveLinkTos($this->resolveLinkTos);
96✔
68

69
        return $message;
96✔
70
    }
71

72
    /**
73
     * @param ReadStreamEventsCompleted $response
74
     * @return InspectionResult
75
     */
76
    protected function inspectResponse(Message $response): InspectionResult
77
    {
78
        switch ($response->getResult()) {
96✔
79
            case ReadStreamResult::Success:
96✔
80
                $this->succeed($response);
58✔
81

82
                return new InspectionResult(InspectionDecision::EndOperation, 'Success');
58✔
83
            case ReadStreamResult::StreamDeleted:
38✔
84
                $this->succeed($response);
3✔
85

86
                return new InspectionResult(InspectionDecision::EndOperation, 'StreamDeleted');
3✔
87
            case ReadStreamResult::NoStream:
35✔
88
                $this->succeed($response);
35✔
89

90
                return new InspectionResult(InspectionDecision::EndOperation, 'NoStream');
35✔
91
            case ReadStreamResult::Error:
×
92
                $this->fail(new ServerError($response->getError()));
×
93

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

98
                return new InspectionResult(InspectionDecision::EndOperation, 'AccessDenied');
×
99
            default:
100
                throw new ServerError('Unexpected ReadStreamResult');
×
101
        }
102
    }
103

104
    /**
105
     * @param ReadStreamEventsCompleted $response
106
     * @return StreamEventsSlice
107
     */
108
    protected function transformResponse(Message $response): StreamEventsSlice
109
    {
110
        $resolvedEvents = [];
96✔
111

112
        foreach ($response->getEvents() as $record) {
96✔
113
            $resolvedEvents[] = new ResolvedEvent(
56✔
114
                EventMessageConverter::convertEventRecordMessageToEventRecord($record->getEvent()),
56✔
115
                EventMessageConverter::convertEventRecordMessageToEventRecord($record->getLink()),
56✔
116
                null
56✔
117
            );
56✔
118
        }
119

120
        return new StreamEventsSlice(
96✔
121
            SliceReadStatus::from($response->getResult()),
96✔
122
            $this->stream,
96✔
123
            $this->fromEventNumber,
96✔
124
            ReadDirection::Forward,
96✔
125
            $resolvedEvents,
96✔
126
            (int) $response->getNextEventNumber(),
96✔
127
            (int) $response->getLastEventNumber(),
96✔
128
            $response->getIsEndOfStream()
96✔
129
        );
96✔
130
    }
131

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

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