Completed
Push — master ( 5a0d0e...e13e59 )
by Dmitry
02:04
created

Provider31::get_request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 *      Main class for EasyPay-Provider 3.1
5
 *
6
 *      @package php_EasyPay
7
 *      @version 1.1
8
 *      @author Dmitry Shovchko <[email protected]>
9
 *
10
 */
11
12
namespace EasyPay;
13
14
class Provider31
15
{
16
    /**
17
     *      @var array
18
     */
19
    protected static $options = array(
20
        'ServiceId' => 0,
21
        'UseSign' => false,
22
        'EasySoftPKey' => '',
23
        'ProviderPKey' => '',
24
    );
25
26
    /**
27
     *      @var \EasyPay\Callback
28
     */
29
    protected static $cb;
30
31
    /**
32
     *      @var mixed
33
     */
34
    protected $request;
35
36
    /**
37
     *      @var Provider31\Response
38
     */
39
    protected $response;
40
41
    /**
42
     *      Provider31 constructor
43
     *
44
     *      @param array $options
45
     *      @param Callback $cb
46
     *      @param \Debulog\LoggerInterface $log
47
     */
48
    public function __construct(array $options, \EasyPay\Callback $cb, \Debulog\LoggerInterface $log)
49
    {
50
        self::$options = array_merge(self::$options, $options);
51
        self::$cb = $cb;
52
53
        Log::set($log);
54
    }
55
56
    /**
57
     *      Get and process request, echo response
58
     *
59
     */
60
    public function process()
61
    {
62
        try
63
        {
64
            //      get request
65
            $this->get_request();
66
67
            //      validate request
68
            $this->request->validate_request(self::$options);
69
            Log::instance()->debug('request is valid');
70
71
            //      verify sign
72
            $this->request->verify_sign(self::$options);
73
            Log::instance()->debug('signature of request is correct');
74
75
            //      get response
76
            $this->response = $this->get_response();
77
78
            Log::instance()->add('the request was processed successfully');
79
        }
80
        catch (Exception\Structure $e)
81
        {
82
            $this->response = $this->get_error_response($e->getCode(), 'Error in request');
83
        }
84
        catch (Exception\Sign $e)
85
        {
86
            $this->response = $this->get_error_response($e->getCode(), 'Signature error!');
87
        }
88
        catch (Exception\Runtime $e)
89
        {
90
            $this->response = $this->get_error_response($e->getCode(), 'Error while processing request');
91
        }
92
        catch (\Exception $e)
93
        {
94
            $this->response = $this->get_error_response($e->getCode(), $e->getMessage());
95
        }
96
97
        //      output response
98
        $this->response->sign_and_out(self::$options);
99
    }
100
101
    /**
102
     *      method to create a specific class of request
103
     *
104
     *      @return Request\General Request class of the appropriate type
0 ignored issues
show
Bug introduced by
The type EasyPay\Request\General was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
105
     *      @throws \EasyPay\Exception\Structure
106
     */
107
    protected function get_request()
108
    {
109
        $raw = new Provider31\Request\RAW();
110
111
        $r = new Provider31\Request\General($raw);
112
        $c = '\\EasyPay\\Provider31\\Request\\'.$r->Operation();
113
114
        $this->request = new $c($raw);
115
    }
116
117
    /**
118
     *      Process request and generate response
119
     *
120
     *      @throws Exception\Structure
121
     */
122
    protected function get_response()
123
    {
124
        switch ($this->request->Operation())
125
        {
126
            case 'Check':
127
                $response = $this->response_check();
128
                break;
129
130
            case 'Payment':
131
                $response = $this->response_payment();
132
                break;
133
134
            case 'Confirm':
135
                $response = $this->response_confirm();
136
                break;
137
138
            case 'Cancel';
139
                $response = $this->response_cancel();
140
                break;
141
142
            default:
143
                throw new Exception\Structure('There is not supported value of Operation in xml-request!', -99);
144
        }
145
146
        return $response;
147
    }
148
149
    /**
150
     *      run check callback and generate a response
151
     *
152
     *      @return Provider31\Response\Check
153
     */
154
    private function response_check()
155
    {
156
        Log::instance()->add(sprintf('Check("%s")', $this->request->Account()));
157
158
        $accountinfo = self::$cb->check(
159
            $this->request->Account()
160
        );
161
162
        // Sending a response
163
        return new Provider31\Response\Check($accountinfo);
164
    }
165
166
    /**
167
     *      run payment callback and generate a response
168
     *
169
     *      @return Provider31\Response\Payment
170
     */
171
    private function response_payment()
172
    {
173
        Log::instance()->add(sprintf('Payment("%s", "%s", "%s")', $this->request->Account(), $this->request->OrderId(), $this->request->Amount()));
174
175
        $paymentid = self::$cb->payment(
176
            $this->request->Account(),
177
            $this->request->OrderId(),
178
            $this->request->Amount()
179
        );
180
181
        // Sending a response
182
        return new Provider31\Response\Payment($paymentid);
183
    }
184
185
    /**
186
     *      run confirm callback and generate a response
187
     *
188
     *      @return Provider31\Response\Confirm
189
     */
190 View Code Duplication
    private function response_confirm()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
    {
192
        Log::instance()->add(sprintf('Confirm("%s")', $this->request->PaymentId()));
193
194
        $orderdate = self::$cb->confirm(
195
            $this->request->PaymentId()
196
        );
197
198
        // Sending a response
199
        return new Provider31\Response\Confirm($orderdate);
200
    }
201
202
    /**
203
     *      run cancel callback and generate a response
204
     *
205
     *      @return Provider31\Response\Cancel
206
     */
207 View Code Duplication
    private function response_cancel()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
208
    {
209
        Log::instance()->add(sprintf('Cancel("%s")', $this->request->PaymentId()));
210
211
        $canceldate = self::$cb->cancel(
212
            $this->request->PaymentId()
213
        );
214
215
        // Sending a response
216
        return new Provider31\Response\Cancel($canceldate);
217
    }
218
219
    /**
220
     *      Generates an xml with an error message
221
     *
222
     *      @param integer $code
223
     *      @param string $message
224
     *
225
     *      @return Provider31\Response\ErrorInfo
226
     */
227
    private function get_error_response($code, $message)
228
    {
229
        Log::instance()->add('the request was processed with an error');
230
231
        // Sending a response
232
        return new Provider31\Response\ErrorInfo($code, $message);
233
    }
234
}
235