Test Failed
Push — main ( fa1171...07280b )
by Daniel
03:16
created

InputOutputCurl   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 3
Metric Value
eloc 50
c 7
b 0
f 3
dl 0
loc 94
rs 10
wmc 21

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUserAgent() 0 4 2
A handleSecureConnection() 0 9 3
A setPostingDetails() 0 6 2
A getContentFromUrlThroughCurl() 0 18 3
A setForceIpv4() 0 5 4
A handleCurlOptions() 0 17 3
A validateUrl() 0 7 2
A setNoBody() 0 4 2
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2019 - 2024 Daniel Popiniuc
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace danielgp\io_operations;
28
29
trait InputOutputCurl
30
{
31
32
    public function getContentFromUrlThroughCurl(string $fullURL, array $features = [])
33
    {
34
        $chanel     = curl_init();
35
        $inScopeUrl = $this->validateUrl($fullURL);
36
        $this->handleCurlOptions($chanel, $inScopeUrl, $features);
0 ignored issues
show
Bug introduced by
It seems like $chanel can also be of type resource; however, parameter $chanel of danielgp\io_operations\I...rl::handleCurlOptions() does only seem to accept CurlHandle|false, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
        $this->handleCurlOptions(/** @scrutinizer ignore-type */ $chanel, $inScopeUrl, $features);
Loading history...
37
        $aReturn    = [
38
            'response' => curl_exec($chanel),
39
            'errNo'    => curl_errno($chanel),
40
            'errMsg'   => curl_error($chanel),
41
        ];
42
        if (array_key_exists('includeStatistics', $features)) {
43
            $aReturn['info'] = curl_getinfo($chanel);
44
        }
45
        if (array_key_exists('NoBody', $features)) {
46
            unset($aReturn['response']);
47
        }
48
        curl_close($chanel);
49
        return $aReturn;
50
    }
51
52
    private function handleCurlOptions(\CurlHandle|false $chanel, string $inScopeUrl, array $features): void
53
    {
54
        $this->setUserAgent($chanel, $features);
55
        $this->handleSecureConnection($chanel, $inScopeUrl, $features);
56
        $this->setForceIpv4($chanel, $features);
57
        $this->setNoBody($chanel, $features);
58
        curl_setopt($chanel, CURLOPT_FAILONERROR, true);
0 ignored issues
show
Bug introduced by
It seems like $chanel can also be of type false; however, parameter $handle of curl_setopt() does only seem to accept CurlHandle|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        curl_setopt(/** @scrutinizer ignore-type */ $chanel, CURLOPT_FAILONERROR, true);
Loading history...
59
        curl_setopt($chanel, CURLOPT_FOLLOWLOCATION, true);
60
        curl_setopt($chanel, CURLOPT_FRESH_CONNECT, true); //avoid a cached response
61
        curl_setopt($chanel, CURLOPT_HEADER, false);
62
        curl_setopt($chanel, CURLOPT_MAXREDIRS, 10);
63
        curl_setopt($chanel, CURLOPT_RETURNTRANSFER, true);
64
        curl_setopt($chanel, CURLOPT_TCP_FASTOPEN, true);
65
        curl_setopt($chanel, CURLOPT_URL, $inScopeUrl);
66
        $this->setPostingDetails($chanel, $features);
67
        if (array_key_exists('HttpHeader', $features) && !array_key_exists('PostFields', $features)) {
68
            curl_setopt($chanel, CURLOPT_HTTPHEADER, $features['HttpHeader']);
69
        }
70
    }
71
72
    private function handleSecureConnection(\CurlHandle|false $chanel, string $fullURL, array $features): void
73
    {
74
        if ((substr(strtolower($fullURL), 0, 5) === 'https')) {
75
            $chk = false;
76
            if (array_key_exists('forceSSLverification', $features)) {
77
                $chk = true;
78
            }
79
            curl_setopt($chanel, CURLOPT_SSL_VERIFYHOST, $chk);
0 ignored issues
show
Bug introduced by
It seems like $chanel can also be of type false; however, parameter $handle of curl_setopt() does only seem to accept CurlHandle|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
            curl_setopt(/** @scrutinizer ignore-type */ $chanel, CURLOPT_SSL_VERIFYHOST, $chk);
Loading history...
80
            curl_setopt($chanel, CURLOPT_SSL_VERIFYPEER, $chk);
81
        }
82
    }
83
84
    private function setForceIpv4(\CurlHandle|false $chanel, array $features): void
85
    {
86
        if (array_key_exists('forceIpV4', $features)) {
87
            if (defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')) {
88
                curl_setopt($chanel, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
0 ignored issues
show
Bug introduced by
It seems like $chanel can also be of type false; however, parameter $handle of curl_setopt() does only seem to accept CurlHandle|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
                curl_setopt(/** @scrutinizer ignore-type */ $chanel, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
Loading history...
89
            }
90
        }
91
    }
92
93
    private function setNoBody(\CurlHandle|false $chanel, array $features): void
94
    {
95
        if (array_key_exists('NoBody', $features)) {
96
            curl_setopt($chanel, CURLOPT_NOBODY, true);
0 ignored issues
show
Bug introduced by
It seems like $chanel can also be of type false; however, parameter $handle of curl_setopt() does only seem to accept CurlHandle|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
            curl_setopt(/** @scrutinizer ignore-type */ $chanel, CURLOPT_NOBODY, true);
Loading history...
97
        }
98
    }
99
100
    private function setPostingDetails(\CurlHandle|false $chanel, array $features): void
101
    {
102
        if (array_key_exists('PostFields', $features)) {
103
            curl_setopt($chanel, CURLOPT_POST, true);
0 ignored issues
show
Bug introduced by
It seems like $chanel can also be of type false; however, parameter $handle of curl_setopt() does only seem to accept CurlHandle|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
            curl_setopt(/** @scrutinizer ignore-type */ $chanel, CURLOPT_POST, true);
Loading history...
104
            curl_setopt($chanel, CURLOPT_POSTFIELDS, $features['PostFields']);
105
            curl_setopt($chanel, CURLOPT_HTTPHEADER, $features['HttpHeader']);
106
        }
107
    }
108
109
    private function setUserAgent(\CurlHandle|false $chanel, array $features): void
110
    {
111
        if (array_key_exists('setUserAgent', $features)) {
112
            curl_setopt($chanel, CURLOPT_USERAGENT, $features['setUserAgent']);
0 ignored issues
show
Bug introduced by
It seems like $chanel can also be of type false; however, parameter $handle of curl_setopt() does only seem to accept CurlHandle|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
            curl_setopt(/** @scrutinizer ignore-type */ $chanel, CURLOPT_USERAGENT, $features['setUserAgent']);
Loading history...
113
        }
114
    }
115
116
    private function validateUrl(string $strUrl): string
117
    {
118
        $inScopeUrl = filter_var($strUrl, FILTER_VALIDATE_URL);
119
        if ($inScopeUrl === false) {
120
            throw new \Exception('Invalid URL provided...');
121
        }
122
        return $inScopeUrl;
123
    }
124
}
125