Passed
Push — master ( 56e3ba...d2a4fa )
by İbrahim
03:24
created

AbstractPos::getProviderTransactionType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
namespace Paranoia\Pos;
3
4
use Guzzle\Http\Client as HttpClient;
5
use Guzzle\Http\Exception\RequestException;
6
use Paranoia\Configuration\AbstractConfiguration;
7
use Paranoia\Exception\CommunicationError;
8
use Paranoia\Request\Request;
9
use Paranoia\TransactionType;
10
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
11
12
abstract class AbstractPos
13
{
14
    /**
15
     * @var AbstractConfiguration
16
     */
17
    protected $configuration;
18
19
    /** @var EventDispatcherInterface  */
20
    private $dispatcher;
0 ignored issues
show
introduced by
The private property $dispatcher is not used, and could be removed.
Loading history...
21
22
    public function __construct(AbstractConfiguration $configuration)
23
    {
24
        $this->configuration = $configuration;
25
    }
26
27
    /**
28
     *  build complete raw data for the specified request.
29
     *
30
     * @param \Paranoia\Request $request
31
     * @param string $transactionType
32
     *
33
     * @return mixed
34
     */
35
    abstract protected function buildRequest(Request $request, $transactionType);
36
37
    /**
38
     * parses response from returned provider.
39
     *
40
     * @param string $rawResponse
41
     * @param string $transactionType
42
     *
43
     * @return \Paranoia\Response\Response
0 ignored issues
show
Bug introduced by
The type Paranoia\Response\Response 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...
44
     */
45
    abstract protected function parseResponse($rawResponse, $transactionType);
46
47
    /**
48
     * Makes http request to remote host.
49
     *
50
     * @param string $url
51
     * @param mixed  $data
52
     * @param array $options
53
     *
54
     * @throws \ErrorException|\Exception
55
     * @return mixed
56
     */
57
    protected function sendRequest($url, $data, $options = null)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

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

57
    protected function sendRequest($url, $data, /** @scrutinizer ignore-unused */ $options = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
    {
59
        $client = new HttpClient();
60
        $client->setConfig(array(
61
           'curl.options' => array(
62
               CURLOPT_SSL_VERIFYPEER => false,
63
               CURLOPT_SSL_VERIFYHOST => false,
64
           )
65
        ));
66
        $request = $client->post($url, null, $data);
67
        try {
68
            return $request->send()->getBody();
69
        } catch (RequestException $e) {
70
            throw new CommunicationError('Communication failed: ' . $url);
71
        }
72
    }
73
74
    private function performTransaction(Request $request, $transactionType)
75
    {
76
        $rawRequest  = $this->buildRequest($request, $transactionType);
77
        $rawResponse = $this->sendRequest($this->configuration->getApiUrl(), $rawRequest);
78
        $response    = $this->parseResponse($rawResponse, $transactionType);
79
        return $response;
80
    }
81
82
    /**
83
     * @param \Paranoia\Request $request
84
     *
85
     * @return \Paranoia\Response\Response
86
     */
87
    public function preAuthorization(Request $request)
88
    {
89
        return $this->performTransaction($request, TransactionType::PRE_AUTHORIZATION);
90
    }
91
92
    /**
93
     * @param \Paranoia\Request $request
94
     *
95
     * @return \Paranoia\Response\Response
96
     */
97
    public function postAuthorization(Request $request)
98
    {
99
        return $this->performTransaction($request, TransactionType::POST_AUTHORIZATION);
100
    }
101
102
    /**
103
     * @param \Paranoia\Request $request
104
     *
105
     * @return \Paranoia\Response\Response
106
     */
107
    public function sale(Request $request)
108
    {
109
        return $this->performTransaction($request, TransactionType::SALE);
110
    }
111
112
    /**
113
     * @param \Paranoia\Request $request
114
     *
115
     * @return \Paranoia\Response\Response
116
     */
117
    public function refund(Request $request)
118
    {
119
        return $this->performTransaction($request, TransactionType::REFUND);
120
    }
121
122
    /**
123
     * @param \Paranoia\Request $request
124
     *
125
     * @return \Paranoia\Response\Response
126
     */
127
    public function cancel(Request $request)
128
    {
129
        return $this->performTransaction($request, TransactionType::CANCEL);
130
    }
131
132
    /**
133
     * @param \Paranoia\Request $request
134
     *
135
     * @return \Paranoia\Response\Response
136
     */
137
    public function pointQuery(Request $request)
138
    {
139
        return $this->performTransaction($request, TransactionType::POINT_INQUIRY);
140
    }
141
142
    /**
143
     * @param \Paranoia\Request $request
144
     *
145
     * @return \Paranoia\Response\Response
146
     */
147
    public function pointUsage(Request $request)
148
    {
149
        return $this->performTransaction($request, TransactionType::POINT_USAGE);
150
    }
151
}
152