Purchase::validate()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 16
cts 16
cp 1
rs 8.8657
c 0
b 0
f 0
cc 6
nc 6
nop 1
crap 6
1
<?php
2
3
namespace Omnipay\Paysera\Common;
4
5
use Omnipay\Paysera\Message\PurchaseRequest;
6
use Omnipay\Common\Exception\InvalidRequestException;
7
8
class Purchase
9
{
10
    /**
11
     * Generate the encoded string with parameters.
12
     *
13
     * @param  \Omnipay\Paysera\Message\PurchaseRequest  $request
14
     * @return string
15
     *
16
     * @throws \Omnipay\Common\Exception\InvalidRequestException
17
     */
18 5
    public static function generate(PurchaseRequest $request)
19
    {
20 5
        $parameters = static::parameters($request);
21
22 5
        if ($card = $request->getCard()) {
23 4
            $parameters = array_merge($parameters, static::customer($card));
24
        }
25
26 5
        static::validate($parameters = self::filterParameters($parameters));
27
28 2
        return Encoder::encode(http_build_query($parameters, '', '&'));
29
    }
30
31
    /**
32
     * Get the parameters from the request.
33
     *
34
     * @param  \Omnipay\Paysera\Message\PurchaseRequest  $request
35
     * @return array
36
     */
37 5
    protected static function parameters(PurchaseRequest $request)
38
    {
39
        return [
40 5
            'projectid' => $request->getProjectId(),
41 5
            'orderid' => $request->getTransactionId(),
42 5
            'accepturl' => $request->getReturnUrl(),
43 5
            'cancelurl' => $request->getCancelUrl(),
44 5
            'callbackurl' => $request->getNotifyUrl(),
45 5
            'version' => $request->getVersion(),
46 5
            'payment' => $request->getPaymentMethod(),
47 5
            'lang' => $request->getLanguage(),
48 5
            'amount' => $request->getAmountInteger(),
49 5
            'currency' => $request->getCurrency(),
50 5
            'test' => $request->getTestMode() ? '1' : '0',
51
        ];
52
    }
53
54
    /**
55
     * Get the customer from card.
56
     *
57
     * @param  \Omnipay\Common\CreditCard  $card
58
     * @return array
59
     */
60 4
    protected static function customer($card)
61
    {
62
        return [
63 4
            'p_firstname' => $card->getFirstName(),
64 4
            'p_lastname' => $card->getLastName(),
65 4
            'p_email' => $card->getEmail(),
66 4
            'p_street' => $card->getAddress1(),
67 4
            'p_city' => $card->getCity(),
68 4
            'p_state' => $card->getState(),
69 4
            'p_zip' => $card->getPostcode(),
70 4
            'country' => $card->getCountry(),
71
        ];
72
    }
73
74
    /**
75
     * Get the filtered parameters.
76
     *
77
     * @param  array  $parameters
78
     * @return array
79
     */
80
    protected static function filterParameters(array $parameters)
81
    {
82 5
        return array_filter($parameters, function ($value) {
83 5
            return ! is_null($value) && $value !== '';
84 5
        });
85
    }
86
87
    /**
88
     * Validate the data.
89
     *
90
     * @param  array  $data
91
     * @return void
92
     * @throws \Omnipay\Common\Exception\InvalidRequestException
93
     */
94 5
    public static function validate(array $data)
95
    {
96 5
        foreach (self::getRequestSpecifications() as $specification) {
97 5
            list($name, $maxLength, $isRequired, $regexp) = $specification;
98
99 5
            if (static::isRequiredButMissing($isRequired, $data, $name)) {
100 1
                throw new InvalidRequestException("Parameter [{$name}] is required but missing.");
101
            }
102
103 4
            if (static::exists($data, $name)) {
104 4
                if (static::isTooLong($maxLength, $data[$name])) {
105 1
                    throw new InvalidRequestException(
106 1
                        sprintf(
107 1
                            'Parameter [%s] value is too long (%d), %d characters allowed.',
108 1
                            $name,
109 1
                            strlen($data[$name]),
110 1
                            $maxLength
111
                        )
112
                    );
113
                }
114
115 3
                if (static::isInvalid($regexp, $data[$name])) {
116 3
                    throw new InvalidRequestException("Parameter [{$name}] value [{$data[$name]}] is invalid.");
117
                }
118
            }
119
        }
120 2
    }
121
122
    /**
123
     * Get the request specifications.
124
     *
125
     * Array structure:
126
     *   name      – request parameter name
127
     *   maxLength – max allowed length for parameter
128
     *   required  – is this parameter required
129
     *   regexp    – regexp to test parameter value
130
     *
131
     * @return array
132
     */
133 5
    protected static function getRequestSpecifications()
134
    {
135
        return [
136 5
            ['orderid', 40, true, ''],
137
            ['accepturl', 255, true, ''],
138
            ['cancelurl', 255, true, ''],
139
            ['callbackurl', 255, true, ''],
140
            ['lang', 3, false, '/^[a-z]{3}$/i'],
141
            ['amount', 11, false, '/^\d+$/'],
142
            ['currency', 3, false, '/^[a-z]{3}$/i'],
143
            ['payment', 20, false, ''],
144
            ['country', 2, false, '/^[a-z_]{2}$/i'],
145
            ['p_firstname', 255, false, ''],
146
            ['p_lastname', 255, false, ''],
147
            ['p_email', 255, false, ''],
148
            ['p_street', 255, false, ''],
149
            ['p_city', 255, false, ''],
150
            ['p_state', 20, false, ''],
151
            ['p_zip', 20, false, ''],
152
            ['p_countrycode', 2, false, '/^[a-z]{2}$/i'],
153
            ['test', 1, false, '/^[01]$/'],
154
        ];
155
    }
156
157
    /**
158
     * Determine the parameter is required but missing.
159
     *
160
     * @param  bool  $isRequired
161
     * @param  array  $data
162
     * @param  string  $name
163
     * @return bool
164
     */
165 5
    protected static function isRequiredButMissing($isRequired, $data, $name)
166
    {
167 5
        return $isRequired && ! isset($data[$name]);
168
    }
169
170
    /**
171
     * Determine the parameter exists.
172
     *
173
     * @param  array  $data
174
     * @param  string  $name
175
     * @return bool
176
     */
177 4
    protected static function exists($data, $name)
178
    {
179 4
        return ! empty($data[$name]);
180
    }
181
182
    /**
183
     * Determine the parameter value is too long.
184
     *
185
     * @param  mixed  $maxLength
186
     * @param  string  $value
187
     * @return bool
188
     */
189 4
    protected static function isTooLong($maxLength, $value)
190
    {
191 4
        return $maxLength && strlen($value) > $maxLength;
192
    }
193
194
    /**
195
     * Determine the parameter value is invalid.
196
     *
197
     * @param  string  $regexp
198
     * @param  string  $value
199
     * @return bool
200
     */
201 3
    protected static function isInvalid($regexp, $value)
202
    {
203 3
        return $regexp !== '' && ! preg_match($regexp, $value);
204
    }
205
}
206