Issues (8)

src/Pos/AbstractPos.php (1 issue)

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
11
abstract class AbstractPos
12
{
13
    /**
14
     * @var AbstractConfiguration
15
     */
16
    protected $configuration;
17
18
19
    public function __construct(AbstractConfiguration $configuration)
20
    {
21
        $this->configuration = $configuration;
22
    }
23
24
    /**
25
     *  build complete raw data for the specified request.
26
     *
27
     * @param \Paranoia\Request\Request $request
28
     * @param string $transactionType
29
     *
30
     * @return mixed
31
     */
32
    abstract protected function buildRequest(Request $request, $transactionType);
33
34
    /**
35
     * parses response from returned provider.
36
     *
37
     * @param string $rawResponse
38
     * @param string $transactionType
39
     *
40
     * @return \Paranoia\Response
41
     */
42
    abstract protected function parseResponse($rawResponse, $transactionType);
43
44
    /**
45
     * Makes http request to remote host.
46
     *
47
     * @param string $url
48
     * @param mixed  $data
49
     * @param array $options
50
     *
51
     * @throws CommunicationError
52
     * @return mixed
53
     */
54
    protected function sendRequest($url, $data, $options = null)
0 ignored issues
show
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

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