-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathChildWorkflowProxy.php
More file actions
128 lines (107 loc) · 4.21 KB
/
ChildWorkflowProxy.php
File metadata and controls
128 lines (107 loc) · 4.21 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
<?php
/**
* This file is part of Temporal package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Temporal\Internal\Workflow;
use React\Promise\PromiseInterface;
use Temporal\DataConverter\Type;
use Temporal\Internal\Declaration\Prototype\WorkflowPrototype;
use Temporal\Internal\Support\Reflection;
use Temporal\Internal\Transport\CompletableResultInterface;
use Temporal\Workflow\ChildWorkflowOptions;
use Temporal\Workflow\ChildWorkflowStubInterface;
use Temporal\Workflow\WorkflowContextInterface;
/**
* @template-covariant T of object
* @mixin T
* @internal
*/
final class ChildWorkflowProxy extends Proxy
{
private const ERROR_UNDEFINED_WORKFLOW_METHOD =
'The given stub class "%s" does not contain a workflow method named "%s"';
private const ERROR_UNDEFINED_METHOD =
'The given stub class "%s" does not contain a workflow or signal method named "%s"';
private const ERROR_UNSUPPORTED_METHOD =
'The method named "%s" (%s) cannot be executed from a child workflow stub. ' .
'Only workflow and signal methods are allowed';
private ?ChildWorkflowStubInterface $stub = null;
/**
* @param class-string $class
*/
public function __construct(
private readonly string $class,
private readonly WorkflowPrototype $workflow,
private readonly ChildWorkflowOptions $options,
private readonly WorkflowContextInterface $context,
) {}
/**
* @param non-empty-string $method
* @return CompletableResultInterface
*/
public function __call(string $method, array $args): PromiseInterface
{
// If the proxy does not contain information about the running workflow,
// then we try to create a new stub from the workflow method and start
// the workflow.
if (!$this->isRunning()) {
$handler = $this->workflow->getHandler();
if ($method !== $handler?->getName()) {
throw new \BadMethodCallException(
\sprintf(self::ERROR_UNDEFINED_WORKFLOW_METHOD, $this->class, $method),
);
}
// Merge options with defaults defined using attributes:
// - #[MethodRetry]
// - #[CronSchedule]
$options = $this->options->mergeWith(
$this->workflow->getMethodRetry(),
$this->workflow->getCronSchedule(),
);
$this->stub = $this->context->newUntypedChildWorkflowStub(
$this->workflow->getID(),
$options,
);
if ($handler !== null) {
$args = Reflection::orderArguments($handler, $args);
}
return $this->stub->execute($args, $this->resolveReturnType($this->workflow));
}
// Otherwise, we try to find a suitable workflow "signal" method.
foreach ($this->workflow->getSignalHandlers() as $name => $definition) {
if ($definition->method->getName() === $method) {
$args = Reflection::orderArguments($definition->method, $args);
return $this->stub->signal($name, $args);
}
}
// Otherwise, we try to find a suitable workflow "query" method.
foreach ($this->workflow->getQueryHandlers() as $name => $definition) {
$definition->method->getName() === $method and throw new \BadMethodCallException(
\sprintf(self::ERROR_UNSUPPORTED_METHOD, $method, $name),
);
}
throw new \BadMethodCallException(
\sprintf(self::ERROR_UNDEFINED_METHOD, $this->class, $method),
);
}
private function resolveReturnType(WorkflowPrototype $prototype): ?Type
{
if ($attribute = $prototype->getReturnType()) {
return Type::create($attribute);
}
$handler = $prototype->getHandler();
return Type::create($handler?->getReturnType());
}
/**
* @psalm-assert-if-true ChildWorkflowStubInterface $this->stub
* @psalm-assert-if-false null $this->stub
*/
private function isRunning(): bool
{
return $this->stub !== null;
}
}