Completed
Push — master ( 4dc014...648604 )
by Sullivan
02:36
created

AbstractHttpClient::call()   C

Complexity

Conditions 7
Paths 25

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 33
rs 6.7272
cc 7
eloc 19
nc 25
nop 3
1
<?php
2
3
namespace Nexy\PayboxDirect\HttpClient;
4
5
use Nexy\PayboxDirect\Exception\PayboxException;
6
use Nexy\PayboxDirect\Paybox;
7
use Nexy\PayboxDirect\Response\ResponseInterface;
8
9
/**
10
 * @author Sullivan Senechal <[email protected]>
11
 *
12
 * @see http://www1.paybox.com/espace-integrateur-documentation/les-solutions-paybox-direct-et-paybox-direct-plus/
13
 */
14
abstract class AbstractHttpClient
15
{
16
    /**
17
     * @var int
18
     */
19
    protected $timeout;
20
21
    /**
22
     * @var int
23
     */
24
    protected $baseUrl = Paybox::API_URL_TEST;
25
26
    /**
27
     * @var string[]
28
     */
29
    private $baseParameters;
30
31
    /**
32
     * @var int
33
     */
34
    private $defaultDevise;
35
36
    /**
37
     * @var int
38
     */
39
    private $questionNumber;
40
41
    /**
42
     * Constructor.
43
     */
44
    final public function __construct()
45
    {
46
        $this->questionNumber = rand(0, time());
47
    }
48
49
    /**
50
     * @param array $options
51
     */
52
    final public function setOptions($options)
53
    {
54
        $this->timeout = $options['timeout'];
55
        $this->baseUrl = true === $options['production'] ? Paybox::API_URL_PRODUCTION : Paybox::API_URL_TEST;
56
        $this->baseParameters = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('VERSION' => $opti...$options['paybox_key']) of type array<string,?,{"VERSION...IFIANT":"?","CLE":"?"}> is incompatible with the declared type array<integer,string> of property $baseParameters.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57
            'VERSION' => $options['paybox_version'],
58
            'SITE' => $options['paybox_site'],
59
            'RANG' => $options['paybox_rank'],
60
            'IDENTIFIANT' => $options['paybox_identifier'],
61
            'CLE' => $options['paybox_key'],
62
        ];
63
        $this->defaultDevise = $options['paybox_default_currency'];
64
    }
65
66
    /**
67
     * Calls PayBox Direct platform with given operation type and parameters.
68
     *
69
     * @param int      $type          Request type
70
     * @param string[] $parameters    Request parameters
71
     * @param string   $responseClass
72
     *
73
     * @return ResponseInterface The response content
74
     *
75
     * @throws PayboxException
76
     */
77
    public function call($type, array $parameters, $responseClass)
78
    {
79
        if (!in_array(ResponseInterface::class, class_implements($responseClass))) {
80
            throw new \InvalidArgumentException('The response class must implement '.ResponseInterface::class.'.');
81
        }
82
83
        $bodyParams = array_merge($parameters, $this->baseParameters);
84
        $bodyParams['TYPE'] = $type;
85
        $bodyParams['NUMQUESTION'] = $this->questionNumber;
86
        $bodyParams['DATEQ'] = null !== $parameters['DATEQ'] ? $parameters['DATEQ'] : date('dmYHis');
87
        // Restore default_currency from parameters if given
88
        if (array_key_exists('DEVISE', $parameters)) {
89
            $bodyParams['DEVISE'] = null !== $parameters['DEVISE'] ? $parameters['DEVISE'] : $this->defaultDevise;
90
        }
91
92
        $response = $this->request($bodyParams);
93
94
        // Generate results array
95
        $results = [];
96
        foreach (explode('&', $response) as $element) {
97
            list($key, $value) = explode('=', $element);
98
            $value = utf8_encode(trim($value));
99
            $results[$key] = $value;
100
        }
101
102
        $this->questionNumber = (int) $results['NUMQUESTION'] + 1;
103
104
        if ('00000' !== $results['CODEREPONSE']) {
105
            throw new PayboxException($results['COMMENTAIRE'], $results['CODEREPONSE']);
106
        }
107
108
        return new $responseClass($results);
109
    }
110
111
    /**
112
     * Init and setup http client with PayboxDirectPlus SDK options.
113
     */
114
    abstract public function init();
115
116
    /**
117
     * Sends a request to the server, receive a response and returns it as a string.
118
     *
119
     * @param string[] $parameters Request parameters
120
     *
121
     * @return string The response content
122
     */
123
    abstract protected function request($parameters);
124
}
125