Completed
Push — master ( fa4061...e2ec10 )
by Sullivan
02:18
created

AbstractHttpClient::setOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 13
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
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\PayboxResponse;
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_cle']) 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_rang'],
60
            'IDENTIFIANT' => $options['paybox_identifiant'],
61
            'CLE' => $options['paybox_cle'],
62
        ];
63
        $this->defaultDevise = $options['paybox_devise'];
64
    }
65
66
    /**
67
     * Calls PayBox Direct platform with given operation type and parameters.
68
     *
69
     * @param string   $type       Request type
70
     * @param string[] $parameters Request parameters
71
     *
72
     * @return PayboxResponse The response content
73
     *
74
     * @throws PayboxException
75
     */
76
    final public function call($type, array $parameters)
77
    {
78
        $bodyParams = array_merge($parameters, $this->baseParameters);
79
        $bodyParams['TYPE'] = $type;
80
        $bodyParams['NUMQUESTION'] = $this->questionNumber;
81
        $bodyParams['DATEQ'] = null !== $parameters['DATEQ'] ? $parameters['DATEQ'] : date('dmYHis');
82
        // Restore devise from parameters if given
83
        if (array_key_exists('DEVISE', $parameters)) {
84
            $bodyParams['DEVISE'] = null !== $parameters['DEVISE'] ? $parameters['DEVISE'] : $this->defaultDevise;
85
        }
86
87
        $response = $this->request($bodyParams);
88
89
        // Generate results array
90
        $results = [];
91
        foreach (explode('&', $response) as $element) {
92
            list($key, $value) = explode('=', $element);
93
            $value = utf8_encode(trim($value));
94
            $results[$key] = $value;
95
        }
96
97
        $this->questionNumber = (int) $results['NUMQUESTION'] + 1;
98
99
        if ('00000' !== $results['CODEREPONSE']) {
100
            throw new PayboxException($results['COMMENTAIRE'], $results['CODEREPONSE']);
101
        }
102
103
        return new PayboxResponse($results);
104
    }
105
106
    /**
107
     * Init and setup http client with PayboxDirectPlus SDK options.
108
     */
109
    abstract public function init();
110
111
    /**
112
     * Sends a request to the server, receive a response and returns it as a string.
113
     *
114
     * @param string[] $parameters Request parameters
115
     *
116
     * @return string The response content
117
     */
118
    abstract protected function request($parameters);
119
}
120