Passed
Push — master ( 99d0e8...9e7049 )
by Ondra
03:35
created

Response::__construct()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 33
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 1
b 0
f 0
nc 8
nop 10
dl 0
loc 33
ccs 16
cts 16
cp 1
crap 4
rs 9.7998

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
/**
4
 * This file is part of the Pixidos package.
5
 *
6
 *  (c) Ondra Votava <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 *
11
 */
12
13
declare(strict_types=1);
14
15
namespace Pixidos\GPWebPay\Data;
16
17
use Pixidos\GPWebPay\Enum\Operation as EnumOperation;
18
use Pixidos\GPWebPay\Enum\Param;
19
use Pixidos\GPWebPay\Param\IParam;
20
use Pixidos\GPWebPay\Param\Md;
21
use Pixidos\GPWebPay\Param\MerOrderNum;
22
use Pixidos\GPWebPay\Param\Operation as ParamOperation;
23
use Pixidos\GPWebPay\Param\OrderNumber;
24
use Pixidos\GPWebPay\Param\ResponseParam;
25
use Pixidos\GPWebPay\Param\Utils\Sorter;
26
27
class Response implements ResponseInterface
28
{
29
    /**
30
     * @var array<string, IParam> $params
31
     */
32
    private array $params;
33
    /**
34
     * @var string digest
35
     */
36
    private string $digest;
37
    /**
38
     * @var string digest1
39
     */
40
    private string $digest1;
41
    /**
42
     * @var string gatewayKey
43
     */
44
    private string $gatewayKey;
45
46
47 16
    public function __construct(
48
        string $operation,
49
        string $ordernumber,
50
        string $merordernum,
51
        string $md,
52
        int $prcode,
53
        int $srcode,
54
        string $resulttext,
55
        string $digest,
56
        string $digest1,
57
        string $gatewayKey
58
    ) {
59 16
        $this->addParam(
60 16
            new ParamOperation(EnumOperation::fromScalar($operation))
61 16
        );
62 16
        $this->addParam(new OrderNumber($ordernumber));
63
64 16
        if ('' !== $merordernum) {
65 9
            $this->addParam(new MerOrderNum($merordernum));
66
        }
67
68 16
        if ('' !== $md) {
69 14
            $this->addParam(new Md($md));
70
        }
71 16
        $this->addParam(new ResponseParam((string)$prcode, self::PRCODE));
72 16
        $this->addParam(new ResponseParam((string)$srcode, self::SRCODE));
73 16
        if ($resulttext !== '') {
74 15
            $this->addParam(new ResponseParam($resulttext, self::RESULTTEXT));
75
        }
76
77 16
        $this->digest = $digest;
78 16
        $this->digest1 = $digest1;
79 16
        $this->gatewayKey = $gatewayKey;
80
    }
81
82
83
    /**
84
     * @return string
85
     */
86 12
    public function getDigest(): string
87
    {
88 12
        return $this->digest;
89
    }
90
91
    /**
92
     * @return bool
93
     */
94 12
    public function hasError(): bool
95
    {
96 12
        return (bool)$this->params[self::PRCODE]->getValue()
97 12
            || (bool)$this->params[self::SRCODE]->getValue();
98
    }
99
100
    /**
101
     * @return string
102
     */
103 12
    public function getDigest1(): string
104
    {
105 12
        return $this->digest1;
106
    }
107
108
    /**
109
     * @return string|null
110
     */
111 9
    public function getMerOrderNumber(): ?string
112
    {
113 9
        return isset($this->params[Param::MERORDERNUM]) ? (string)$this->params[Param::MERORDERNUM] : null;
114
    }
115
116
    /**
117
     * @return string|null
118
     */
119 4
    public function getMd(): ?string
120
    {
121 4
        if (!isset($this->params[Param::MD])) {
122
            return null;
123
        }
124 4
        $explode = explode('|', (string)$this->params[Param::MD], 2);
125
126 4
        return $explode[1] ?? null;
127
    }
128
129
    /**
130
     * @return string
131
     */
132 15
    public function getGatewayKey(): string
133
    {
134 15
        return $this->gatewayKey;
135
    }
136
137
    /**
138
     * @return string
139
     */
140 9
    public function getOrderNumber(): string
141
    {
142 9
        return (string)$this->params[Param::ORDERNUMBER];
143
    }
144
145
    /**
146
     * @return int
147
     */
148 7
    public function getSrcode(): int
149
    {
150 7
        return (int)$this->params[self::SRCODE]->__toString();
151
    }
152
153
    /**
154
     * @return int
155
     */
156 7
    public function getPrcode(): int
157
    {
158 7
        return (int)$this->params[self::PRCODE]->__toString();
159
    }
160
161
    /**
162
     * @return string
163
     */
164 7
    public function getResultText(): string
165
    {
166 7
        return (string)$this->params[self::RESULTTEXT];
167
    }
168
169
    /**
170
     * @return string|null
171
     */
172 1
    public function getUserParam1(): ?string
173
    {
174 1
        return isset($this->params[Param::USERPARAM]) ? (string)$this->params[Param::USERPARAM] : null;
175
    }
176
177
178 16
    public function addParam(IParam $param): void
179
    {
180 16
        $this->params[$param->getParamName()] = $param;
181
    }
182
183
    public function getParam(string $paramName): ?IParam
184
    {
185
        return $this->params[$paramName] ?? null;
186
    }
187
188
    /**
189
     * @return array<string, IParam>
190
     */
191 13
    public function getParams(): array
192
    {
193 13
        return $this->params;
194
    }
195
196 13
    public function sortParams(): void
197
    {
198 13
        $this->params = Sorter::sortResponseParams($this->params);
199
    }
200
}
201