CurlClient   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 83
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A addOption() 0 6 1
B hit() 0 27 4
1
<?php
2
3
namespace Akibatech\FreeMobileSms;
4
5
/**
6
 * Class CurlClient
7
 *
8
 * @package Akibatech\FreeMobileSms
9
 */
10
class CurlClient
11
{
12
    /**
13
     * @var resource cURL handle
14
     */
15
    private $resource;
16
17
    /**
18
     * @var int
19
     */
20
    private $response;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param   string $url
26
     * @param   array  $options
27
     * @return  self
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
28
     */
29
    public function __construct($url, array $options = [])
30
    {
31
        $this->resource = curl_init();
32
33
        $query = http_build_query($options);
34
        $url .= '?' . $query;
35
36
        $this->addOption(CURLOPT_URL, $url);
37
        $this->addOption(CURLOPT_RETURNTRANSFER, false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
38
    }
39
40
    //-------------------------------------------------------------------------
41
42
    /**
43
     * Add an option.
44
     *
45
     * @param   string $key
46
     * @param   string $value
47
     * @return  $this
48
     */
49
    public function addOption($key, $value)
50
    {
51
        curl_setopt($this->resource, $key, $value);
52
53
        return $this;
54
    }
55
56
    //-------------------------------------------------------------------------
57
58
    /**
59
     * Get the response
60
     *
61
     * @param   void
62
     * @return  int
63
     * @throws \RuntimeException On cURL error
64
     */
65
    public function hit()
66
    {
67
        if (is_null($this->response) === false)
68
        {
69
            return $this->response;
70
        }
71
72
        curl_exec($this->resource);
73
        $error = curl_error($this->resource);
74
        $errno = curl_errno($this->resource);
75
76
        $response = (int)curl_getinfo($this->resource, CURLINFO_HTTP_CODE);
77
78
        if (is_resource($this->resource))
79
        {
80
            curl_close($this->resource);
81
        }
82
83
        if (0 !== $errno)
84
        {
85
            throw new \RuntimeException($error, $errno);
86
        }
87
88
        $this->response = $response;
89
90
        return $response;
91
    }
92
}
93