Completed
Push — master ( e8b86c...279d13 )
by Sullivan
02:12
created

Paybox::resolveRequestParameters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 54
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 54
rs 9.6716
cc 2
eloc 42
nc 2
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Nexy\PayboxDirect;
4
5
use Nexy\PayboxDirect\Enum\Activity;
6
use Nexy\PayboxDirect\Enum\Currency;
7
use Nexy\PayboxDirect\HttpClient\AbstractHttpClient;
8
use Nexy\PayboxDirect\HttpClient\GuzzleHttpClient;
9
use Nexy\PayboxDirect\OptionsResolver\OptionsResolver;
10
use Nexy\PayboxDirect\Request\RequestInterface;
11
use Nexy\PayboxDirect\Response\PayboxResponse;
12
13
/**
14
 * @author Sullivan Senechal <[email protected]>
15
 *
16
 * @see http://www1.paybox.com/espace-integrateur-documentation/les-solutions-paybox-direct-et-paybox-direct-plus/les-operations-de-caisse-direct-plus/
17
 * @see http://www1.paybox.com/espace-integrateur-documentation/dictionnaire-des-donnees/paybox-direct-et-direct-plus/
18
 */
19
final class Paybox
20
{
21
    const VERSION_DIRECT = '00103';
22
    const VERSION_DIRECT_PLUS = '00104';
23
24
    const VERSIONS = [
25
        'direct' => self::VERSION_DIRECT,
26
        'direct_plus' => self::VERSION_DIRECT_PLUS,
27
    ];
28
29
    const API_URL_PRODUCTION = 'https://ppps.paybox.com/PPPS.php';
30
    const API_URL_RESCUE = 'https://ppps1.paybox.com/PPPS.php';
31
    const API_URL_TEST = 'https://preprod-ppps.paybox.com/PPPS.php';
32
33
    /**
34
     * @var AbstractHttpClient
35
     */
36
    private $httpClient;
37
38
    /**
39
     * @var array
40
     */
41
    private $options;
42
43
    public function __construct(array $options = [], AbstractHttpClient $httpClient = null)
44
    {
45
        $resolver = new OptionsResolver();
46
        $this->configureOptions($resolver);
47
48
        $this->options = $resolver->resolve($options);
49
50
        $this->httpClient = $httpClient ? $httpClient : new GuzzleHttpClient();
51
        $this->httpClient->setOptions($this->options);
52
        $this->httpClient->init();
53
    }
54
55
    /**
56
     * @param RequestInterface $request
57
     *
58
     * @return PayboxResponse
59
     *
60
     * @throws Exception\PayboxException
61
     */
62
    public function request(RequestInterface $request)
63
    {
64
        return $this->httpClient->call(
65
            $request->getRequestType(),
66
            $this->resolveRequestParameters($request->getParameters())
67
        );
68
    }
69
70
    /**
71
     * Paybox base options validation.
72
     *
73
     * @param OptionsResolver $resolver
74
     */
75
    private function configureOptions(OptionsResolver $resolver)
76
    {
77
        $resolver->setDefaults([
78
            'timeout' => 10,
79
            'production' => false,
80
            'paybox_default_currency' => Currency::EURO,
81
        ]);
82
        $resolver->setRequired([
83
            'paybox_version', // Paybox Direct Plus protocol
84
            'paybox_site',
85
            'paybox_rank',
86
            'paybox_identifier',
87
            'paybox_key',
88
        ]);
89
90
        $resolver->setAllowedTypes('timeout', 'int');
91
        $resolver->setAllowedTypes('production', 'bool');
92
        $resolver->setAllowedTypes('paybox_version', 'string');
93
        $resolver->setAllowedTypes('paybox_default_currency', 'int');
94
        $resolver->setAllowedTypes('paybox_site', 'string');
95
        $resolver->setAllowedTypes('paybox_rank', 'string');
96
        $resolver->setAllowedTypes('paybox_identifier', 'string');
97
        $resolver->setAllowedTypes('paybox_key', 'string');
98
99
        $resolver->setAllowedValues('paybox_version', static::VERSIONS);
100
    }
101
102
    /**
103
     * Paybox request paramaters validation.
104
     *
105
     * @param array $parameters
106
     *
107
     * @return array
108
     */
109
    private function resolveRequestParameters(array $parameters)
110
    {
111
        $resolver = new OptionsResolver();
112
113
        // Defines parameters keys to enable them.
114
        foreach (array_keys($parameters) as $key) {
115
            $resolver->setDefined($key);
116
        }
117
118
        $resolver
1 ignored issue
show
Bug introduced by
The method setAllowedTypesIfDefined() does not exist on Symfony\Component\OptionsResolver\OptionsResolver. Did you maybe mean setAllowedTypes()?

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...
119
            ->setAllowedTypesIfDefined('ACQUEREUR', 'string')
120
            ->setAllowedTypesIfDefined('ACTIVITE', 'int')
121
            ->setAllowedTypesIfDefined('ARCHIVAGE', 'string')
122
            ->setAllowedTypesIfDefined('AUTORISATION', 'string')
123
            ->setAllowedTypesIfDefined('CVV', 'string')
124
            ->setAllowedTypesIfDefined('DATENAISS', 'string')
125
            ->setAllowedTypesIfDefined('DATEQ', ['string', 'null'])
126
            ->setAllowedTypesIfDefined('DATEVAL', 'string')
127
            ->setAllowedTypesIfDefined('DEVISE', ['int', 'null'])
128
            ->setAllowedTypesIfDefined('DIFFERE', 'int')
129
            ->setAllowedTypesIfDefined('ERRORCODETEST', 'int')
130
            ->setAllowedTypesIfDefined('ID3D', 'string')
131
            ->setAllowedTypesIfDefined('MONTANT', 'int')
132
            ->setAllowedTypesIfDefined('NUMAPPEL', 'int')
133
            ->setAllowedTypesIfDefined('NUMTRANS', 'int')
134
            ->setAllowedTypesIfDefined('PORTEUR', 'string')
135
            ->setAllowedTypesIfDefined('PRIV_CODETRAITEMENT', 'string')
136
            ->setAllowedTypesIfDefined('REFABONNE', 'string')
137
            ->setAllowedTypesIfDefined('REFERENCE', 'string') // TODO: Auto-generated if not provided?
138
        ;
139
140
        $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...
141
            ->setAllowedValuesIfDefined('ACQUEREUR', ['PAYPAL', 'EMS', 'ATOSBE', 'BCMC', 'PSC', 'FINAREF', 'BUYSTER', '34ONEY'])
142
            ->setAllowedValuesIfDefined('ACTIVITE', [
143
                Activity::NOT_SPECIFIED,
144
                Activity::PHONE_REQUEST,
145
                Activity::MAIL_REQUEST,
146
                Activity::MINITEL_REQUEST,
147
                Activity::WEB_REQUEST,
148
                Activity::RECURRING_PAYMENT,
149
            ])
150
            ->setAllowedValuesIfDefined('DEVISE', [
151
                null,
152
                Currency::EURO,
153
                Currency::US_DOLLAR,
154
                Currency::CFA,
155
            ])
156
            ->setAllowedValuesIfDefined('PAYS', '')
157
            ->setAllowedValuesIfDefined('SHA-1', '')
158
            ->setAllowedValuesIfDefined('TYPECARTE', '')
159
        ;
160
161
        return $resolver->resolve($parameters);
162
    }
163
}
164