• 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

90.57
/src/ClientOperations/ReadStreamEventsBackwardOperation.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 ReadStreamEventsBackwardOperation 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(
48✔
51
            $logger,
48✔
52
            $deferred,
48✔
53
            $userCredentials,
48✔
54
            TcpCommand::ReadStreamEventsBackward,
48✔
55
            TcpCommand::ReadStreamEventsBackwardCompleted,
48✔
56
            ReadStreamEventsCompleted::class
48✔
57
        );
48✔
58
    }
59

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

69
        return $message;
47✔
70
    }
71

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

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

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

90
                return new InspectionResult(InspectionDecision::EndOperation, 'NoStream');
23✔
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 = [];
47✔
111

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

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

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

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