ClientFactory::fromConfig()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 2
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
ccs 14
cts 14
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;
22
23
use AmericanExpress\HyperledgerFabricClient\Channel\ChannelFactory;
24
use AmericanExpress\HyperledgerFabricClient\Config\ClientConfigInterface;
25
use AmericanExpress\HyperledgerFabricClient\EndorserClient\EndorserClientManager;
26
use AmericanExpress\HyperledgerFabricClient\Exception\InvalidArgumentException;
27
use AmericanExpress\HyperledgerFabricClient\Exception\UnexpectedValueException;
28
use AmericanExpress\HyperledgerFabricClient\Header\HeaderGenerator;
29
use AmericanExpress\HyperledgerFabricClient\Nonce\RandomBytesNonceGenerator;
30
use AmericanExpress\HyperledgerFabricClient\Peer\PeerFactory;
31
use AmericanExpress\HyperledgerFabricClient\Signatory\MdanterEccSignatory;
32
use AmericanExpress\HyperledgerFabricClient\Transaction\TransactionIdentifierGenerator;
33
use AmericanExpress\HyperledgerFabricClient\User\UserContextFactory;
34
35
/**
36
 * Factory class that simplifies the creation of a `Client` instance
37
 *
38
 * #### Example Usage
39
 *
40
 * ```php
41
 * $config = ClientConfigFactory::fromFile(new \SplFileObject('config.php'));
42
 * $client = ClientFactory::fromConfig($config);
43
 * ```
44
 */
45
class ClientFactory
46
{
47
    /**
48
     *
49
     * Factory method that instantiates a `Client` object from an instance of `ClientConfigurationInterface`
50
     *
51
     * The `$organization` parameter can be optionally provided to select the desired organization, if the configuration
52
     * includes multiple organizations
53
     *
54
     * @param ClientConfigInterface $config
55
     * @param string|null $organization
56
     * @return Client
57
     * @throws InvalidArgumentException
58
     */
59 4
    public static function fromConfig(
60
        ClientConfigInterface $config,
61
        string $organization = null
62
    ): Client {
63
        try {
64 4
            $user = UserContextFactory::fromConfig($config, $organization);
65 2
        } catch (UnexpectedValueException $e) {
66 2
            throw new InvalidArgumentException('Could not create Client; invalid organization config');
67
        }
68
69 2
        $signatory = new MdanterEccSignatory($config->getHashAlgorithm());
70
71
        try {
72 2
            $nonceGenerator = new RandomBytesNonceGenerator($config->getNonceSize());
73 1
        } catch (InvalidArgumentException $e) {
74 1
            throw new InvalidArgumentException('Could not create Client; invalid nonce size', 0, $e);
75
        }
76
77 1
        $transactionContextFactory = new TransactionIdentifierGenerator($nonceGenerator, $config->getHashAlgorithm());
78
79 1
        $headerGenerator = new HeaderGenerator($transactionContextFactory, $config->getEpoch());
80
81 1
        $endorserClients = new EndorserClientManager();
82
83 1
        $peerFactory = new PeerFactory($endorserClients);
84
85 1
        $channelFactory = new ChannelFactory($headerGenerator, $peerFactory);
86
87 1
        return new Client($user, $signatory, $channelFactory);
88
    }
89
}
90