This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathInvoker.php
126 lines (106 loc) · 3.47 KB
/
Invoker.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
<?php
/**
* This file is part of RoadRunner GRPC 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 Spiral\GRPC;
use Google\Protobuf\Internal\Message;
use Spiral\GRPC\Exception\GRPCExceptionInterface;
use Spiral\GRPC\Exception\InvokeException;
final class Invoker implements InvokerInterface
{
/**
* @var string
*/
private const ERROR_METHOD_RETURN =
'Method %s must return an object that instance of %s, ' .
'but the result provides type of %s';
/**
* @var string
*/
private const ERROR_METHOD_IN_TYPE =
'Method %s input type must be an instance of %s, ' .
'but the input is type of %s'
;
/**
* {@inheritDoc}
*/
public function invoke(ServiceInterface $service, Method $method, ContextInterface $ctx, ?string $input): string
{
/** @var callable $callable */
$callable = [$service, $method->getName()];
/** @var Message $message */
$message = $callable($ctx, $this->makeInput($method, $input));
// Note: This validation will only work if the
// assertions option ("zend.assertions") is enabled.
assert($this->assertResultType($method, $message));
try {
return $message->serializeToString();
} catch (\Throwable $e) {
throw InvokeException::create($e->getMessage(), StatusCode::INTERNAL, $e);
}
}
/**
* Checks that the result from the GRPC service method returns the
* Message object.
*
* @param Method $method
* @param mixed $result
* @return bool
* @throws \BadFunctionCallException
*/
private function assertResultType(Method $method, $result): bool
{
if (! $result instanceof Message) {
$type = \is_object($result) ? \get_class($result) : \get_debug_type($result);
throw new \BadFunctionCallException(
\sprintf(self::ERROR_METHOD_RETURN, $method->getName(), Message::class, $type)
);
}
return true;
}
/**
* @param Method $method
* @param string|null $body
* @return Message
* @throws InvokeException
*/
private function makeInput(Method $method, ?string $body): Message
{
try {
$class = $method->getInputType();
// Note: This validation will only work if the
// assertions option ("zend.assertions") is enabled.
assert($this->assertInputType($method, $class));
/** @psalm-suppress UnsafeInstantiation */
$in = new $class();
if ($body !== null) {
$in->mergeFromString($body);
}
return $in;
} catch (\Throwable $e) {
throw InvokeException::create($e->getMessage(), StatusCode::INTERNAL, $e);
}
}
/**
* Checks that the input of the GRPC service method contains the
* Message object.
*
* @param Method $method
* @param string $class
* @return bool
* @throws \InvalidArgumentException
*/
private function assertInputType(Method $method, string $class): bool
{
if (! \is_subclass_of($class, Message::class)) {
throw new \InvalidArgumentException(
\sprintf(self::ERROR_METHOD_IN_TYPE, $method->getName(), Message::class, $class)
);
}
return true;
}
}