Skip to content

Commit 5249117

Browse files
author
Stepan Zolotarev
committed
CS fix
1 parent 9b4c376 commit 5249117

33 files changed

+199
-361
lines changed

src/Configuration/Attributes/SagaAttributeBasedConfigurationLoader.php

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use ServiceBus\Sagas\Configuration\Metadata\SagaHandlerOptions;
2525
use ServiceBus\Sagas\Configuration\Metadata\SagaMetadata;
2626
use ServiceBus\Sagas\Configuration\SagaConfigurationLoader;
27+
2728
use function ServiceBus\Sagas\createEventListenerName;
2829

2930
final class SagaAttributeBasedConfigurationLoader implements SagaConfigurationLoader
@@ -48,8 +49,7 @@ public function __construct(
4849

4950
public function load(string $sagaClass): SagaConfiguration
5051
{
51-
try
52-
{
52+
try {
5353
$attributes = $this->attributesReader->extract($sagaClass);
5454

5555
$sagaHeader = self::searchSagaHeader(
@@ -77,9 +77,7 @@ classLevelAttributes: $attributes->classLevelCollection
7777
sagaMetadata: $sagaMetadata
7878
)
7979
);
80-
}
81-
catch (\Throwable $throwable)
82-
{
80+
} catch (\Throwable $throwable) {
8381
throw InvalidSagaConfiguration::fromThrowable($throwable);
8482
}
8583
}
@@ -101,10 +99,8 @@ private function collectSagaEventHandlers(
10199
$handlersCollection = new \SplObjectStorage();
102100

103101
/** @var MethodLevel $methodLevelAttribute */
104-
foreach ($methodLevelAttributes as $methodLevelAttribute)
105-
{
106-
if ($methodLevelAttribute->attribute instanceof SagaEventListener)
107-
{
102+
foreach ($methodLevelAttributes as $methodLevelAttribute) {
103+
if ($methodLevelAttribute->attribute instanceof SagaEventListener) {
108104
$handlersCollection->attach(
109105
$this->createMessageHandler(
110106
methodLevelAttribute: $methodLevelAttribute,
@@ -147,17 +143,14 @@ private function createMessageHandler(
147143

148144
$messageClass = $this->extractMessageClass($reflectionMethod);
149145

150-
$expectedMethodName = match ($handlerType)
151-
{
146+
$expectedMethodName = match ($handlerType) {
152147
SagaMessageHandlerType::INITIAL_COMMAND_HANDLER => self::INITIAL_COMMAND_METHOD,
153148
SagaMessageHandlerType::EVENT_LISTENER => createEventListenerName($messageClass)
154149
};
155150

156-
if ($expectedMethodName === $reflectionMethod->name)
157-
{
151+
if ($expectedMethodName === $reflectionMethod->name) {
158152
/** @var callable $processor */
159-
$processor = match ($handlerType)
160-
{
153+
$processor = match ($handlerType) {
161154
SagaMessageHandlerType::INITIAL_COMMAND_HANDLER => $this->eventListenerProcessorFactory->createHandler(
162155
command: $messageClass,
163156
handlerOptions: $options
@@ -198,16 +191,14 @@ private function extractMessageClass(\ReflectionMethod $reflectionMethod): strin
198191
? $reflectionParameters[0]->getType()
199192
: null;
200193

201-
if ($firstArgumentType !== null)
202-
{
194+
if ($firstArgumentType !== null) {
203195
/** @var \ReflectionNamedType $reflectionType */
204196
$reflectionType = $reflectionParameters[0]->getType();
205197

206198
/** @psalm-var class-string $messageClass */
207199
$messageClass = $reflectionType->getName();
208200

209-
if (\class_exists($messageClass))
210-
{
201+
if (\class_exists($messageClass)) {
211202
return $messageClass;
212203
}
213204
}
@@ -224,8 +215,7 @@ private function extractMessageClass(\ReflectionMethod $reflectionMethod): strin
224215
*/
225216
private static function createSagaMetadata(string $sagaClass, SagaHeader $sagaHeader): SagaMetadata
226217
{
227-
if (\class_exists($sagaHeader->idClass) === false)
228-
{
218+
if (\class_exists($sagaHeader->idClass) === false) {
229219
throw new \InvalidArgumentException(
230220
\sprintf(
231221
'In the metadata of the saga "%s" an incorrect value of the "idClass"',
@@ -258,16 +248,14 @@ private function findInitialCommandHandlerAttribute(
258248
/** @var MethodLevel[] $commandHandlersAttributes */
259249
$commandHandlersAttributes = \array_filter(
260250
\array_map(
261-
static function (MethodLevel $attribute): ?MethodLevel
262-
{
251+
static function (MethodLevel $attribute): ?MethodLevel {
263252
return $attribute->attribute instanceof SagaInitialHandler ? $attribute : null;
264253
},
265254
\iterator_to_array($methodLevelAttributes)
266255
)
267256
);
268257

269-
if (\count($commandHandlersAttributes) === 1)
270-
{
258+
if (\count($commandHandlersAttributes) === 1) {
271259
return \end($commandHandlersAttributes);
272260
}
273261

@@ -287,12 +275,10 @@ static function (MethodLevel $attribute): ?MethodLevel
287275
private static function searchSagaHeader(string $sagaClass, \SplObjectStorage $classLevelAttributes): SagaHeader
288276
{
289277
/** @var ClassLevel $attributes */
290-
foreach ($classLevelAttributes as $attributes)
291-
{
278+
foreach ($classLevelAttributes as $attributes) {
292279
$attributeObject = $attributes->attribute;
293280

294-
if ($attributeObject instanceof SagaHeader)
295-
{
281+
if ($attributeObject instanceof SagaHeader) {
296282
return $attributeObject;
297283
}
298284
}

src/Configuration/MessageProcessor/DefaultEventListenerProcessor.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use ServiceBus\Sagas\Saga;
2424
use ServiceBus\Sagas\SagaId;
2525
use ServiceBus\Sagas\Store\SagasStore;
26+
2627
use function Amp\call;
2728
use function ServiceBus\Common\invokeReflectionMethod;
2829
use function ServiceBus\Common\now;
@@ -95,8 +96,7 @@ public function message(): string
9596
public function __invoke(object $message, ServiceBusContext $context): Promise
9697
{
9798
return call(
98-
function () use ($message, $context): \Generator
99-
{
99+
function () use ($message, $context): \Generator {
100100
/** @psalm-var SagaId $id */
101101
$id = yield $this->sagaIdLocator->process(
102102
handlerOptions: $this->sagaListenerOptions,
@@ -107,17 +107,15 @@ function () use ($message, $context): \Generator
107107
/** @phpstan-ignore-next-line */
108108
yield $this->mutexService->withLock(
109109
id: createMutexKey($id),
110-
code: function () use ($id, $message, $context): \Generator
111-
{
110+
code: function () use ($id, $message, $context): \Generator {
112111
/** @var \ServiceBus\Sagas\Saga $saga */
113112
$saga = yield from $this->loadSaga($id);
114113

115114
$stateHash = $saga->hash();
116115

117116
$description = $this->sagaListenerOptions->description();
118117

119-
if ($description !== null)
120-
{
118+
if ($description !== null) {
121119
$context->logger()->debug($description);
122120
}
123121

@@ -131,17 +129,15 @@ function () use ($message, $context): \Generator
131129

132130
yield call($messageHandler->closure, ...$resolvedArgs);
133131

134-
if ($stateHash !== $saga->hash())
135-
{
132+
if ($stateHash !== $saga->hash()) {
136133
/**
137134
* @var object[] $messages
138135
*/
139136
$messages = invokeReflectionMethod($saga, 'messages');
140137

141138
yield $this->sagasStore->update(
142139
saga: $saga,
143-
publisher: static function () use ($messages, $context): \Generator
144-
{
140+
publisher: static function () use ($messages, $context): \Generator {
145141
yield $context->deliveryBulk($messages);
146142
}
147143
);
@@ -162,8 +158,7 @@ private function loadSaga(SagaId $id): \Generator
162158
/** @var \ServiceBus\Sagas\Saga|null $saga */
163159
$saga = yield $this->sagasStore->obtain($id);
164160

165-
if ($saga === null)
166-
{
161+
if ($saga === null) {
167162
throw ChangeSagaStateFailed::applyEventFailed(
168163
\sprintf(
169164
'Attempt to apply event to non-existent saga (ID: %s)',
@@ -173,8 +168,7 @@ private function loadSaga(SagaId $id): \Generator
173168
}
174169

175170
/** Non-expired saga */
176-
if ($saga->expireDate() > now())
177-
{
171+
if ($saga->expireDate() > now()) {
178172
return $saga;
179173
}
180174

@@ -188,8 +182,7 @@ private function loadSaga(SagaId $id): \Generator
188182
*/
189183
private function buildMessageHandler(Saga $saga, object $event): MessageHandler
190184
{
191-
try
192-
{
185+
try {
193186
$reflectionMethod = new \ReflectionMethod($saga, createEventListenerName($event));
194187

195188
return new MessageHandler(
@@ -199,9 +192,7 @@ private function buildMessageHandler(Saga $saga, object $event): MessageHandler
199192
options: $this->sagaListenerOptions,
200193
description: $this->sagaListenerOptions->description()
201194
);
202-
}
203-
catch (\Throwable $throwable)
204-
{
195+
} catch (\Throwable $throwable) {
205196
throw new \RuntimeException(
206197
\sprintf(
207198
'Unable to compile message handler for `%s`: %s',

src/Configuration/MessageProcessor/DefaultInitialCommandHandlerMessageProcessor.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use ServiceBus\Sagas\SagaId;
1818
use ServiceBus\Sagas\SagaMetadataStore;
1919
use ServiceBus\Sagas\Store\SagasStore;
20+
2021
use function Amp\call;
2122
use function ServiceBus\Common\datetimeInstantiator;
2223
use function ServiceBus\Common\invokeReflectionMethod;
@@ -88,8 +89,7 @@ public function message(): string
8889
public function __invoke(object $message, ServiceBusContext $context): Promise
8990
{
9091
return call(
91-
function () use ($message, $context): \Generator
92-
{
92+
function () use ($message, $context): \Generator {
9393
/** @psalm-var SagaId $id */
9494
$id = yield $this->sagaIdLocator->process(
9595
handlerOptions: $this->sagaListenerOptions,
@@ -100,8 +100,7 @@ function () use ($message, $context): \Generator
100100
/** @phpstan-ignore-next-line */
101101
yield $this->mutexService->withLock(
102102
id: createMutexKey($id),
103-
code: function () use ($id, $message, $context): \Generator
104-
{
103+
code: function () use ($id, $message, $context): \Generator {
105104
$sagaMetaData = SagaMetadataStore::instance()->get($id->sagaClass)
106105
?? throw SagaMetaDataNotFound::create($id->sagaClass);
107106

@@ -113,8 +112,7 @@ function () use ($message, $context): \Generator
113112

114113
$description = $this->sagaListenerOptions->description();
115114

116-
if ($description !== null)
117-
{
115+
if ($description !== null) {
118116
$context->logger()->debug($description);
119117
}
120118

@@ -133,8 +131,7 @@ function () use ($message, $context): \Generator
133131

134132
yield $this->sagasStore->save(
135133
saga: $saga,
136-
publisher: static function () use ($messages, $context): \Generator
137-
{
134+
publisher: static function () use ($messages, $context): \Generator {
138135
yield $context->deliveryBulk($messages);
139136
}
140137
);
@@ -149,8 +146,7 @@ function () use ($message, $context): \Generator
149146
*/
150147
private function buildMessageHandler(Saga $saga, object $command): MessageHandler
151148
{
152-
try
153-
{
149+
try {
154150
$reflectionMethod = new \ReflectionMethod($saga, SagaConfigurationLoader::INITIAL_COMMAND_METHOD);
155151

156152
return new MessageHandler(
@@ -160,9 +156,7 @@ private function buildMessageHandler(Saga $saga, object $command): MessageHandle
160156
options: $this->sagaListenerOptions,
161157
description: $this->sagaListenerOptions->description()
162158
);
163-
}
164-
catch (\Throwable $throwable)
165-
{
159+
} catch (\Throwable $throwable) {
166160
throw new \RuntimeException(
167161
\sprintf(
168162
'Unable to compile message handler for `%s`: %s',

src/Configuration/Metadata/SagaHandlerOptions.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ public function containingIdentifierProperty(): string
106106
{
107107
$containingIdentifierProperty = $this->containingIdentifierProperty;
108108

109-
if ($containingIdentifierProperty !== null && $containingIdentifierProperty !== '')
110-
{
109+
if ($containingIdentifierProperty !== null && $containingIdentifierProperty !== '') {
111110
return $containingIdentifierProperty;
112111
}
113112

@@ -121,8 +120,7 @@ public function containingIdentifierSource(): string
121120
{
122121
$containingIdentifierSource = $this->containingIdentifierSource;
123122

124-
if ($containingIdentifierSource !== null && $containingIdentifierSource !== '')
125-
{
123+
if ($containingIdentifierSource !== null && $containingIdentifierSource !== '') {
126124
return $containingIdentifierSource;
127125
}
128126

src/Configuration/Metadata/SagaMetadata.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ public function __construct(
9898
string $containingIdentifierProperty,
9999
string $expireDateModifier
100100
) {
101-
if (\in_array($containingIdentifierSource, self::CORRELATION_ID_SOURCES, true) === false)
102-
{
101+
if (\in_array($containingIdentifierSource, self::CORRELATION_ID_SOURCES, true) === false) {
103102
throw new \InvalidArgumentException(
104103
\sprintf(
105104
'In the metadata of the saga "%s" an incorrect value of the "containingIdentifierSource" (can be `message` or `headers` only)',

src/Configuration/SagaIdLocator.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use ServiceBus\Sagas\Exceptions\InvalidSagaIdentifier;
1111
use ServiceBus\Sagas\SagaId;
1212
use ServiceBus\Sagas\Store\SagasStore;
13+
1314
use function Amp\call;
1415
use function ServiceBus\Common\readReflectionPropertyValue;
1516

@@ -39,11 +40,9 @@ public function process(
3940
array $headers
4041
): Promise {
4142
return call(
42-
function () use ($handlerOptions, $message, $headers): \Generator
43-
{
43+
function () use ($handlerOptions, $message, $headers): \Generator {
4444
$propertyName = $handlerOptions->containingIdentifierProperty();
45-
$propertyValue = match ($handlerOptions->containingIdentifierSource())
46-
{
45+
$propertyValue = match ($handlerOptions->containingIdentifierSource()) {
4746
SagaMetadata::CORRELATION_ID_SOURCE_HEADERS => !empty($headers[$propertyName])
4847
? (string) $headers[$propertyName]
4948
: throw InvalidSagaIdentifier::headerKeyCantBeEmpty($propertyName),
@@ -82,27 +81,21 @@ function () use ($handlerOptions, $message, $headers): \Generator
8281
*/
8382
private function readMessagePropertyValue(object $message, string $propertyName): string
8483
{
85-
try
86-
{
84+
try {
8785
/** @psalm-var object|string|int|float $value */
8886
$value = $message->{$propertyName} ?? readReflectionPropertyValue($message, $propertyName);
89-
}
90-
catch (\Throwable)
91-
{
87+
} catch (\Throwable) {
9288
throw InvalidSagaIdentifier::propertyNotFound($propertyName, $message);
9389
}
9490

95-
if (\is_string($value) && $value !== '')
96-
{
91+
if (\is_string($value) && $value !== '') {
9792
return $value;
9893
}
9994

100-
if (\is_object($value) && \method_exists($value, 'toString'))
101-
{
95+
if (\is_object($value) && \method_exists($value, 'toString')) {
10296
$value = (string) $value->toString();
10397

104-
if ($value !== '')
105-
{
98+
if ($value !== '') {
10699
return $value;
107100
}
108101
}
@@ -126,8 +119,7 @@ private function identifierInstantiator(
126119
/** @var object|SagaId $identifier */
127120
$identifier = new $idClass($idValue, $sagaClass);
128121

129-
if ($identifier instanceof SagaId)
130-
{
122+
if ($identifier instanceof SagaId) {
131123
return $identifier;
132124
}
133125

src/Configuration/functions.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ function createClosure(Saga $saga, \ReflectionMethod $method): \Closure
2626

2727
/** @noinspection PhpConditionAlreadyCheckedInspection */
2828
// @codeCoverageIgnoreStart
29-
if ($closure === null)
30-
{
29+
if ($closure === null) {
3130
throw new \LogicException(
3231
\sprintf(
3332
'Unable to create a closure for the "%s" method',

0 commit comments

Comments
 (0)