Curl   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%
Metric Value
dl 0
loc 141
wmc 16
lcom 1
cbo 1
ccs 0
cts 87
cp 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setTimeout() 0 4 1
A setLogger() 0 4 1
B get() 0 46 5
A debug() 0 6 2
A logTransferTime() 0 6 2
A logResponseError() 0 11 2
A error() 0 6 2
1
<?php
2
namespace Fmaj\LaposteDatanovaBundle\Client;
3
4
use Exception;
5
use Psr\Log\LoggerInterface;
6
7
class Curl implements ClientInterface
8
{
9
    /** @var string */
10
    protected $server;
11
12
    /** @var string */
13
    protected $version;
14
15
    /** @var LoggerInterface */
16
    protected $logger;
17
18
    /** @var float */
19
    protected $timeout;
20
21
    /**
22
     * @param string $server
23
     * @param string $apiVersion
24
     */
25
    public function __construct($server, $apiVersion)
26
    {
27
        $this->server = $server;
28
        $this->version = $apiVersion;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function setTimeout($timeout)
35
    {
36
        $this->timeout = $timeout;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function setLogger(LoggerInterface $logger)
43
    {
44
        $this->logger = $logger;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function get($operation, $parameters = array(), $data = 'records')
51
    {
52
        $this->debug(sprintf('%s %s', $operation, $data), $parameters);
53
        $result = null;
54
        $url = sprintf(
55
            '%s/api/%s/%s/%s/?%s',
56
            $this->server,
57
            $data,
58
            $this->version,
59
            $operation,
60
            http_build_query($parameters)
61
        );
62
        $curl = curl_init();
63
        curl_setopt_array($curl, array(
64
            CURLOPT_RETURNTRANSFER => 1,
65
            CURLOPT_URL => $url,
66
        ));
67
        if (isset($this->timeout)) {
68
            curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
69
        }
70
        try {
71
            $response = curl_exec($curl);
72
            if (!$response) {
73
                $this->error(
74
                    'Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl) . ' - Url: ' . $url,
75
                    $parameters
76
                );
77
            } else {
78
                $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
79
                $time = curl_getinfo($curl, CURLINFO_TOTAL_TIME);
80
                $this->logTransferTime($time);
81
                if (200 === $status) {
82
                    $result = $response;
83
                } else {
84
                    $this->debug('Target url:  ' . $url);
85
                    $this->logResponseError($status, $response, $parameters);
86
                }
87
            }
88
            curl_close($curl);
89
        } catch (Exception $exception) {
90
            $this->debug($exception->getTraceAsString());
91
            $this->error($exception->getMessage());
92
        }
93
94
        return $result;
95
    }
96
97
    /**
98
     * @param string $message
99
     * @param array $context
100
     */
101
    private function debug($message, $context = array())
102
    {
103
        if ($this->logger) {
104
            $this->logger->debug($message, $context);
105
        }
106
    }
107
108
    /**
109
     * @param string $time
110
     *
111
     * @return array
112
     */
113
    private function logTransferTime($time)
114
    {
115
        if ($this->logger) {
116
            $this->logger->debug(sprintf('Transfer time: %.3f sec', $time));
117
        }
118
    }
119
120
    /**
121
     * @param int $status
122
     * @param string $response
123
     * @param array $parameters
124
     */
125
    private function logResponseError($status, $response, $parameters)
126
    {
127
        if ($this->logger) {
128
            $log = sprintf(
129
                '%d: %s',
130
                $status,
131
                $response
132
            );
133
            $this->logger->error($log, $parameters);
134
        }
135
    }
136
137
    /**
138
     * @param string $message
139
     * @param array $context
140
     */
141
    private function error($message, $context = array())
142
    {
143
        if ($this->logger) {
144
            $this->logger->error($message, $context);
145
        }
146
    }
147
}
148