• 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

89.36
/src/ClientOperations/ReadAllEventsBackwardOperation.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\AllEventsSlice;
19
use Prooph\EventStore\Exception\AccessDenied;
20
use Prooph\EventStore\Exception\ServerError;
21
use Prooph\EventStore\Position;
22
use Prooph\EventStore\ReadDirection;
23
use Prooph\EventStore\ResolvedEvent;
24
use Prooph\EventStore\UserCredentials;
25
use Prooph\EventStoreClient\Internal\EventMessageConverter;
26
use Prooph\EventStoreClient\Messages\ClientMessages\ReadAllEvents;
27
use Prooph\EventStoreClient\Messages\ClientMessages\ReadAllEventsCompleted;
28
use Prooph\EventStoreClient\Messages\ClientMessages\ReadAllEventsCompleted\ReadAllResult;
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<ReadAllEventsCompleted, AllEventsSlice>
37
 */
38
class ReadAllEventsBackwardOperation extends AbstractOperation
39
{
40
    public function __construct(
41
        Logger $logger,
42
        DeferredFuture $deferred,
43
        private readonly bool $requireMaster,
44
        private readonly Position $position,
45
        private readonly int $maxCount,
46
        private readonly bool $resolveLinkTos,
47
        ?UserCredentials $userCredentials
48
    ) {
49
        parent::__construct(
18✔
50
            $logger,
18✔
51
            $deferred,
18✔
52
            $userCredentials,
18✔
53
            TcpCommand::ReadAllEventsBackward,
18✔
54
            TcpCommand::ReadAllEventsBackwardCompleted,
18✔
55
            ReadAllEventsCompleted::class
18✔
56
        );
18✔
57
    }
58

59
    protected function createRequestDto(): Message
60
    {
61
        $message = new ReadAllEvents();
17✔
62
        $message->setRequireMaster($this->requireMaster);
17✔
63
        $message->setCommitPosition($this->position->commitPosition());
17✔
64
        $message->setPreparePosition($this->position->preparePosition());
17✔
65
        $message->setMaxCount($this->maxCount);
17✔
66
        $message->setResolveLinkTos($this->resolveLinkTos);
17✔
67

68
        return $message;
17✔
69
    }
70

71
    /**
72
     * @param ReadAllEventsCompleted $response
73
     * @return InspectionResult
74
     */
75
    protected function inspectResponse(Message $response): InspectionResult
76
    {
77
        switch ($response->getResult()) {
17✔
78
            case ReadAllResult::Success:
79
                $this->succeed($response);
17✔
80

81
                return new InspectionResult(InspectionDecision::EndOperation, 'Success');
17✔
82
            case ReadAllResult::Error:
83
                $this->fail(new ServerError($response->getError()));
×
84

85
                return new InspectionResult(InspectionDecision::EndOperation, 'Error');
×
86
            case
87
            ReadAllResult::AccessDenied:
88
                $this->fail(AccessDenied::toAllStream());
×
89

90
                return new InspectionResult(InspectionDecision::EndOperation, 'AccessDenied');
×
91
            default:
92
                throw new ServerError('Unexpected ReadAllResult');
×
93
        }
94
    }
95

96
    /**
97
     * @param ReadAllEventsCompleted $response
98
     * @return AllEventsSlice
99
     */
100
    protected function transformResponse(Message $response): AllEventsSlice
101
    {
102
        $resolvedEvents = [];
17✔
103

104
        foreach ($response->getEvents() as $record) {
17✔
105
            $resolvedEvents[] = new ResolvedEvent(
17✔
106
                EventMessageConverter::convertEventRecordMessageToEventRecord($record->getEvent()),
17✔
107
                EventMessageConverter::convertEventRecordMessageToEventRecord($record->getLink()),
17✔
108
                new Position(
17✔
109
                    $record->getCommitPosition(),
17✔
110
                    $record->getPreparePosition()
17✔
111
                )
17✔
112
            );
17✔
113
        }
114

115
        return new AllEventsSlice(
17✔
116
            ReadDirection::Backward,
17✔
117
            new Position((int) $response->getCommitPosition(), (int) $response->getPreparePosition()),
17✔
118
            new Position((int) $response->getNextCommitPosition(), (int) $response->getNextPreparePosition()),
17✔
119
            $resolvedEvents
17✔
120
        );
17✔
121
    }
122

123
    public function name(): string
124
    {
125
        return 'ReadAllEventsBackward';
17✔
126
    }
127

128
    public function __toString(): string
129
    {
130
        return \sprintf(
17✔
131
            'Position: %s, MaxCount: %d, ResolveLinkTos: %s, RequireMaster: %s',
17✔
132
            (string) $this->position,
17✔
133
            $this->maxCount,
17✔
134
            $this->resolveLinkTos ? 'yes' : 'no',
17✔
135
            $this->requireMaster ? 'yes' : 'no'
17✔
136
        );
17✔
137
    }
138
}
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