Repository navigation
Expand file tree
/
Copy pathGateway.php
More file actions
792 lines (665 loc) · 27.5 KB
/
Copy pathGateway.php
File metadata and controls
792 lines (665 loc) · 27.5 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
<?php
namespace Payum\Core;
use Exception;
use Payum\Core\Action\ActionInterface;
use Payum\Core\Command\CommandInterface;
use Payum\Core\Command\NotifyCommand;
use Payum\Core\Event\CommandDispatched;
use Payum\Core\Event\CommandFailed;
use Payum\Core\Event\FailureRaised;
use Payum\Core\Event\HandlerResolved;
use Payum\Core\Event\NullEventDispatcher;
use Payum\Core\Event\ResultReturned;
use Payum\Core\Event\WebhookReceived;
use Payum\Core\Exception\CommandNotSupportedException;
use Payum\Core\Exception\LogicException;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\Exception\UnsupportedApiException;
use Payum\Core\Extension\Context;
use Payum\Core\Extension\ExtensionCollection;
use Payum\Core\Extension\ExtensionInterface;
use Payum\Core\Gateway\GatewayInterface as PaymentGateway;
use Payum\Core\Handler\Context as HandlerContext;
use Payum\Core\Handler\HandlerInterface;
use Payum\Core\Handler\HandlerMap;
use Payum\Core\Handler\NotifyHandlerInterface;
use Payum\Core\Legacy\ActionToHandlerAdapter;
use Payum\Core\Legacy\HandlerToActionAdapter;
use Payum\Core\Legacy\LegacyFactoryScope;
use Payum\Core\Legacy\RequestToCommand;
use Payum\Core\Legacy\ResultToReply;
use Payum\Core\Legacy\StatusMarker;
use Payum\Core\Legacy\UndescribedGateway;
use Payum\Core\Middleware\MiddlewareCollection;
use Payum\Core\Middleware\Pipeline;
use Payum\Core\Model\PaymentStatuses;
use Payum\Core\Model\SubjectInterface;
use Payum\Core\Registry\StorageRegistryInterface;
use Payum\Core\Reply\ReplyInterface;
use Payum\Core\Request\Generic;
use Payum\Core\Request\GetStatusInterface;
use Payum\Core\Result\Failure;
use Payum\Core\Result\NextAction;
use Payum\Core\Result\NextAction\RenderTemplate;
use Payum\Core\Result\Result;
use Payum\Core\Security\TokenIssuerInterface;
use Payum\Core\Storage\IdentityInterface;
use Payum\Core\Template\RendererInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\ServerRequestInterface;
use ReflectionClass;
use ReflectionProperty;
use Throwable;
use function func_num_args;
use function get_debug_type;
use function is_object;
use function preg_match;
use function preg_replace;
use function sprintf;
use function trigger_deprecation;
class Gateway implements GatewayInterface
{
/**
* @var list<class-string<Action\ActionInterface>|Action\ActionInterface>
*/
protected array $actions = [];
/**
* @var mixed[]
* @deprecated since 2.0.0, will be removed in 3.0. Take the api as a constructor argument on the
* action instead, and let the container inject it.
*/
protected $apis = [];
/**
* @var ExtensionCollection
*/
protected $extensions;
/**
* @var Context[]
*/
protected array $stack = [];
protected ContainerInterface $container;
protected ?HandlerMap $handlerMap = null;
protected ?Pipeline $pipeline = null;
protected ?EventDispatcherInterface $events = null;
/**
* The name this gateway is registered under. Set by the builder; null when built by hand.
*/
protected ?string $name = null;
/**
* @var list<HandlerContext>
*/
protected array $commandStack = [];
/**
* What has already been reported as 1.x, keyed by request class or by argument name. A gateway
* dispatches the same request on every payment, so reporting per call would bury the notice in
* repetitions of itself; the thing to change is the request, and there is one of those per class.
*
* @var array<string, true>
*/
private static array $reportedLegacyUse = [];
public function __construct()
{
$this->extensions = new ExtensionCollection();
}
/**
* @param mixed $api
* @param bool $forcePrepend
*/
public function addApi($api, $forcePrepend = false): void
{
// A 1.x factory's payum.api.* keys are reported by key while core registers them.
if (! LegacyFactoryScope::isActive()) {
trigger_deprecation(
'payum/core',
'2.0.0',
'The %s method is deprecated and will be removed in 3.0. Take the api as a constructor argument on the action instead, and let the container inject it.',
__METHOD__
);
}
$forcePrepend ?
array_unshift($this->apis, $api) :
array_push($this->apis, $api)
;
}
/**
* @param bool $forcePrepend
*/
public function addAction(ActionInterface $action, $forcePrepend = false): void
{
// Registration happens once per action, and handing the api over happens on every execute().
if ($action instanceof ApiAwareInterface) {
trigger_deprecation(
'payum/core',
'2.0.0',
'Implementing %s in %s is deprecated and will be removed in 3.0. Take the api as a constructor argument instead, and let the container inject it.',
ApiAwareInterface::class,
$action::class,
);
}
$forcePrepend ?
array_unshift($this->actions, $action) :
array_push($this->actions, $action)
;
}
/**
* @param bool $forcePrepend
*/
public function addExtension(ExtensionInterface $extension, $forcePrepend = false): void
{
$this->extensions->addExtension($extension, $forcePrepend);
}
public function getExtensions(): ExtensionCollection
{
return $this->extensions;
}
public function execute(/* CommandInterface */ $request, $catchReply = false)
{
if (func_num_args() > 1 && ! isset(self::$reportedLegacyUse['$catchReply'])) {
self::$reportedLegacyUse['$catchReply'] = true;
trigger_deprecation(
'payum/core',
'2.0.0',
'Passing the $catchReply argument to %s is deprecated and will be removed in 3.0. Dispatch a %s and act on the %s it returns instead.',
__METHOD__,
CommandInterface::class,
Result::class,
);
}
if ($request instanceof CommandInterface) {
return $this->dispatch($request);
}
$requestType = get_debug_type($request);
if (! isset(self::$reportedLegacyUse[$requestType])) {
self::$reportedLegacyUse[$requestType] = true;
trigger_deprecation(
'payum/core',
'2.0.0',
'Passing a %s to %s is deprecated and will not be supported in 3.0. %s',
$requestType,
__METHOD__,
(is_object($request) ? self::replacementFor($request::class) : null)
?? sprintf('Port the action that dispatches it to a %s.', HandlerInterface::class),
);
}
// A 1.x request is answered by the handler that means the same thing, when the gateway has one.
//
// Handlers are asked first on purpose. Core's own actions claim requests broadly -- a token is a
// DetailsAggregateInterface, so ExecuteSameRequestWithModelDetailsAction supports Capture($token)
// -- and asking actions first would swallow every request before a handler ever saw it. Asking
// handlers first is also what a gateway part-way through moving needs: what it has ported goes to
// a handler, and the rest falls through to the actions it still has.
if ($this->handlerMap instanceof HandlerMap) {
if ($request instanceof GetStatusInterface) {
// A gateway still holding a status action reads the details of whatever has not moved,
// which is more than the recorded status knows. Only answer from the record when nothing
// else claims it. An identity is not unclaimed yet: StorageExtension loads it on the
// action path, and the request comes back here carrying the model.
$unloaded = $request->getModel() instanceof IdentityInterface && [] !== $this->actions;
if (! $unloaded && ! $this->findActionSupported($request)) {
$this->answerStatus($request);
return null;
}
} elseif (RequestToCommand::supports($request)) {
// A request on a token becomes a command on that token, so the payment is not loaded here,
// ahead of the lock.
$command = RequestToCommand::translate($request);
// A handler asking for the 1.x request of the command it is answering wants the actions:
// that is how an action answers a command through ActionToHandlerAdapter.
if ($command instanceof CommandInterface && $this->supportsCommand($command::class) && ! $this->isDispatching($command)) {
try {
return $this->replyFor($this->dispatch($command), $catchReply);
} finally {
$this->persistForLegacyCaller($command);
}
}
}
}
$context = new Context($this, $request, $this->stack);
$this->stack[] = $context;
try {
$this->extensions->onPreExecute($context);
if (! $context->getAction()) {
if (! $action = $this->findActionSupported($context->getRequest())) {
throw RequestNotSupportedException::create($context->getRequest());
}
$context->setAction($action);
}
$this->extensions->onExecute($context);
$context->getAction()->execute($request);
$this->extensions->onPostExecute($context);
array_pop($this->stack);
} catch (ReplyInterface $reply) {
$this->reportLegacyReply($reply, $context->getAction());
$context->setReply($reply);
$this->extensions->onPostExecute($context);
array_pop($this->stack);
if ($catchReply && $context->getReply()) {
return $context->getReply();
}
if ($context->getReply()) {
throw $context->getReply();
}
} catch (Exception $e) {
$context->setException($e);
$this->onPostExecuteWithException($context);
}
return null;
}
public function setContainer(ContainerInterface $container): self
{
$this->container = $container;
return $this;
}
/**
* Whether this gateway has a handler for the given command.
*
* @param class-string<CommandInterface<Result>> $commandClass
*/
public function supportsCommand(string $commandClass): bool
{
return null !== $this->handlerMap?->serviceIdFor($commandClass);
}
/**
* Whether any registered action claims the request, answered without executing anything.
*
* What lets {@see \Payum\Core\Legacy\LegacyGatewayAdapter} report what a gateway built from actions
* can do. Handlers are not consulted: {@see self::supportsCommand()} is that question.
*
* @deprecated since 2.0, removed in 3.0 along with actions.
*/
public function supportsRequest(object $request): bool
{
return false !== $this->findActionSupported($request);
}
/**
* Runs a handler this gateway does not list for a 1.x request, and throws the reply the 1.x caller is
* waiting to catch. What lets a gateway built by a 1.x factory carry a handler.
*
* @param CommandInterface<Result> $command
*
* @internal for {@see \Payum\Core\Legacy\HandlerToActionAdapter}
*
* @deprecated since 2.0, removed in 3.0 along with actions.
*/
public function executeHandler(HandlerInterface $handler, CommandInterface $command, ?SubjectInterface $subject = null): void
{
$this->replyFor($this->dispatch($command, $handler, $subject), false);
}
/**
* Opens a 1.x extension context for a command on the stack the action path uses, so an extension sees
* the same nesting whichever path a request took. StorageExtension relies on it to write once, when the
* outermost call returns.
*
* @internal for {@see \Payum\Core\Middleware\LegacyExtensionMiddleware}
*/
public function openExtensionContext(object $request): Context
{
return $this->stack[] = new Context($this, $request, $this->stack);
}
/**
* @internal for {@see \Payum\Core\Middleware\LegacyExtensionMiddleware}
*/
public function closeExtensionContext(): void
{
array_pop($this->stack);
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setHandlerMap(HandlerMap $handlerMap): self
{
$this->handlerMap = $handlerMap;
return $this;
}
protected function onPostExecuteWithException(Context $context): void
{
array_pop($this->stack);
$exception = $context->getException();
try {
$this->extensions->onPostExecute($context);
} catch (Exception $e) {
// logic is similar to one in Symfony's ExceptionListener::onKernelException
$wrapper = $e;
while (($prev = $wrapper->getPrevious()) instanceof Throwable) {
if ($exception === $wrapper = $prev) {
throw $e;
}
}
$prev = new ReflectionProperty('Exception', 'previous');
$prev->setValue($wrapper, $exception);
throw $e;
}
if ($context->getException()) {
throw $context->getException();
}
}
/**
* @param mixed $request
*
* @return ActionInterface|false
*/
protected function findActionSupported($request)
{
foreach ($this->actions as $action) {
if ($action instanceof GatewayAwareInterface) {
$action->setGateway($this);
}
if ($action instanceof ApiAwareInterface) {
$apiSet = false;
$unsupportedException = null;
foreach ($this->apis as $api) {
try {
$action->setApi($api);
$apiSet = true;
break;
} catch (UnsupportedApiException $e) {
$unsupportedException = $e;
}
}
if (! $apiSet) {
throw new LogicException(sprintf('Cannot find right api for the action %s', $action::class), 0, $unsupportedException);
}
}
if ($action->supports($request)) {
return $action;
}
}
return false;
}
/**
* Reported where an action throws the reply rather than where it is loaded: core builds replies of its
* own for a 1.x caller, and an application cannot act on a notice about those.
*/
private function reportLegacyReply(ReplyInterface $reply, ?ActionInterface $action): void
{
$key = 'reply:' . $reply::class;
if ($action instanceof HandlerToActionAdapter || isset(self::$reportedLegacyUse[$key])) {
return;
}
self::$reportedLegacyUse[$key] = true;
trigger_deprecation(
'payum/core',
'2.0.0',
'Throwing a %s from %s is deprecated and will not be supported in 3.0. %s',
$reply::class,
null === $action ? 'an extension' : $action::class,
self::replacementFor($reply::class)
?? sprintf('A handler returns a %s carrying a %s instead.', Result::class, NextAction::class),
);
}
/**
* What the `@deprecated` on the class, or on the nearest parent that has one, says to use instead. The
* docblock is the one place each replacement is written down, and DeprecationMessagesTest keeps it
* pointing at classes that exist.
*
* @param class-string $class
*/
private static function replacementFor(string $class): ?string
{
for ($reflection = new ReflectionClass($class); false !== $reflection; $reflection = $reflection->getParentClass()) {
if (preg_match('#@deprecated\s+since\s.*?\bin 3\.0\.\s+(.+?)\s*(?:\*/|\n\s*\*\s*(?:@|\n))#s', (string) $reflection->getDocComment(), $matches)) {
return (string) preg_replace('#\s*\n\s*\*\s*#', ' ', $matches[1]);
}
}
return null;
}
/**
* @param CommandInterface<Result> $command
* @param HandlerInterface|null $handler one the handler map does not name, run in its place
* @param SubjectInterface|null $subject what the command is about, when the caller has already loaded it
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function dispatch(CommandInterface $command, ?HandlerInterface $handler = null, ?SubjectInterface $subject = null): Result
{
$serviceId = $handler ?? $this->handlerMap?->serviceIdFor($command::class);
if (null === $serviceId) {
throw CommandNotSupportedException::create(
$command,
$this->name,
$this->container->has(PaymentGateway::class) ? $this->container->get(PaymentGateway::class) : null,
$this->handlerMap?->commands() ?? [],
);
}
$context = $this->buildContext($command, $subject);
$events = $this->events();
$events->dispatch(new CommandDispatched($command, $context));
$this->commandStack[] = $context;
try {
$result = $this->pipeline()->process(
$command,
$context,
fn (CommandInterface $c, HandlerContext $ctx): Result => $this->handle($serviceId, $c, $ctx),
);
} catch (Throwable $exception) {
$events->dispatch(new CommandFailed($command, $context, $exception));
throw $exception;
} finally {
array_pop($this->commandStack);
}
$events->dispatch(new ResultReturned($command, $context, $result));
if ($result->failure instanceof Failure) {
$events->dispatch(new FailureRaised($command, $context, $result->failure, $result));
}
return $result;
}
/**
* @param class-string<HandlerInterface>|HandlerInterface $serviceId
* @param CommandInterface<Result> $command
*/
private function handle(string|HandlerInterface $serviceId, CommandInterface $command, HandlerContext $context): Result
{
$handler = $serviceId instanceof HandlerInterface ? $serviceId : $this->container->get($serviceId);
if (! $handler instanceof HandlerInterface || ! method_exists($handler, 'handle')) {
throw new LogicException(sprintf('%s must be a handler declaring handle().', get_debug_type($handler)));
}
if ($handler instanceof GatewayAwareInterface) {
$handler->setGateway($this);
}
$this->events()->dispatch(new HandlerResolved($command, $context, $handler));
if ($handler instanceof NotifyHandlerInterface) {
if (! $command instanceof NotifyCommand) {
throw new LogicException(sprintf(
'%s handles %s, but %s was dispatched.',
$handler::class,
NotifyCommand::class,
$command::class,
));
}
// Verification runs here rather than during dispatch so that middleware wraps it: a message
// that fails the check is something an application will want to see.
$webhook = $handler->verify($context->httpRequest());
$this->events()->dispatch(new WebhookReceived($command, $context, $webhook));
$result = $handler->handle($command, $webhook, $context);
} else {
$result = $handler->handle($command, $context);
}
if (! $result instanceof Result) {
throw new LogicException(sprintf('%s::handle() must return a %s.', $handler::class, Result::class));
}
return $result;
}
/**
* The application's PSR-14 dispatcher, or one that throws events away.
*
* Resolved rather than injected because a gateway built by hand has no container to resolve from, and
* a no-op is what keeps every call site free of a null check.
*/
private function events(): EventDispatcherInterface
{
if ($this->events instanceof EventDispatcherInterface) {
return $this->events;
}
return $this->events = $this->container->has(EventDispatcherInterface::class)
? $this->container->get(EventDispatcherInterface::class)
: new NullEventDispatcher();
}
/**
* Built once and reused. A sub-command dispatched from a handler comes back through the same pipeline,
* which is what makes nesting work.
*/
private function pipeline(): Pipeline
{
if ($this->pipeline instanceof Pipeline) {
return $this->pipeline;
}
$collection = $this->container->has(MiddlewareCollection::class)
? $this->container->get(MiddlewareCollection::class)
: new MiddlewareCollection();
$middleware = $collection->resolve($this->container);
foreach ($middleware as $one) {
if ($one instanceof GatewayAwareInterface) {
$one->setGateway($this);
}
}
return $this->pipeline = new Pipeline($middleware);
}
/**
* @param CommandInterface<Result> $command
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function buildContext(CommandInterface $command, ?SubjectInterface $subject = null): HandlerContext
{
return new HandlerContext(
$this,
$command,
$this->container->has(PaymentGateway::class)
? $this->container->get(PaymentGateway::class)
: new UndescribedGateway($this->name),
$this->container->get(ServerRequestInterface::class),
$this->container->get(TokenIssuerInterface::class),
// Loaded on first use rather than here, which is before LockingMiddleware runs: a payment read
// before the lock is taken can be one another execution is about to change.
$subject ?? fn (): ?SubjectInterface => $this->resolveSubject($command),
$command->token(),
$this->commandStack,
$this->name,
);
}
/**
* Tells a 1.x caller what a handler decided, the way it expects to hear it.
*
* Throws the matching reply when the customer has somewhere to be, or returns it when the caller
* asked to catch it. A result with nothing left to do answers with null, the same as a 1.x action
* returning without throwing.
*/
private function replyFor(Result $result, bool $catchReply): ?ReplyInterface
{
$reply = ResultToReply::translate(
$result,
// Resolved only for a result that names a template, so a gateway built without a renderer
// keeps working.
$result->next instanceof RenderTemplate ? $this->container->get(RendererInterface::class) : null,
);
// Every reply is an exception; the interface just cannot say so.
if (! $reply instanceof Throwable) {
return null;
}
if ($catchReply) {
return $reply;
}
throw $reply;
}
/**
* Whether a command meaning the same thing, about the same subject, is already being answered.
*
* @param CommandInterface<Result> $command
*/
private function isDispatching(CommandInterface $command): bool
{
foreach ($this->commandStack as $context) {
$running = $context->command();
if ($running::class === $command::class && $running->token() === $command->token() && $running->subject() === $command->subject()) {
return true;
}
}
return false;
}
/**
* A gateway built from handlers has no status action. It has something better: the status recorded
* after every command, which is what this reads.
*
* A subject that tracks no status leaves the request marked unknown, since nobody knows.
*/
private function answerStatus(GetStatusInterface $request): void
{
$subject = $this->resolveSubjectOf($request);
if ($subject instanceof SubjectInterface) {
// 1.x actions do this, and it is what makes getFirstModel() return the payment rather than
// the token it arrived on -- which Payum::done() relies on.
$request->setModel($subject);
}
StatusMarker::mark($request, $subject instanceof SubjectInterface ? PaymentStatuses::of($subject) : null);
}
/**
* A 1.x caller that handed over a stored payment expects it written back, as StorageExtension did.
* Arriving on a token, PersistStateMiddleware has already written it.
*
* @param CommandInterface<Result> $command
*/
private function persistForLegacyCaller(CommandInterface $command): void
{
$subject = $command->subject();
if (null !== $command->token() || ! $subject instanceof SubjectInterface || ! $this->container->has(StorageRegistryInterface::class)) {
return;
}
// Answered by actions, so StorageExtension has written it back already, as it did on 1.x.
if (is_a($this->handlerMap?->bindings()[$this->handlerMap->serviceIdFor($command::class)] ?? '', ActionToHandlerAdapter::class, true)) {
return;
}
foreach ($this->container->get(StorageRegistryInterface::class)->getStorages() as $storage) {
if ($storage->support($subject)) {
$storage->update($subject);
}
}
}
/**
* The subject a 1.x request is about, from the model it carries or the token it arrived on.
*/
private function resolveSubjectOf(object $request): ?SubjectInterface
{
if (! $request instanceof Generic) {
return null;
}
if (($model = $request->getFirstModel()) instanceof SubjectInterface) {
return $model;
}
$identity = $request->getToken()?->getDetails();
if (null === $identity || ! $this->container->has(StorageRegistryInterface::class)) {
return null;
}
$model = $this->container->get(StorageRegistryInterface::class)
->getStorage($identity->getClass())
->find($identity);
return $model instanceof SubjectInterface ? $model : null;
}
/**
* @param CommandInterface<Result> $command
*/
private function resolveSubject(CommandInterface $command): ?SubjectInterface
{
if (($subject = $command->subject()) instanceof SubjectInterface) {
return $subject;
}
$identity = $command->token()?->getDetails();
if (null === $identity || ! $this->container->has(StorageRegistryInterface::class)) {
return null;
}
$model = $this->container->get(StorageRegistryInterface::class)
->getStorage($identity->getClass())
->find($identity);
return $model instanceof SubjectInterface ? $model : null;
}
}