PeerFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 10
c 0
b 0
f 0
ccs 13
cts 13
cp 1
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromArray() 0 9 2
A __construct() 0 3 2
A fromPeerOptions() 0 5 1
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\Peer;
22
23
use AmericanExpress\HyperledgerFabricClient\EndorserClient\EndorserClientManager;
24
use AmericanExpress\HyperledgerFabricClient\EndorserClient\EndorserClientManagerInterface;
25
use AmericanExpress\HyperledgerFabricClient\Exception\ExceptionInterface;
26
use AmericanExpress\HyperledgerFabricClient\Exception\InvalidArgumentException;
27
28
final class PeerFactory implements PeerFactoryInterface
29
{
30
    /**
31
     * @var EndorserClientManagerInterface
32
     */
33
    private $endorserClients;
34
35
    /**
36
     * PeerFactory constructor.
37
     * @param EndorserClientManagerInterface $endorserClients
38
     */
39 3
    public function __construct(EndorserClientManagerInterface $endorserClients = null)
40
    {
41 3
        $this->endorserClients = $endorserClients ?: new EndorserClientManager();
42 3
    }
43
44
    /**
45
     * @param PeerOptionsInterface $options
46
     * @return PeerInterface
47
     */
48 2
    public function fromPeerOptions(PeerOptionsInterface $options): PeerInterface
49
    {
50 2
        $endorserClient = $this->endorserClients->get($options->getRequests());
51
52 2
        return new Peer($endorserClient);
53
    }
54
55
    /**
56
     * @param mixed[] $options
57
     * @return PeerInterface
58
     * @throws ExceptionInterface
59
     */
60 2
    public function fromArray(array $options): PeerInterface
61
    {
62
        try {
63 2
            return $this->fromPeerOptions(new PeerOptions($options));
64 1
        } catch (ExceptionInterface $e) {
65 1
            throw new InvalidArgumentException(
66 1
                sprintf('Unable to create Peer.'),
67 1
                0,
68 1
                $e
69
            );
70
        }
71
    }
72
}
73