Passed
Push — main ( f1b061...8b9c51 )
by Gabriel
03:30
created

HasSoapRequestTrait   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
dl 0
loc 139
rs 10
c 1
b 0
f 0
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A array2xml() 0 17 5
A getSoapEndpoint() 0 3 1
A AESEnc() 0 6 1
A setFRequest() 0 38 3
A runSoapRequest() 0 10 2
A getSoapAction() 0 3 1
A getSoapOptions() 0 6 1
A RSAEnc() 0 6 1
1
<?php
2
3
namespace ByTIC\Omnipay\PlatiOnline\Message\Traits;
4
5
use ByTIC\Omnipay\PlatiOnline\Utils\Urls;
6
use Nip\Utility\Xml;
7
use phpseclib\Crypt\AES;
8
use phpseclib\Crypt\RSA;
9
10
/**
11
 * Trait HasSoapRequestTrait
12
 * @package ByTIC\Omnipay\PlatiOnline\Message\Traits
13
 */
14
trait HasSoapRequestTrait
15
{
16
    use \ByTIC\Omnipay\Common\Message\Traits\Soap\AbstractSoapRequestTrait;
17
18
    /**
19
     * @inheritDoc
20
     */
21
    public function getSoapEndpoint()
22
    {
23
        return null;
24
    }
25
26
    /**
27
     * @return string
28
     */
29
    protected function getSoapAction(): string
30
    {
31
        return 'auth-only';
32
    }
33
34
    protected function getSoapOptions(): array
35
    {
36
        $options = $this->getSoapOptionsGeneric();
37
        $options['location'] = Urls::$url;
38
        $options['uri'] = $this->getSoapAction();
39
        return $options;
40
    }
41
42
    abstract protected function getSoapRequestValidationUrl();
43
    abstract protected function getSoapResponseValidationUrl();
44
45
    /**
46
     * @param \SoapClient $soapClient
47
     * @param $data
48
     * @return \SimpleXMLElement
49
     * @throws \Exception
50
     */
51
    protected function runSoapRequest($soapClient, $data, $type)
52
    {
53
        $request = $this->setFRequest($data, $type, $this->getSoapRequestValidationUrl());
54
        $response = $soapClient->__doRequest($request, Urls::$url, $this->getSoapAction(), 1);
55
        if (empty($response)) {
56
            throw new \Exception('ERROR: Nu am putut comunica cu serverul PO pentru operatiunea de autorizare!');
57
        }
58
        Xml::validate($response, $this->getSoapResponseValidationUrl());
59
        $responseObject = simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA);
60
        return $responseObject;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $responseObject could also return false which is incompatible with the documented return type SimpleXMLElement. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
61
    }
62
63
    /**
64
     * setez f_request, criptez f_request cu AES si cheia AES cu RSA
65
     * @param $message
66
     * @param $type
67
     * @param $validationUrl
68
     * @return mixed
69
     * @throws \Exception
70
     */
71
    protected function setFRequest($message, $type, $validationUrl)
72
    {
73
        // aici construiesc XML din array
74
        $xml = new \SimpleXMLElement('<' . $type . '/>');
75
76
        // test mode
77
        if (in_array($type, ['po_auth_request', 'po_payment_sale_by_token'])) {
78
            $message['f_test_request'] = ($this->getTestMode() == true) ? 1 : 0;
0 ignored issues
show
Bug introduced by
It seems like getTestMode() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
            $message['f_test_request'] = ($this->/** @scrutinizer ignore-call */ getTestMode() == true) ? 1 : 0;
Loading history...
79
            $message['f_sequence'] = rand(1, 1000);
80
            $message['f_customer_ip'] = $this->getClientIp();
0 ignored issues
show
Bug introduced by
It seems like getClientIp() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
            /** @scrutinizer ignore-call */ 
81
            $message['f_customer_ip'] = $this->getClientIp();
Loading history...
81
        }
82
83
        $message['f_timestamp'] = date('Y-m-d\TH:i:sP');
84
85
        // set f_login
86
        $message['f_login'] = $this->getLoginId();
0 ignored issues
show
Bug introduced by
It seems like getLoginId() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
        /** @scrutinizer ignore-call */ 
87
        $message['f_login'] = $this->getLoginId();
Loading history...
87
88
        // sortez parametrii alfabetic
89
        ksort($message);
90
91
        $this->array2xml($message, $xml);
92
        $message = $xml->asXML();
93
94
        // validez XML conform schemei (parametrul 2)
95
        Xml::validate($message, $validationUrl);
96
97
        $request = [
98
            'po_request' => [
99
                'f_login' => $this->getLoginId(),
100
                'f_message' => $this->AESEnc($message),
101
                'f_crypt_message' => $this->RSAEnc()
102
            ]
103
        ];
104
105
        $xml_auth_soap = Xml::fromArray($request)->asXML();
0 ignored issues
show
Bug introduced by
The method asXML() does not exist on DOMDocument. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
        $xml_auth_soap = Xml::fromArray($request)->/** @scrutinizer ignore-call */ asXML();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
        Xml::validate($xml_auth_soap, Urls::$requestXml);
107
108
        return $xml_auth_soap;
109
    }
110
    /**
111
     * function definition to convert array to xml
112
     * @param $arr
113
     * @param $xml_arr
114
     */
115
    protected function array2xml($arr, &$xml_arr)
116
    {
117
        foreach ($arr as $key => $value) {
118
            if (is_array($value)) {
119
                if (!is_numeric($key)) {
120
                    if (strpos($key, 'coupon') !== false) {
121
                        $subnode = $xml_arr->addChild("coupon");
122
                    } else {
123
                        $subnode = $xml_arr->addChild("$key");
124
                    }
125
                    $this->array2xml($value, $subnode);
126
                } else {
127
                    $subnode = $xml_arr->addChild("item");
128
                    $this->array2xml($value, $subnode);
129
                }
130
            } else {
131
                $xml_arr->addChild("$key", htmlspecialchars("$value"));
132
            }
133
        }
134
    }
135
136
137
    // criptez f_request cu AES
138
    protected function AESEnc($message)
139
    {
140
        $aes = new AES();
141
        $aes->setIV($this->getInitialVector());
0 ignored issues
show
Bug introduced by
It seems like getInitialVector() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
        $aes->setIV($this->/** @scrutinizer ignore-call */ getInitialVector());
Loading history...
142
        $aes->setKey($this->getAesKey());
0 ignored issues
show
Bug introduced by
It seems like getAesKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

142
        $aes->setKey($this->/** @scrutinizer ignore-call */ getAesKey());
Loading history...
143
        return bin2hex(base64_encode($aes->encrypt($message)));
144
    }
145
146
    // criptez cheia AES cu RSA
147
    protected function RSAEnc()
148
    {
149
        $rsa = new RSA();
150
        $rsa->loadKey($this->getPublicKey());
0 ignored issues
show
Bug introduced by
It seems like getPublicKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

150
        $rsa->loadKey($this->/** @scrutinizer ignore-call */ getPublicKey());
Loading history...
151
        $rsa->setEncryptionMode(RSA::ENCRYPTION_PKCS1);
152
        return base64_encode($rsa->encrypt($this->getAesKey()));
153
    }
154
}