Completed
Push — master ( dc93ce...91f911 )
by Dmitry
02:08
created

CompletePurchaseRequest::getData()   C

Complexity

Conditions 10
Paths 48

Size

Total Lines 48
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 0
cts 26
cp 0
rs 5.3454
c 0
b 0
f 0
cc 10
eloc 29
nc 48
nop 0
crap 110

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * OKPAY driver for Omnipay PHP payment library.
4
 *
5
 * @link      https://github.com/hiqdev/omnipay-okpay
6
 * @package   omnipay-okpay
7
 * @license   MIT
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace Omnipay\OKPAY\Message;
12
13
use Omnipay\Common\Exception\InvalidResponseException;
14
15
class CompletePurchaseRequest extends AbstractRequest
16
{
17
    public function getData()
18
    {
19
        $request = 'ok_verify=true';
20
21
        foreach ($this->httpRequest->request->all() as $key => $value) {
22
            $value = urlencode(stripslashes($value));
23
            $request .= "&$key=$value";
24
        }
25
26
        $fsocket = false;
27
        $result = false;
28
29
        if ($fp = @fsockopen('ssl://www.okpay.com', 443, $errno, $errstr, 30)) {
30
            // Connected via HTTPS
31
            $fsocket = true;
32
        } elseif ($fp = @fsockopen('www.okpay.com', 80, $errno, $errstr, 30)) {
33
            // Connected via HTTP
34
            $fsocket = true;
35
        }
36
37
        // If connected to OKPAY
38
        if ($fsocket === true) {
39
            $header = 'POST /ipn-verify.html HTTP/1.0' . "\r\n" .
40
                'Host: www.okpay.com' . "\r\n" .
41
                'Content-Type: application/x-www-form-urlencoded' . "\r\n" .
42
                'Content-Length: ' . strlen($request) . "\r\n" .
43
                'Connection: close' . "\r\n\r\n";
44
45
            @fputs($fp, $header . $request);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
46
            $string = '';
47
            while (!@feof($fp)) {
48
                $res = @fgets($fp, 1024);
49
                $string .= $res;
50
                // Find verification result in response
51
                if ($res === 'VERIFIED' || $res === 'INVALID' || $res === 'TEST') {
52
                    $result = $res;
53
                    break;
54
                }
55
            }
56
            @fclose($fp);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
57
        }
58
59
        if ($result !== 'VERIFIED') {
60
            throw new InvalidResponseException('IPN verify failed: ' . $result);
61
        }
62
63
        return $this->httpRequest->request->all();
64
    }
65
66 1
    public function sendData($data)
67
    {
68 1
        return $this->response = new CompletePurchaseResponse($this, $data);
69
    }
70
}
71