UserContextFactory::getOrganization()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
ccs 9
cts 9
cp 1
crap 3
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $organization of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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