1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2017 American Express Travel Related Services Company, Inc. |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
15
|
|
|
* or implied. See the License for the specific language governing |
16
|
|
|
* permissions and limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
declare(strict_types=1); |
20
|
|
|
|
21
|
|
|
namespace AmericanExpress\HyperledgerFabricClient\Peer; |
22
|
|
|
|
23
|
|
|
use AmericanExpress\HyperledgerFabricClient\Exception\RuntimeException; |
24
|
|
|
use AmericanExpress\HyperledgerFabricClient\Proposal\Response; |
25
|
|
|
use AmericanExpress\HyperledgerFabricClient\Proposal\ResponseCollection; |
26
|
|
|
use Grpc\UnaryCall; |
27
|
|
|
use Hyperledger\Fabric\Protos\Peer\ProposalResponse; |
28
|
|
|
use function igorw\get_in; |
|
|
|
|
29
|
|
|
|
30
|
|
|
final class UnaryCallResolver implements UnaryCallResolverInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @param UnaryCall $call |
34
|
|
|
* @return Response |
35
|
|
|
*/ |
36
|
5 |
|
public function resolveOne(UnaryCall $call): Response |
37
|
|
|
{ |
38
|
5 |
|
[$proposalResponse, $status] = $call->wait(); |
39
|
|
|
|
40
|
5 |
|
if ($proposalResponse instanceof ProposalResponse) { |
41
|
3 |
|
return Response::fromProposalResponse($proposalResponse); |
42
|
|
|
} |
43
|
|
|
|
44
|
3 |
|
$status = (array) $status; |
45
|
3 |
|
return Response::fromException( |
46
|
3 |
|
new RuntimeException(get_in($status, ['details']), get_in($status, ['code'])) |
|
|
|
|
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param UnaryCall|UnaryCall[] ...$calls |
52
|
|
|
* @return ResponseCollection |
53
|
|
|
*/ |
54
|
3 |
|
public function resolveMany(UnaryCall ...$calls): ResponseCollection |
55
|
|
|
{ |
56
|
3 |
|
$peer = $this; |
57
|
|
|
|
58
|
3 |
|
return new ResponseCollection(\array_map(function (UnaryCall $call) use ($peer) { |
59
|
3 |
|
return $peer->resolveOne($call); |
60
|
3 |
|
}, $calls)); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|