Message::__construct()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 31
cts 31
cp 1
rs 8.8234
c 0
b 0
f 0
cc 5
nc 6
nop 8
crap 5

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Fhp\Message;
4
5
use Fhp\DataElementGroups\SecurityProfile;
6
use Fhp\Segment\AbstractSegment;
7
use Fhp\Segment\HNHBS;
8
use Fhp\Segment\HNSHA;
9
use Fhp\Segment\HNSHK;
10
use Fhp\Segment\HNVSD;
11
use Fhp\Segment\HNVSK;
12
use Fhp\Segment\SegmentInterface;
13
14
/**
15
 * Class Message.
16
 *
17
 * @package Fhp\Message
18
 */
19
class Message extends AbstractMessage
20
{
21
    /**
22
     * @var int
23
     */
24
    protected $encryptedSegmentsCount = 0;
25
26
    /**
27
     * @var int
28
     */
29
    protected $securityReference;
30
31
    /**
32
     * @var string
33
     */
34
    protected $pin;
35
36
    /**
37
     * @var string
38
     */
39
    protected $bankCode;
40
41
    /**
42
     * @var string
43
     */
44
    protected $username;
45
46
    /**
47
     * @var string
48
     */
49
    protected $systemId;
50
51
    /**
52
     * @var array
53
     */
54
    protected $options;
55
56
    /**
57
     * @var int
58
     */
59
    protected $profileVersion;
60
61
    /**
62
     * @var string
63
     */
64
    protected $securityFunction;
65
66
    /**
67
     * @var array
68
     */
69
    private $encryptedSegments = array();
70
71
    /**
72
     * @var HNVSD
73
     */
74
    protected $encryptionEnvelop;
75
76
    /**
77
     * Message constructor.
78
     * @param string $bankCode
79
     * @param string $username
80
     * @param string $pin
81
     * @param $systemId
82
     * @param int $dialogId
83
     * @param int $messageNumber
84
     * @param array $encryptedSegments
85
     * @param array $options
86
     */
87 4
    public function __construct(
88
        $bankCode,
89
        $username,
90
        $pin,
91
        $systemId,
92
        $dialogId = 0,
93
        $messageNumber = 0,
94
        array $encryptedSegments = array(),
95
        array $options = array()
96
    ) {
97 4
        $this->securityReference = rand(1000000, 9999999);
98 4
        $this->dialogId = $dialogId;
99 4
        $this->messageNumber = $messageNumber;
100 4
        $this->bankCode = $bankCode;
101 4
        $this->username = $username;
102 4
        $this->pin = $pin;
103 4
        $this->systemId = $systemId;
104 4
        $this->options = $options;
105 4
        $this->profileVersion = SecurityProfile::PROFILE_VERSION_1;
106 4
        $this->securityFunction = HNSHK::SECURITY_FUNC_999;
107
108 4
        if (isset($options[static::OPT_PINTAN_MECH]) && !empty($this->options[static::OPT_PINTAN_MECH])) {
109 1
            if (!in_array('999', $this->options[static::OPT_PINTAN_MECH])) {
110 1
                $this->profileVersion = SecurityProfile::PROFILE_VERSION_2;
111 1
                $this->securityFunction = $this->options[static::OPT_PINTAN_MECH][0];
112 1
            }
113 1
        }
114
115 4
        $signatureHead = $this->buildSignatureHead();
116 4
        $hnvsk = $this->buildEncryptionHead();
117
118 4
        $this->addSegment($hnvsk);
119
120 4
        $this->encryptionEnvelop = new HNVSD(999, '');
121 4
        $this->addSegment($this->encryptionEnvelop);
122
123 4
        $this->addEncryptedSegment($signatureHead);
124
125 4
        foreach ($encryptedSegments as $es) {
126 1
            $this->addEncryptedSegment($es);
127 4
        }
128
129 4
        $curCount = count($encryptedSegments) + 3;
130
131 4
        $signatureEnd = new HNSHA($curCount, $this->securityReference, $this->pin);
132 4
        $this->addEncryptedSegment($signatureEnd);
133 4
        $this->addSegment(new HNHBS($curCount + 1, $this->messageNumber));
134 4
    }
135
136
    /**
137
     * @return HNVSK
138
     * @codeCoverageIgnore
139
     */
140
    protected function buildEncryptionHead()
141
    {
142
        return new HNVSK(
143
            998,
144
            $this->bankCode,
145
            $this->username,
146
            $this->systemId,
147
            HNVSK::SECURITY_SUPPLIER_ROLE_ISS,
148
            HNVSK::DEFAULT_COUNTRY_CODE,
149
            HNVSK::COMPRESSION_NONE,
150
            $this->profileVersion
151
        );
152
    }
153
154
    /**
155
     * @return HNSHK
156
     * @codeCoverageIgnore
157
     */
158
    protected function buildSignatureHead()
159
    {
160
        return new HNSHK(
161
            2,
162
            $this->securityReference,
163
            280, // country code
164
            $this->bankCode,
165
            $this->username,
166
            $this->systemId,
167
            $this->securityFunction,
168
            HNSHK::SECURITY_BOUNDARY_SHM,
169
            HNSHK::SECURITY_SUPPLIER_ROLE_ISS,
170
            $this->profileVersion
171
        );
172
    }
173
174
    /**
175
     * Adds a encrypted segment to the message.
176
     *
177
     * @param SegmentInterface $segment
178
     */
179 4
    protected function addEncryptedSegment(SegmentInterface $segment)
180
    {
181 4
        $this->encryptedSegmentsCount++;
182 4
        $this->encryptedSegments[] = $segment;
183 4
        $encodedData = $this->encryptionEnvelop->getEncodedData()->getData();
184 4
        $encodedData .= (string) $segment;
185 4
        $this->encryptionEnvelop->setEncodedData($encodedData);
186 4
    }
187
188
    /**
189
     * Only for read-only access.
190
     * @return AbstractSegment[]
191
     */
192 1
    public function getEncryptedSegments()
193
    {
194 1
        return $this->encryptedSegments;
195
    }
196
}
197