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\User; |
22
|
|
|
|
23
|
|
|
use AmericanExpress\HyperledgerFabricClient\Config\ClientConfigInterface; |
24
|
|
|
use AmericanExpress\HyperledgerFabricClient\Exception\UnexpectedValueException; |
25
|
|
|
use AmericanExpress\HyperledgerFabricClient\Organization\OrganizationOptionsInterface; |
26
|
|
|
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\SerializedIdentityFactory; |
27
|
|
|
|
28
|
|
|
class UserContextFactory |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @param ClientConfigInterface $config |
32
|
|
|
* @param string|null $organization |
33
|
|
|
* @return UserContext |
34
|
|
|
* @throws UnexpectedValueException |
35
|
|
|
*/ |
36
|
4 |
|
public static function fromConfig( |
37
|
|
|
ClientConfigInterface $config, |
38
|
|
|
string $organization = null |
39
|
|
|
): UserContext { |
40
|
4 |
|
$organizationOptions = self::getOrganization($config, $organization); |
41
|
|
|
|
42
|
3 |
|
$identity = SerializedIdentityFactory::fromFile( |
43
|
3 |
|
$organizationOptions->getMspId(), |
44
|
3 |
|
new \SplFileObject($organizationOptions->getAdminCerts()) |
45
|
|
|
); |
46
|
|
|
|
47
|
3 |
|
return new UserContext($identity, $organizationOptions); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param ClientConfigInterface $config |
52
|
|
|
* @param string|null $organization |
53
|
|
|
* @return OrganizationOptionsInterface |
54
|
|
|
* @throws UnexpectedValueException |
55
|
|
|
*/ |
56
|
4 |
|
private static function getOrganization( |
57
|
|
|
ClientConfigInterface $config, |
58
|
|
|
string $organization = null |
59
|
|
|
): OrganizationOptionsInterface { |
60
|
4 |
|
if ($organization) { |
|
|
|
|
61
|
3 |
|
$options = $config->getOrganizationByName($organization); |
62
|
|
|
|
63
|
3 |
|
if ($options === null) { |
64
|
1 |
|
throw new UnexpectedValueException(sprintf( |
65
|
1 |
|
'Unable to load options for organization `%s`.', |
66
|
1 |
|
$organization |
67
|
|
|
)); |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
return $options; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
return $config->getDefaultOrganization(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: