Completed
Pull Request — master (#79)
by ARCANEDEV
04:20
created

Request::interpretResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 10
ccs 4
cts 4
cp 1
cc 2
nc 2
nop 1
crap 2
1
<?php namespace Arcanedev\NoCaptcha\Utilities;
2
3
use Arcanedev\NoCaptcha\Contracts\Utilities\Request as RequestContract;
4
use Arcanedev\NoCaptcha\Exceptions\InvalidUrlException;
5
6
/**
7
 * Class     Request
8
 *
9
 * @package  Arcanedev\NoCaptcha\Utilities
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class Request implements RequestContract
13
{
14
    /* -----------------------------------------------------------------
15
     |  Properties
16
     | -----------------------------------------------------------------
17
     */
18
19
    /**
20
     * URL to request.
21
     *
22
     * @var string
23
     */
24
    protected $url;
25
26
    /* -----------------------------------------------------------------
27
     |  Getters & Setters
28
     | -----------------------------------------------------------------
29
     */
30
31
    /**
32
     * Set URL.
33
     *
34
     * @param  string  $url
35
     *
36
     * @return self
37
     */
38 20
    protected function setUrl($url)
39
    {
40 20
        $this->checkUrl($url);
41
42 8
        $this->url = $url;
43
44 8
        return $this;
45
    }
46
47
    /* -----------------------------------------------------------------
48
     |  Main Methods
49
     | -----------------------------------------------------------------
50
     */
51
52
    /**
53
     * Create an api request using curl.
54
     *
55
     * @return string
56
     */
57 8
    protected function curl()
58
    {
59 8
        $curl    = curl_init();
60 8
        curl_setopt_array($curl, [
61 8
            CURLOPT_URL            => $this->url,
62 8
            CURLOPT_RETURNTRANSFER => true,
63 8
            CURLOPT_SSL_VERIFYPEER => false,
64
        ]);
65 8
        $result = curl_exec($curl);
66 8
        curl_close($curl);
67
68 8
        return $result;
69
    }
70
71
    /**
72
     * Run the request and get response.
73
     *
74
     * @param  string  $url
75
     * @param  bool    $curled
76
     *
77
     * @return string
78
     */
79 20
    public function send($url, $curled = true)
80
    {
81 20
        $this->setUrl($url);
82
83 8
        $result = ($this->isCurlExists() && $curled === true)
84 8
            ? $this->curl()
85 8
            : file_get_contents($this->url);
86
87 8
        return $this->checkResult($result) ? $result : '{}';
88
    }
89
90
    /* -----------------------------------------------------------------
91
     |  Check Methods
92
     | -----------------------------------------------------------------
93
     */
94
95
    /**
96
     * Check URL.
97
     *
98
     * @param  string  $url
99
     *
100
     * @throws \Arcanedev\NoCaptcha\Exceptions\InvalidUrlException
101
     */
102 20
    private function checkUrl(&$url)
103
    {
104 20
        if ( ! is_string($url))
105 4
            throw new InvalidUrlException(
106 4
                'The url must be a string value, ' . gettype($url) . ' given'
107
            );
108
109 16
        $url = trim($url);
110
111 16
        if (empty($url))
112 4
            throw new InvalidUrlException('The url must not be empty');
113
114 12
        if (filter_var($url, FILTER_VALIDATE_URL) === false)
115 4
            throw new InvalidUrlException('The url [' . $url . '] is invalid');
116 8
    }
117
118
    /**
119
     * Check if curl exists.
120
     *
121
     * @return bool
122
     */
123 8
    private function isCurlExists()
124
    {
125 8
        return function_exists('curl_version');
126
    }
127
128
    /**
129
     * Check Result.
130
     *
131
     * @param  string  $result
132
     *
133
     * @return bool
134
     */
135 8
    private function checkResult($result)
136
    {
137 8
        return is_string($result) && ! empty($result);
138
    }
139
}
140