-
Notifications
You must be signed in to change notification settings - Fork 31
/
CreatePersistentSubscriptionOperation.php
146 lines (129 loc) · 5.8 KB
/
CreatePersistentSubscriptionOperation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* This file is part of `prooph/event-store-client`.
* (c) 2018-2020 Alexander Miertsch <kontakt@codeliner.ws>
* (c) 2018-2020 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Prooph\EventStoreClient\ClientOperations;
use Amp\Deferred;
use Google\Protobuf\Internal\Message;
use Prooph\EventStore\Common\SystemConsumerStrategies;
use Prooph\EventStore\Exception\AccessDenied;
use Prooph\EventStore\Exception\InvalidOperationException;
use Prooph\EventStore\Exception\UnexpectedOperationResult;
use Prooph\EventStore\PersistentSubscriptionCreateResult;
use Prooph\EventStore\PersistentSubscriptionCreateStatus;
use Prooph\EventStore\PersistentSubscriptionSettings;
use Prooph\EventStore\UserCredentials;
use Prooph\EventStoreClient\Messages\ClientMessages\CreatePersistentSubscription;
use Prooph\EventStoreClient\Messages\ClientMessages\CreatePersistentSubscriptionCompleted;
use Prooph\EventStoreClient\Messages\ClientMessages\CreatePersistentSubscriptionCompleted\CreatePersistentSubscriptionResult;
use Prooph\EventStoreClient\SystemData\InspectionDecision;
use Prooph\EventStoreClient\SystemData\InspectionResult;
use Prooph\EventStoreClient\SystemData\TcpCommand;
use Psr\Log\LoggerInterface as Logger;
/**
* @internal
* @extends AbstractOperation<CreatePersistentSubscriptionCompleted, PersistentSubscriptionCreateResult>
*/
class CreatePersistentSubscriptionOperation extends AbstractOperation
{
private string $stream;
private string $groupName;
private PersistentSubscriptionSettings $settings;
public function __construct(
Logger $logger,
Deferred $deferred,
string $stream,
string $groupNameName,
PersistentSubscriptionSettings $settings,
?UserCredentials $userCredentials
) {
$this->stream = $stream;
$this->groupName = $groupNameName;
$this->settings = $settings;
parent::__construct(
$logger,
$deferred,
$userCredentials,
TcpCommand::createPersistentSubscription(),
TcpCommand::createPersistentSubscriptionCompleted(),
CreatePersistentSubscriptionCompleted::class
);
}
protected function createRequestDto(): Message
{
$message = new CreatePersistentSubscription();
$message->setSubscriptionGroupName($this->groupName);
$message->setEventStreamId($this->stream);
$message->setResolveLinkTos($this->settings->resolveLinkTos());
$message->setStartFrom($this->settings->startFrom());
$message->setMessageTimeoutMilliseconds($this->settings->messageTimeoutMilliseconds());
$message->setRecordStatistics($this->settings->extraStatistics());
$message->setLiveBufferSize($this->settings->liveBufferSize());
$message->setReadBatchSize($this->settings->readBatchSize());
$message->setBufferSize($this->settings->bufferSize());
$message->setMaxRetryCount($this->settings->maxRetryCount());
$message->setPreferRoundRobin($this->settings->namedConsumerStrategy() === SystemConsumerStrategies::ROUND_ROBIN);
$message->setCheckpointAfterTime($this->settings->checkPointAfterMilliseconds());
$message->setCheckpointMaxCount($this->settings->maxCheckPointCount());
$message->setCheckpointMinCount($this->settings->minCheckPointCount());
$message->setSubscriberMaxCount($this->settings->maxSubscriberCount());
$message->setNamedConsumerStrategy($this->settings->namedConsumerStrategy());
return $message;
}
/**
* @param CreatePersistentSubscriptionCompleted $response
* @return InspectionResult
*/
protected function inspectResponse(Message $response): InspectionResult
{
switch ($response->getResult()) {
case CreatePersistentSubscriptionResult::Success:
$this->succeed($response);
return new InspectionResult(InspectionDecision::endOperation(), 'Success');
case CreatePersistentSubscriptionResult::Fail:
$this->fail(new InvalidOperationException(\sprintf(
'Subscription group \'%s\' on stream \'%s\' failed \'%s\'',
$this->groupName,
$this->stream,
$response->getReason()
)));
return new InspectionResult(InspectionDecision::endOperation(), 'Fail');
case CreatePersistentSubscriptionResult::AccessDenied:
$this->fail(AccessDenied::toStream($this->stream));
return new InspectionResult(InspectionDecision::endOperation(), 'AccessDenied');
case CreatePersistentSubscriptionResult::AlreadyExists:
$this->fail(new InvalidOperationException(\sprintf(
'Subscription group \'%s\' on stream \'%s\' already exists',
$this->groupName,
$this->stream
)));
return new InspectionResult(InspectionDecision::endOperation(), 'AlreadyExists');
default:
throw new UnexpectedOperationResult();
}
}
/**
* @param CreatePersistentSubscriptionCompleted $response
* @return PersistentSubscriptionCreateResult
*/
protected function transformResponse(Message $response): PersistentSubscriptionCreateResult
{
return new PersistentSubscriptionCreateResult(
PersistentSubscriptionCreateStatus::success()
);
}
public function name(): string
{
return 'CreatePersistentSubscription';
}
public function __toString(): string
{
return \sprintf('Stream: %s, Group Name: %s', $this->stream, $this->groupName);
}
}