|
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\EndorserClient\EndorserClientManager; |
|
24
|
|
|
use AmericanExpress\HyperledgerFabricClient\EndorserClient\EndorserClientManagerInterface; |
|
25
|
|
|
use AmericanExpress\HyperledgerFabricClient\Exception\ExceptionInterface; |
|
26
|
|
|
use AmericanExpress\HyperledgerFabricClient\Exception\InvalidArgumentException; |
|
27
|
|
|
|
|
28
|
|
|
final class PeerFactory implements PeerFactoryInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var EndorserClientManagerInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $endorserClients; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* PeerFactory constructor. |
|
37
|
|
|
* @param EndorserClientManagerInterface $endorserClients |
|
38
|
|
|
*/ |
|
39
|
3 |
|
public function __construct(EndorserClientManagerInterface $endorserClients = null) |
|
40
|
|
|
{ |
|
41
|
3 |
|
$this->endorserClients = $endorserClients ?: new EndorserClientManager(); |
|
42
|
3 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param PeerOptionsInterface $options |
|
46
|
|
|
* @return PeerInterface |
|
47
|
|
|
*/ |
|
48
|
2 |
|
public function fromPeerOptions(PeerOptionsInterface $options): PeerInterface |
|
49
|
|
|
{ |
|
50
|
2 |
|
$endorserClient = $this->endorserClients->get($options->getRequests()); |
|
51
|
|
|
|
|
52
|
2 |
|
return new Peer($endorserClient); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param mixed[] $options |
|
57
|
|
|
* @return PeerInterface |
|
58
|
|
|
* @throws ExceptionInterface |
|
59
|
|
|
*/ |
|
60
|
2 |
|
public function fromArray(array $options): PeerInterface |
|
61
|
|
|
{ |
|
62
|
|
|
try { |
|
63
|
2 |
|
return $this->fromPeerOptions(new PeerOptions($options)); |
|
64
|
1 |
|
} catch (ExceptionInterface $e) { |
|
65
|
1 |
|
throw new InvalidArgumentException( |
|
66
|
1 |
|
sprintf('Unable to create Peer.'), |
|
67
|
1 |
|
0, |
|
68
|
1 |
|
$e |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|