|
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\Config; |
|
22
|
|
|
|
|
23
|
|
|
use AmericanExpress\HyperledgerFabricClient\Exception\BadMethodCallException; |
|
24
|
|
|
use AmericanExpress\HyperledgerFabricClient\Exception\InvalidArgumentException; |
|
25
|
|
|
use AmericanExpress\HyperledgerFabricClient\HashAlgorithm; |
|
26
|
|
|
use AmericanExpress\HyperledgerFabricClient\Organization\OrganizationOptions; |
|
27
|
|
|
use AmericanExpress\HyperledgerFabricClient\Organization\OrganizationOptionsInterface; |
|
28
|
|
|
use function igorw\get_in; |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
final class ClientConfig implements ClientConfigInterface |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
private $config; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* ClientConfig constructor. |
|
39
|
|
|
* @param array $config |
|
40
|
|
|
* @throws InvalidArgumentException |
|
41
|
|
|
*/ |
|
42
|
16 |
|
public function __construct(array $config) |
|
43
|
|
|
{ |
|
44
|
16 |
|
$this->config = array_merge( |
|
45
|
|
|
[ |
|
46
|
16 |
|
'timeout' => 5000, |
|
47
|
|
|
'epoch' => 0, |
|
48
|
|
|
'crypto-hash-algo' => 'sha256', |
|
49
|
|
|
'nonce-size' => 24, |
|
50
|
|
|
'organizations' => [], |
|
51
|
|
|
], |
|
52
|
16 |
|
$config |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
16 |
|
$hash = $this->getIn(['crypto-hash-algo']); |
|
56
|
|
|
|
|
57
|
16 |
|
if (!$hash instanceof HashAlgorithm) { |
|
58
|
|
|
try { |
|
59
|
16 |
|
$this->config['crypto-hash-algo'] = new HashAlgorithm((string) $hash); |
|
60
|
1 |
|
} catch (InvalidArgumentException $e) { |
|
61
|
1 |
|
throw new InvalidArgumentException( |
|
62
|
1 |
|
"Unable to create ClientConfig; Invalid 'crypto-hash-algo' supplied", |
|
63
|
1 |
|
$e->getCode(), |
|
64
|
1 |
|
$e |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
16 |
|
$this->config['organizations'] = array_map(function (array $data) { |
|
70
|
|
|
try { |
|
71
|
3 |
|
return new OrganizationOptions($data); |
|
72
|
1 |
|
} catch (BadMethodCallException $e) { |
|
73
|
1 |
|
throw new InvalidArgumentException('Cannot create ClientConfig; invalid organizations key', 0, $e); |
|
74
|
|
|
} |
|
75
|
16 |
|
}, $this->config['organizations']); |
|
76
|
16 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string[] $keys |
|
80
|
|
|
* @param mixed $default |
|
81
|
|
|
* @return mixed|null |
|
82
|
|
|
*/ |
|
83
|
16 |
|
public function getIn(array $keys = [], $default = null) |
|
84
|
|
|
{ |
|
85
|
16 |
|
return get_in($this->config, $keys, $default); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return int |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public function getNonceSize(): int |
|
92
|
|
|
{ |
|
93
|
2 |
|
return (int) $this->getIn(['nonce-size']); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return HashAlgorithm |
|
98
|
|
|
*/ |
|
99
|
2 |
|
public function getHashAlgorithm(): HashAlgorithm |
|
100
|
|
|
{ |
|
101
|
2 |
|
return $this->getIn(['crypto-hash-algo']); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return int |
|
106
|
|
|
*/ |
|
107
|
2 |
|
public function getTimeout(): int |
|
108
|
|
|
{ |
|
109
|
2 |
|
return (int) $this->getIn(['timeout']); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return int |
|
114
|
|
|
*/ |
|
115
|
2 |
|
public function getEpoch(): int |
|
116
|
|
|
{ |
|
117
|
2 |
|
return (int) $this->getIn(['epoch']); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param string $name |
|
122
|
|
|
* @return OrganizationOptionsInterface|null |
|
123
|
|
|
* @throws \AmericanExpress\HyperledgerFabricClient\Exception\BadMethodCallException |
|
124
|
|
|
*/ |
|
125
|
2 |
|
public function getOrganizationByName(string $name): ?OrganizationOptionsInterface |
|
126
|
|
|
{ |
|
127
|
2 |
|
$organizations = array_filter( |
|
128
|
2 |
|
$this->getIn(['organizations'], []), |
|
129
|
2 |
|
function (OrganizationOptionsInterface $organization) use ($name) { |
|
130
|
1 |
|
return $organization->getName() === $name; |
|
131
|
2 |
|
} |
|
132
|
|
|
); |
|
133
|
|
|
|
|
134
|
2 |
|
return \count($organizations) > 0 ? \reset($organizations) : null; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return OrganizationOptionsInterface |
|
139
|
|
|
*/ |
|
140
|
1 |
|
public function getDefaultOrganization(): OrganizationOptionsInterface |
|
141
|
|
|
{ |
|
142
|
1 |
|
$organizations = $this->getIn(['organizations']); |
|
143
|
|
|
|
|
144
|
1 |
|
return \reset($organizations); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|