GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Request::send()   D
last analyzed

Complexity

Conditions 9
Paths 15

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 4.909
c 0
b 0
f 0
cc 9
eloc 23
nc 15
nop 0
1
<?php
2
3
namespace Lexik\Bundle\PayboxBundle\Paybox\DirectPlus;
4
5
use Lexik\Bundle\PayboxBundle\Event\PayboxEvents;
6
use Lexik\Bundle\PayboxBundle\Event\PayboxResponseEvent;
7
use Lexik\Bundle\PayboxBundle\Paybox\AbstractRequest;
8
use Lexik\Bundle\PayboxBundle\Transport\TransportInterface;
9
use Psr\Log\LoggerInterface;
10
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
11
12
/**
13
 * Class Request
14
 *
15
 * @package Lexik\Bundle\PayboxBundle\Paybox\DirectPlus
16
 *
17
 * @author Lexik <[email protected]>
18
 * @author Olivier Maisonneuve <[email protected]>
19
 */
20
class Request extends AbstractRequest
21
{
22
    /**
23
     * @var LoggerInterface
24
     */
25
    private $logger;
26
27
    /**
28
     * @var EventDispatcherInterface
29
     */
30
    private $dispatcher;
31
32
    /**
33
     * @var TransportInterface
34
     */
35
    private $transport;
36
37
    /**
38
     * Constructor.
39
     *
40
     * @param array                    $parameters
41
     * @param array                    $servers
42
     * @param LoggerInterface          $logger
43
     * @param EventDispatcherInterface $dispatcher
44
     */
45
    public function __construct(array $parameters, array $servers, LoggerInterface $logger, EventDispatcherInterface $dispatcher, TransportInterface $transport)
46
    {
47
        $this->parameters = array();
48
        $this->globals    = array();
49
        $this->servers    = $servers['direct_plus'];
50
        $this->logger     = $logger;
51
        $this->dispatcher = $dispatcher;
52
        $this->transport  = $transport;
53
54
        $this->initGlobals($parameters);
55
        $this->initParameters();
56
    }
57
58
    /**
59
     * Initialize the object with the defaults values.
60
     *
61
     * @param array $parameters
62
     */
63
    protected function initGlobals(array $parameters)
64
    {
65
        $this->globals = array(
66
            'production' => isset($parameters['production']) ? $parameters['production'] : false,
67
            'currencies' => $parameters['currencies'],
68
            'site'       => $parameters['site'],
69
            'rang'       => $parameters['rang'],
70
            'cle'        => $parameters['cle'],
71
        );
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    protected function initParameters()
78
    {
79
        $this->setParameter('VERSION', null); // 'VERSION' must be the first parameter so it have to be declared first...
80
        $this->setParameter('SITE',    $this->globals['site']);
81
        $this->setParameter('RANG',    $this->globals['rang']);
82
        $this->setParameter('CLE',     $this->globals['cle']);
83
    }
84
85
    /**
86
     * Sets a parameter.
87
     *
88
     * @param  string $name
89
     * @param  mixed  $value
90
     *
91
     * @return Request
92
     */
93
    public function setParameter($name, $value)
94
    {
95
        $this->parameters[strtoupper($name)] = $value;
96
97
        return $this;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function getParameters()
104
    {
105
        $resolver = new ParameterResolver($this->globals['currencies']);
106
107
        return $resolver->resolve($this->parameters);
108
    }
109
110
    /**
111
     * @return array|false
112
     */
113
    public function send()
114
    {
115
        $this->logger->info('New API call.');
116
        $this->logger->info('Url : ' . $this->getUrl());
117
118
        $this->transport->setEndpoint($this->getUrl());
119
        $result = $this->transport->call($this);
120
121
        $this->logger->info('Data :');
122
        foreach ($this->getParameters() as $parameterName => $parameterValue) {
123
            /**
124
             * We don't want credit card's information in the server's log.
125
             */
126
            if (!in_array($parameterName, array('PORTEUR', 'DATEVAL', 'CVV'))) {
127
                $this->logger->info(sprintf(' > %s = %s', $parameterName, $parameterValue));
128
            }
129
        }
130
131
        $this->logger->info('Result : ' . $result);
132
133
        if (null === $result) {
134
            $this->logger->error('Http error.');
135
136
            return false;
137
        } else {
138
            parse_str($result, $result);
139
140
            if (isset($result['CODEREPONSE']) && isset($result['NUMTRANS']) && isset($result['NUMAPPEL'])) {
141
                $verified = ('00000' === $result['CODEREPONSE']) && !empty($result['NUMTRANS']) && !empty($result['NUMAPPEL']);
142
143
                $event = new PayboxResponseEvent($result, $verified);
144
                $this->dispatcher->dispatch(PayboxEvents::PAYBOX_API_RESPONSE, $event);
145
146
                return $result;
147
            } else {
148
                $this->logger->error('Bad response content.');
149
150
                return false;
151
            }
152
        }
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function getUrl()
159
    {
160
        $server_name = $this->globals['production'] ? 'primary' : 'preprod';
161
162
        return sprintf(
163
            '%s://%s%s',
164
            $this->servers[$server_name]['protocol'],
165
            $this->servers[$server_name]['host'],
166
            $this->servers[$server_name]['api_path']
167
        );
168
    }
169
}
170