AbstractChaincodeTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 110
Duplicated Lines 12.73 %

Importance

Changes 0
Metric Value
dl 14
loc 110
rs 10
c 0
b 0
f 0
wmc 7

12 Methods

Rating   Name   Duplication   Size   Complexity  
createChaincodeProposal() 0 27 ?
getPrivateKeyFile() 0 3 ?
B hp$0 ➔ createChaincodeProposal() 0 27 1
A setUp() 0 15 1
A hp$0 ➔ generateNonce() 0 3 1
A hp$0 ➔ loadStaticData() 0 11 2
A hp$0 ➔ createMockTransactionIdentifierGenerator() 0 7 1
A hp$0 ➔ getPrivateKeyFile() 0 3 1
A hp$0 ➔ getChainCodeProposalDataset() 12 12 1
createMockTransactionIdentifierGenerator() 0 7 ?
loadStaticData() 0 11 ?
getChainCodeProposalDataset() 12 12 ?

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 AmericanExpressTest\HyperledgerFabricClient\Chaincode;
22
23
use AmericanExpress\HyperledgerFabricClient\Nonce\NonceGeneratorInterface;
24
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\ChaincodeHeaderExtensionFactory;
25
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\ChaincodeIdFactory;
26
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\ChaincodeProposalPayloadFactory;
27
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\ChannelHeaderFactory;
28
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\HeaderFactory;
29
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\ProposalFactory;
30
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\SerializedIdentityFactory;
31
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\SignatureHeaderFactory;
32
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\TimestampFactory;
33
use AmericanExpress\HyperledgerFabricClient\Transaction\TransactionIdentifierGenerator;
34
use Hyperledger\Fabric\Protos\Peer\Proposal;
35
use org\bovigo\vfs\vfsStream;
36
use org\bovigo\vfs\vfsStreamFile;
37
use PHPUnit\Framework\TestCase;
38
39
abstract class AbstractChaincodeTest extends TestCase
40
{
41
    /**
42
     * @var vfsStreamFile
43
     */
44
    protected $privateKey;
45
46
    /**
47
     * @var \SplFileObject
48
     */
49
    protected $privateKeyFile;
50
51
    protected function setUp()
52
    {
53
        $files = vfsStream::setup('test');
54
55
        $this->privateKey = vfsStream::newFile('foo');
56
        $this->privateKey->setContent(<<<'TAG'
57
-----BEGIN PRIVATE KEY-----
58
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQghnA7rdgbZi/wndus
59
iXjyf0KgE6OKZjQ+5INjwelRAC6hRANCAASb3u+hY+U/FZvhYDN6d08HJ1v56UJU
60
yz/n2NHyJgTg6kC05AaJMeGIinEF0JeJtRDNVQGzoQJQYjnzUTS9FvGh
61
-----END PRIVATE KEY-----
62
TAG
63
        );
64
        $files->addChild($this->privateKey);
65
        $this->privateKeyFile = new \SplFileObject($this->privateKey->url());
66
    }
67
68
    private function createMockTransactionIdentifierGenerator(): TransactionIdentifierGenerator
69
    {
70
        return new TransactionIdentifierGenerator(
71
            new class implements NonceGeneratorInterface {
72
                public function generateNonce(): string
73
                {
74
                    return 'u23m5k4hf86j';
75
                }
76
            }
77
        );
78
    }
79
80
    protected function createChaincodeProposal(string $dateTime, \SplFileObject $privateKeyFile): Proposal
81
    {
82
        $transactionContextFactory = $this->createMockTransactionIdentifierGenerator();
83
        $identity = SerializedIdentityFactory::fromFile('1234', $privateKeyFile);
84
        $transactionContext = $transactionContextFactory->fromSerializedIdentity($identity);
85
86
        $channelHeader = ChannelHeaderFactory::create('MyChannelId');
87
        $channelHeader->setTxId($transactionContext->getId());
88
        $channelHeader->setEpoch(0);
89
        $channelHeader->setTimestamp(TimestampFactory::fromDateTime(new \DateTime($dateTime)));
90
91
        $chaincodeId = ChaincodeIdFactory::create(
92
            'MyChaincodePath',
93
            'MyChaincodeName',
94
            'MyChaincodeVersion'
95
        );
96
97
        $chaincodeHeaderExtension = ChaincodeHeaderExtensionFactory::fromChaincodeId($chaincodeId);
98
        $channelHeader->setExtension($chaincodeHeaderExtension->serializeToString());
99
100
        $header = HeaderFactory::create(SignatureHeaderFactory::create(
101
            $identity,
102
            $transactionContext->getNonce()
103
        ), $channelHeader);
104
105
        $chaincodeProposalPayload = ChaincodeProposalPayloadFactory::fromChaincodeInvocationSpecArgs([]);
106
        return ProposalFactory::create($header, $chaincodeProposalPayload->serializeToString());
107
    }
108
109
    /**
110
     * @return array[]
111
     */
112
    protected function loadStaticData(): array
113
    {
114
        $contents = file_get_contents(__DIR__ . '/../../_files/signed-proposals.json');
115
116
        $json = json_decode($contents, true);
0 ignored issues
show
Bug introduced by
It seems like $contents can also be of type false; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

116
        $json = json_decode(/** @scrutinizer ignore-type */ $contents, true);
Loading history...
117
118
        if (json_last_error() !== JSON_ERROR_NONE) {
119
            throw new \RuntimeException(json_last_error_msg(), json_last_error());
120
        }
121
122
        return $json;
123
    }
124
125
    /**
126
     * @return array[]
127
     */
128 View Code Duplication
    public function getChainCodeProposalDataset(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130
        $data = $this->loadStaticData();
131
132
        return array_map(
133
            function ($value) {
134
                return array_intersect_key(
135
                    $value,
136
                    array_flip(['dateTime', 'proposalHeader', 'proposalPayload', 'proposalExtension'])
137
                );
138
            },
139
            $data
140
        );
141
    }
142
143
    /**
144
     * @return \SplFileObject
145
     */
146
    protected function getPrivateKeyFile(): \SplFileObject
147
    {
148
        return $this->privateKeyFile;
149
    }
150
}
151