|
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\Channel; |
|
22
|
|
|
|
|
23
|
|
|
use AmericanExpress\HyperledgerFabricClient\Exception\ExceptionInterface; |
|
24
|
|
|
use AmericanExpress\HyperledgerFabricClient\Exception\InvalidArgumentException; |
|
25
|
|
|
use AmericanExpress\HyperledgerFabricClient\Header\HeaderGeneratorInterface; |
|
26
|
|
|
use AmericanExpress\HyperledgerFabricClient\Identity\SerializedIdentityAwareHeaderGenerator; |
|
27
|
|
|
use AmericanExpress\HyperledgerFabricClient\Identity\SerializedIdentityAwareHeaderGeneratorInterface; |
|
28
|
|
|
use AmericanExpress\HyperledgerFabricClient\Peer\PeerFactory; |
|
29
|
|
|
use AmericanExpress\HyperledgerFabricClient\Peer\PeerFactoryInterface; |
|
30
|
|
|
use AmericanExpress\HyperledgerFabricClient\Peer\PeerInterface; |
|
31
|
|
|
use AmericanExpress\HyperledgerFabricClient\Peer\PeerOptionsInterface; |
|
32
|
|
|
use AmericanExpress\HyperledgerFabricClient\Proposal\ProposalProcessorInterface; |
|
33
|
|
|
use AmericanExpress\HyperledgerFabricClient\User\UserContextInterface; |
|
34
|
|
|
use Hyperledger\Fabric\Protos\MSP\SerializedIdentity; |
|
35
|
|
|
|
|
36
|
|
|
final class ChannelFactory implements ChannelFactoryInterface |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* @var HeaderGeneratorInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $headerGenerator; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var PeerFactoryInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
private $peerFactory; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* ChannelFactory constructor. |
|
50
|
|
|
* @param HeaderGeneratorInterface $headerGenerator |
|
51
|
|
|
* @param PeerFactoryInterface|null $peerFactory |
|
52
|
|
|
*/ |
|
53
|
2 |
|
public function __construct(HeaderGeneratorInterface $headerGenerator, PeerFactoryInterface $peerFactory = null) |
|
54
|
|
|
{ |
|
55
|
2 |
|
$this->headerGenerator = $headerGenerator; |
|
56
|
2 |
|
$this->peerFactory = $peerFactory ?: new PeerFactory(); |
|
57
|
2 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $name |
|
61
|
|
|
* @param ProposalProcessorInterface $proposalProcessor |
|
62
|
|
|
* @param UserContextInterface $user |
|
63
|
|
|
* @return ChannelInterface |
|
64
|
|
|
* @throws ExceptionInterface |
|
65
|
|
|
*/ |
|
66
|
2 |
|
public function create( |
|
67
|
|
|
string $name, |
|
68
|
|
|
ProposalProcessorInterface $proposalProcessor, |
|
69
|
|
|
UserContextInterface $user |
|
70
|
|
|
): ChannelInterface { |
|
71
|
2 |
|
$headerGenerator = $this->createSerializedIdentityAwareHeaderGenerator($user->getIdentity()); |
|
72
|
|
|
|
|
73
|
2 |
|
$peers = $this->getPeers($user); |
|
74
|
|
|
|
|
75
|
2 |
|
return new Channel($name, $proposalProcessor, $headerGenerator, $peers); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param UserContextInterface $user |
|
80
|
|
|
* @return PeerInterface[] |
|
81
|
|
|
*/ |
|
82
|
|
|
private function getPeers(UserContextInterface $user): array |
|
83
|
|
|
{ |
|
84
|
2 |
|
return \array_map(function (PeerOptionsInterface $options) { |
|
85
|
1 |
|
return $this->peerFactory->fromPeerOptions($options); |
|
86
|
2 |
|
}, $user->getOrganization()->getPeers()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param SerializedIdentity $identity |
|
91
|
|
|
* @return SerializedIdentityAwareHeaderGeneratorInterface |
|
92
|
|
|
*/ |
|
93
|
2 |
|
private function createSerializedIdentityAwareHeaderGenerator( |
|
94
|
|
|
SerializedIdentity $identity |
|
95
|
|
|
): SerializedIdentityAwareHeaderGeneratorInterface { |
|
96
|
2 |
|
return new SerializedIdentityAwareHeaderGenerator($identity, $this->headerGenerator); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|