HeaderGenerator::generateSignatureHeader()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 4
cp 1
crap 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\Header;
22
23
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\HeaderFactory;
24
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\SignatureHeaderFactory;
25
use AmericanExpress\HyperledgerFabricClient\Transaction\TransactionIdentifier;
26
use AmericanExpress\HyperledgerFabricClient\Transaction\TransactionIdentifierGeneratorInterface;
27
use Hyperledger\Fabric\Protos\Common\ChannelHeader;
28
use Hyperledger\Fabric\Protos\Common\Header;
29
use Hyperledger\Fabric\Protos\Common\SignatureHeader;
30
use Hyperledger\Fabric\Protos\MSP\SerializedIdentity;
31
32
final class HeaderGenerator implements HeaderGeneratorInterface
33
{
34
    /**
35
     * @var TransactionIdentifierGeneratorInterface
36
     */
37
    private $transactionIdentifierGenerator;
38
39
    /**
40
     * @var int
41
     */
42
    private $epoch;
43
44
    /**
45
     * HeaderGenerator constructor.
46
     * @param TransactionIdentifierGeneratorInterface $transactionIdentifierGenerator
47
     * @param int $epoch
48
     */
49 5
    public function __construct(TransactionIdentifierGeneratorInterface $transactionIdentifierGenerator, $epoch = 0)
50
    {
51 5
        $this->transactionIdentifierGenerator = $transactionIdentifierGenerator;
52 5
        $this->epoch = $epoch;
53 5
    }
54
55
    /**
56
     * @param SerializedIdentity $identity
57
     * @param TransactionIdentifier $transactionIdentifier
58
     * @return SignatureHeader
59
     */
60 5
    private function generateSignatureHeader(
61
        SerializedIdentity $identity,
62
        TransactionIdentifier $transactionIdentifier
63
    ): SignatureHeader {
64 5
        return SignatureHeaderFactory::create(
65 5
            $identity,
66 5
            $transactionIdentifier->getNonce()
67
        );
68
    }
69
70
    /**
71
     * @param SerializedIdentity $identity
72
     * @param ChannelHeader $channelHeader
73
     * @return Header
74
     */
75 5
    public function fromChannelHeader(SerializedIdentity $identity, ChannelHeader $channelHeader): Header
76
    {
77 5
        $transactionId = $this->transactionIdentifierGenerator->fromSerializedIdentity($identity);
78 5
        $channelHeader->setTxId($transactionId->getId());
79 5
        $channelHeader->setEpoch($this->epoch);
80 5
        return HeaderFactory::create($this->generateSignatureHeader($identity, $transactionId), $channelHeader);
81
    }
82
}
83