Completed
Pull Request — master (#32)
by Sullivan
02:36
created

Paybox::requestDirectPlus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Nexy\PayboxDirect;
4
5
use Nexy\PayboxDirect\Enum\Activity;
6
use Nexy\PayboxDirect\Enum\Currency;
7
use Nexy\PayboxDirect\Enum\Receiver;
8
use Nexy\PayboxDirect\Enum\Version;
9
use Nexy\PayboxDirect\HttpClient\AbstractHttpClient;
10
use Nexy\PayboxDirect\HttpClient\GuzzleHttpClient;
11
use Nexy\PayboxDirect\OptionsResolver\OptionsResolver;
12
use Nexy\PayboxDirect\Request\RequestInterface;
13
use Nexy\PayboxDirect\Response\DirectPlusResponse;
14
use Nexy\PayboxDirect\Response\DirectResponse;
15
16
/**
17
 * @author Sullivan Senechal <[email protected]>
18
 *
19
 * @see http://www1.paybox.com/espace-integrateur-documentation/les-solutions-paybox-direct-et-paybox-direct-plus/les-operations-de-caisse-direct-plus/
20
 * @see http://www1.paybox.com/espace-integrateur-documentation/dictionnaire-des-donnees/paybox-direct-et-direct-plus/
21
 */
22
final class Paybox
23
{
24
    const API_URL_PRODUCTION = 'https://ppps.paybox.com/PPPS.php';
25
    const API_URL_RESCUE = 'https://ppps1.paybox.com/PPPS.php';
26
    const API_URL_TEST = 'https://preprod-ppps.paybox.com/PPPS.php';
27
28
    /**
29
     * @var AbstractHttpClient
30
     */
31
    private $httpClient;
32
33
    /**
34
     * @var array
35
     */
36
    private $options;
37
38
    public function __construct(array $options = [], AbstractHttpClient $httpClient = null)
39
    {
40
        $resolver = new OptionsResolver();
41
        $this->configureOptions($resolver);
42
43
        $this->options = $resolver->resolve($options);
44
45
        $this->httpClient = $httpClient ? $httpClient : new GuzzleHttpClient();
46
        $this->httpClient->setOptions($this->options);
47
        $this->httpClient->init();
48
    }
49
50
    /**
51
     * @param RequestInterface $request
52
     *
53
     * @return DirectResponse
54
     */
55 View Code Duplication
    public function requestDirect(RequestInterface $request)
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...
56
    {
57
        if ($request->getRequestType() >= RequestInterface::SUBSCRIBER_AUTHORIZE) {
58
            throw new \InvalidArgumentException(
59
                'Direct Plus requests must be passed onto '.__CLASS__.'::requestDirectPlus method.'
60
            );
61
        }
62
63
        return $this->request($request);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->request($request); of type Nexy\PayboxDirect\Respon...Response\DirectResponse adds the type Nexy\PayboxDirect\Response\DirectPlusResponse to the return on line 63 which is incompatible with the return type documented by Nexy\PayboxDirect\Paybox::requestDirect of type Nexy\PayboxDirect\Response\DirectResponse.
Loading history...
64
    }
65
66
    /**
67
     * @param RequestInterface $request
68
     *
69
     * @return DirectPlusResponse
70
     */
71 View Code Duplication
    public function requestDirectPlus(RequestInterface $request)
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...
72
    {
73
        if ($request->getRequestType() < RequestInterface::SUBSCRIBER_AUTHORIZE) {
74
            throw new \InvalidArgumentException(
75
                'Direct requests must be passed onto '.__CLASS__.'::requestDirect method.'
76
            );
77
        }
78
79
        return $this->request($request, true);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->request($request, true); of type Nexy\PayboxDirect\Respon...Response\DirectResponse adds the type Nexy\PayboxDirect\Response\DirectResponse to the return on line 79 which is incompatible with the return type documented by Nexy\PayboxDirect\Paybox::requestDirectPlus of type Nexy\PayboxDirect\Response\DirectPlusResponse.
Loading history...
80
    }
81
82
    /**
83
     * @param RequestInterface $request
84
     * @param bool             $directPlus
85
     *
86
     * @return DirectResponse|DirectPlusResponse
87
     *
88
     * @throws Exception\PayboxException
89
     */
90
    private function request(RequestInterface $request, $directPlus = false)
91
    {
92
        return $this->httpClient->call(
93
            $request->getRequestType(),
94
            $this->resolveRequestParameters($request->getParameters()),
95
            $directPlus
96
        );
97
    }
98
99
    /**
100
     * Paybox base options validation.
101
     *
102
     * @param OptionsResolver $resolver
103
     */
104
    private function configureOptions(OptionsResolver $resolver)
105
    {
106
        $resolver->setDefaults([
107
            'timeout' => 10,
108
            'production' => false,
109
            'paybox_default_currency' => Currency::EURO,
110
        ]);
111
        $resolver->setRequired([
112
            'paybox_version', // Paybox Direct Plus protocol
113
            'paybox_site',
114
            'paybox_rank',
115
            'paybox_identifier',
116
            'paybox_key',
117
        ]);
118
119
        $resolver->setAllowedTypes('timeout', 'int');
120
        $resolver->setAllowedTypes('production', 'bool');
121
        $resolver->setAllowedTypes('paybox_version', 'string');
122
        $resolver->setAllowedTypes('paybox_default_currency', 'int');
123
        $resolver->setAllowedTypes('paybox_site', 'string');
124
        $resolver->setAllowedTypes('paybox_rank', 'string');
125
        $resolver->setAllowedTypes('paybox_identifier', 'string');
126
        $resolver->setAllowedTypes('paybox_key', 'string');
127
128
        $resolver->setAllowedValues('paybox_version', Version::getConstants());
129
    }
130
131
    /**
132
     * Paybox request paramaters validation.
133
     *
134
     * @param array $parameters
135
     *
136
     * @return array
137
     */
138
    private function resolveRequestParameters(array $parameters)
139
    {
140
        $resolver = new OptionsResolver();
141
142
        // Defines parameters keys to enable them.
143
        foreach (array_keys($parameters) as $key) {
144
            $resolver->setDefined($key);
145
        }
146
147
        $resolver
148
            ->setAllowedTypesIfDefined('ACQUEREUR', 'string')
149
            ->setAllowedTypesIfDefined('ACTIVITE', 'int')
150
            ->setAllowedTypesIfDefined('ARCHIVAGE', 'string')
151
            ->setAllowedTypesIfDefined('AUTORISATION', 'string')
152
            ->setAllowedTypesIfDefined('CVV', 'string')
153
            ->setAllowedTypesIfDefined('DATENAISS', 'string')
154
            ->setAllowedTypesIfDefined('DATEQ', ['string', 'null'])
155
            ->setAllowedTypesIfDefined('DATEVAL', 'string')
156
            ->setAllowedTypesIfDefined('DEVISE', ['int', 'null'])
157
            ->setAllowedTypesIfDefined('DIFFERE', 'int')
158
            ->setAllowedTypesIfDefined('ERRORCODETEST', 'int')
159
            ->setAllowedTypesIfDefined('ID3D', 'string')
160
            ->setAllowedTypesIfDefined('MONTANT', 'int')
161
            ->setAllowedTypesIfDefined('NUMAPPEL', 'int')
162
            ->setAllowedTypesIfDefined('NUMTRANS', 'int')
163
            ->setAllowedTypesIfDefined('PORTEUR', 'string')
164
            ->setAllowedTypesIfDefined('PRIV_CODETRAITEMENT', 'string')
165
            ->setAllowedTypesIfDefined('REFABONNE', 'string')
166
            ->setAllowedTypesIfDefined('REFERENCE', 'string') // TODO: Auto-generated if not provided?
167
        ;
168
169
        $resolver
0 ignored issues
show
Bug introduced by
The method setAllowedValuesIfDefined() does not exist on Symfony\Component\OptionsResolver\OptionsResolver. Did you maybe mean setAllowedValues()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
170
            ->setAllowedValuesIfDefined('ACQUEREUR', Receiver::getConstants())
171
            ->setAllowedValuesIfDefined('ACTIVITE', Activity::getConstants())
172
            ->setAllowedValuesIfDefined('DEVISE', array_merge([null], Currency::getConstants()))
173
            ->setAllowedValuesIfDefined('PAYS', '')
174
            ->setAllowedValuesIfDefined('SHA-1', '')
175
            ->setAllowedValuesIfDefined('TYPECARTE', '')
176
        ;
177
178
        return $resolver->resolve($parameters);
179
    }
180
}
181