• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlocati / PayWay / 8493738543

30 Mar 2024 10:18PM UTC coverage: 74.592% (-0.1%) from 74.732%
8493738543

push

github

web-flow
Fix coding style (#7)

1600 of 2145 relevant lines covered (74.59%)

1.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.0
/src/Service/BaseRequestTrait.php
1
<?php
2

3
namespace MLocati\PayWay\Service;
4

5
use DateTime;
6
use DateTimeInterface;
7
use MLocati\PayWay\Exception;
8
use ValueError;
9

10
trait BaseRequestTrait
11
{
12
    use JsonCleanupTrait;
13

14
    /**
15
     * Undocumented.
16
     *
17
     * @var string
18
     */
19
    private $apiVersion = '2.4.1';
20

21
    /**
22
     * The merchant terminal code.
23
     *
24
     * @var string
25
     */
26
    private $tid = '';
27

28
    /**
29
     * Undocumented.
30
     *
31
     * @var string
32
     */
33
    private $merID = '';
34

35
    /**
36
     * Undocumented.
37
     *
38
     * @var string
39
     */
40
    private $payInstr = '';
41

42
    /**
43
     * @var \DateTimeInterface|null
44
     */
45
    private $reqTime;
46

47
    /**
48
     * Undocumented.
49
     *
50
     * @return string
51
     */
52
    public function getApiVersion()
53
    {
54
        return $this->apiVersion;
5✔
55
    }
56

57
    /**
58
     * Undocumented.
59
     *
60
     * @param string $value
61
     *
62
     * @return $this
63
     */
64
    public function setApiVersion($value)
65
    {
66
        $this->apiVersion = (string) $value;
×
67

68
        return $this;
×
69
    }
70

71
    /**
72
     * Get the merchant terminal code.
73
     *
74
     * @return string
75
     */
76
    public function getTID()
77
    {
78
        return $this->tid;
5✔
79
    }
80

81
    /**
82
     * Set the merchant terminal code.
83
     *
84
     * @param string $value
85
     *
86
     * @return $this
87
     */
88
    public function setTID($value)
89
    {
90
        $this->tid = (string) $value;
2✔
91

92
        return $this;
2✔
93
    }
94

95
    /**
96
     * Undocumented.
97
     *
98
     * @return string
99
     */
100
    public function getMerID()
101
    {
102
        return $this->merID;
5✔
103
    }
104

105
    /**
106
     * Undocumented.
107
     *
108
     * @param string $value
109
     *
110
     * @return $this
111
     */
112
    public function setMerID($value)
113
    {
114
        $this->merID = (string) $value;
×
115

116
        return $this;
×
117
    }
118

119
    /**
120
     * Undocumented.
121
     *
122
     * @return string
123
     */
124
    public function getPayInstr()
125
    {
126
        return $this->payInstr;
5✔
127
    }
128

129
    /**
130
     * Undocumented.
131
     *
132
     * @param string $value
133
     *
134
     * @return $this
135
     */
136
    public function setPayInstr($value)
137
    {
138
        $this->payInstr = (string) $value;
×
139

140
        return $this;
×
141
    }
142

143
    /**
144
     * Undocumented.
145
     *
146
     * @return \DateTimeInterface|null
147
     */
148
    public function getReqTime()
149
    {
150
        return $this->reqTime;
5✔
151
    }
152

153
    /**
154
     * Undocumented.
155
     *
156
     * @return $this
157
     */
158
    public function setReqTime(DateTimeInterface $value = null)
159
    {
160
        $this->reqTime = $value;
×
161

162
        return $this;
×
163
    }
164

165
    /**
166
     * Get the HMAC-SHA256 signature.
167
     *
168
     * @param string $key
169
     *
170
     * @throws \MLocati\PayWay\Exception\UnableToCreateSignature
171
     *
172
     * @return string
173
     */
174
    public function getSignature($key)
175
    {
176
        $data = implode('', $this->getSignatureFields());
5✔
177
        try {
178
            $rawSignature = hash_hmac('sha256', $data, $key, true);
5✔
179
        } catch (ValueError $x) {
×
180
            throw new Exception\UnableToCreateSignature($x->getMessage());
×
181
        }
182
        if ($rawSignature === false) {
5✔
183
            throw new Exception\UnableToCreateSignature();
×
184
        }
185

186
        return base64_encode($rawSignature);
5✔
187
    }
188

189
    /**
190
     * {@inheritdoc}
191
     *
192
     * @see \JsonSerializable::jsonSerialize()
193
     */
194
    #[\ReturnTypeWillChange]
195
    public function jsonSerialize()
196
    {
197
        return $this->cleanupJson([
2✔
198
            'apiVersion' => $this->apiVersion,
2✔
199
            'tid' => $this->tid,
2✔
200
            'merID' => $this->merID,
2✔
201
            'payInstr' => $this->payInstr,
2✔
202
            'reqTime' => $this->reqTime === null ? '' : $this->reqTime->format(DateTime::RFC3339),
2✔
203
        ]);
2✔
204
    }
205

206
    /**
207
     * @return string[]
208
     *
209
     * @private
210
     */
211
    abstract protected function getSignatureFields();
212

213
    /**
214
     * @throws \MLocati\PayWay\Exception\MissingRequiredField
215
     * @throws \MLocati\PayWay\Exception\FieldValueTooLong
216
     * @throws \MLocati\PayWay\Exception\FieldValueOutOfRange
217
     * @throws \MLocati\PayWay\Exception\InvalidFieldUrl
218
     */
219
    private function checkBaseRequest()
220
    {
221
        $this->checkStringField('tid', true, 16);
5✔
222
    }
223

224
    /**
225
     * @param string $fieldName
226
     * @param bool $required
227
     * @param int $maxLength
228
     *
229
     * @throws \MLocati\PayWay\Exception\MissingRequiredField
230
     * @throws \MLocati\PayWay\Exception\FieldValueTooLong
231
     */
232
    private function checkStringField($fieldName, $required, $maxLength)
233
    {
234
        $value = $this->{$fieldName};
5✔
235
        if ($value === '') {
5✔
236
            if ($required) {
4✔
237
                throw new Exception\MissingRequiredField($fieldName);
×
238
            }
239
        } elseif (strlen($value) > $maxLength) {
5✔
240
            throw new Exception\FieldValueTooLong($fieldName, $maxLength);
×
241
        }
242
    }
243

244
    /**
245
     * @param string $fieldName
246
     * @param bool $required
247
     * @param string[] $allowedValues
248
     *
249
     * @throws \MLocati\PayWay\Exception\MissingRequiredField
250
     * @throws \MLocati\PayWay\Exception\FieldValueOutOfRange
251
     */
252
    private function checkEnumField($fieldName, $required, array $allowedValues)
253
    {
254
        $value = $this->{$fieldName};
4✔
255
        if ($value === '') {
4✔
256
            if ($required) {
4✔
257
                throw new Exception\MissingRequiredField($fieldName);
×
258
            }
259
        } elseif (!in_array($value, $allowedValues, true)) {
4✔
260
            throw new Exception\FieldValueOutOfRange($fieldName, $allowedValues);
×
261
        }
262
    }
263

264
    /**
265
     * @param string $fieldName
266
     * @param bool $required
267
     * @param int $maxLength
268
     *
269
     * @throws \MLocati\PayWay\Exception\MissingRequiredField
270
     * @throws \MLocati\PayWay\Exception\FieldValueTooLong
271
     * @throws \MLocati\PayWay\Exception\InvalidFieldUrl
272
     */
273
    private function checkUrlField($fieldName, $required, $maxLength)
274
    {
275
        $value = $this->{$fieldName};
4✔
276
        if ($value === '') {
4✔
277
            if ($required) {
4✔
278
                throw new Exception\MissingRequiredField($fieldName);
×
279
            }
280
        } elseif (strlen($value) > $maxLength) {
4✔
281
            throw new Exception\FieldValueTooLong($fieldName, $maxLength);
×
282
        } elseif (!filter_var($value, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)) {
4✔
283
            throw new Exception\InvalidFieldUrl($fieldName);
×
284
        }
285
    }
286
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc