Passed
Pull Request — master (#6)
by Mattia
32:50 queued 25:28
created

IgfsCgPayByMailInit   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Test Coverage

Coverage 83.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 115
c 1
b 0
f 0
dl 0
loc 173
ccs 82
cts 98
cp 0.8367
rs 10
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A buildRequest() 0 30 2
B checkFields() 0 23 9
A resetFields() 0 26 1
A parseResponseMap() 0 7 1
A getResponseSignature() 0 12 1
A getAdditionalRequestSignatureFields() 0 18 1
1
<?php
2
3
namespace PagOnline\PayByMail;
4
5
use PagOnline\Exceptions\IgfsException;
6
use PagOnline\Exceptions\IgfsMissingParException;
7
use PagOnline\IgfsUtils;
8
9
class IgfsCgPayByMailInit extends BaseIgfsCgPayByMail
10
{
11
    public $shopUserRef;
12
    public $shopUserName;
13
    public $shopUserAccount;
14
    public $shopUserMobilePhone;
15
    public $shopUserIMEI;
16
    public $trType = 'AUTH';
17
    public $linkType = 'MAIL';
18
    public $amount;
19
    public $currencyCode;
20
    public $langID = 'EN';
21
    public $callbackURL;
22
    public $addInfo1;
23
    public $addInfo2;
24
    public $addInfo3;
25
    public $addInfo4;
26
    public $addInfo5;
27
    public $accountName;
28
    public $level3Info;
29
    public $description;
30
    public $paymentReason;
31
32
    public $mailID;
33
    public $linkURL;
34
    /**
35
     * @var string
36
     */
37
    protected $requestNamespace = Requests\IgfsCgPayByMailInitRequest::class;
38
39 1
    public function resetFields()
40
    {
41 1
        parent::resetFields();
42 1
        $this->shopUserRef = null;
43 1
        $this->shopUserName = null;
44 1
        $this->shopUserAccount = null;
45 1
        $this->shopUserMobilePhone = null;
46 1
        $this->shopUserIMEI = null;
47 1
        $this->trType = 'AUTH';
48 1
        $this->linkType = 'MAIL';
49 1
        $this->amount = null;
50 1
        $this->currencyCode = null;
51 1
        $this->langID = 'EN';
52 1
        $this->callbackURL = null;
53 1
        $this->addInfo1 = null;
54 1
        $this->addInfo2 = null;
55 1
        $this->addInfo3 = null;
56 1
        $this->addInfo4 = null;
57 1
        $this->addInfo5 = null;
58 1
        $this->accountName = null;
59 1
        $this->level3Info = null;
60 1
        $this->description = null;
61 1
        $this->paymentReason = null;
62
63 1
        $this->mailID = null;
64 1
        $this->linkURL = null;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 1
    protected function getAdditionalRequestSignatureFields(): array
71
    {
72 1
        return [
73 1
            $this->shopUserRef, // SHOPUSERREF
74 1
            $this->shopUserName, // SHOPUSERNAME
75 1
            $this->shopUserAccount, // SHOPUSERACCOUNT
76 1
            $this->shopUserMobilePhone, //SHOPUSERMOBILEPHONE
77 1
            $this->shopUserIMEI, //SHOPUSERIMEI
78 1
            $this->trType, // TRTYPE
79 1
            $this->amount, // AMOUNT
80 1
            $this->currencyCode, // CURRENCYCODE
81 1
            $this->langID, // LANGID
82 1
            $this->callbackURL, // CALLBACKURL
83 1
            $this->addInfo1, // UDF1
84 1
            $this->addInfo2, // UDF2
85 1
            $this->addInfo3, // UDF3
86 1
            $this->addInfo4, // UDF4
87 1
            $this->addInfo5,
88 1
        ];
89
    }
90
91 10
    protected function checkFields()
92
    {
93 10
        parent::checkFields();
94 6
        if ($this->trType == null) {
95 1
            throw new IgfsMissingParException('Missing trType');
96
        }
97 5
        if ($this->langID == null) {
98 1
            throw new IgfsMissingParException('Missing langID');
99
        }
100 4
        if ($this->shopUserRef == null) {
101 1
            throw new IgfsMissingParException('Missing shopUserRef');
102
        }
103 3
        if ($this->level3Info != null) {
104 2
            $i = 0;
105 2
            if ($this->level3Info->product != null) {
106 2
                foreach ($this->level3Info->product as $product) {
107 2
                    if ($product->productCode == null) {
108 1
                        throw new IgfsMissingParException("Missing productCode[{$i}]");
109
                    }
110 1
                    if ($product->productDescription == null) {
111 1
                        throw new IgfsMissingParException("Missing productDescription[{$i}]");
112
                    }
113
                    ++$i;
114
                }
115
            }
116
        }
117
    }
118
119 1
    protected function buildRequest()
120
    {
121 1
        $request = parent::buildRequest();
122 1
        $this->replaceRequestParameter($request, 'shopUserRef', $this->shopUserRef);
123 1
        $this->replaceRequestParameter($request, 'shopUserName', $this->shopUserName);
124 1
        $this->replaceRequestParameter($request, 'shopUserAccount', $this->shopUserAccount);
125 1
        $this->replaceRequestParameter($request, 'shopUserMobilePhone', $this->shopUserMobilePhone);
126 1
        $this->replaceRequestParameter($request, 'shopUserIMEI', $this->shopUserIMEI);
127 1
        $this->replaceRequestParameter($request, 'trType', $this->trType);
128 1
        $this->replaceRequestParameter($request, 'linkType', $this->linkType);
129 1
        $this->replaceRequestParameter($request, 'amount', $this->amount);
130 1
        $this->replaceRequestParameter($request, 'currencyCode', $this->currencyCode);
131 1
        $this->replaceRequestParameter($request, 'langID', $this->langID);
132 1
        $this->replaceRequestParameter($request, 'callbackURL', $this->callbackURL);
133 1
        $this->replaceRequestParameter($request, 'addInfo1', $this->addInfo1);
134 1
        $this->replaceRequestParameter($request, 'addInfo2', $this->addInfo2);
135 1
        $this->replaceRequestParameter($request, 'addInfo3', $this->addInfo3);
136 1
        $this->replaceRequestParameter($request, 'addInfo4', $this->addInfo4);
137 1
        $this->replaceRequestParameter($request, 'addInfo5', $this->addInfo5);
138 1
        $this->replaceRequestParameter($request, 'accountName', $this->accountName);
139 1
        $this->replaceRequestParameter($request, 'description', $this->description);
140 1
        $this->replaceRequestParameter($request, 'paymentReason', $this->paymentReason);
141
142 1
        if ($this->level3Info != null) {
143
            $this->replaceRequestParameter($request, 'level3Info', $this->level3Info->toXml('level3Info'), false);
144
        } else {
145 1
            $this->replaceRequestParameter($request, 'level3Info', '');
146
        }
147
148 1
        return $request;
149
    }
150
151
    /**
152
     * @param array $response
153
     */
154
    protected function parseResponseMap($response)
155
    {
156
        parent::parseResponseMap($response);
157
        // Opzionale
158
        $this->mailID = IgfsUtils::getValue($response, 'mailID');
159
        // Opzionale
160
        $this->linkURL = IgfsUtils::getValue($response, 'linkURL');
161
    }
162
163
    /**
164
     * @param array $response
165
     *
166
     * @throws IgfsException
167
     *
168
     * @return string
169
     */
170
    protected function getResponseSignature($response): string
171
    {
172
        $fields = [
173
            IgfsUtils::getValue($response, 'tid'), // TID
174
            IgfsUtils::getValue($response, 'shopID'), // SHOPID
175
            IgfsUtils::getValue($response, 'rc'), // RC
176
            IgfsUtils::getValue($response, 'errorDesc'), // ERRORDESC
177
            IgfsUtils::getValue($response, 'mailID'), // MAILID
178
            IgfsUtils::getValue($response, 'linkURL'),
179
        ];
180
        // signature dove il buffer e' cosi composto TID|SHOPID|RC|ERRORDESC|MAILID
181
        return $this->getSignature($fields);
182
    }
183
}
184