Geocode::getCurlConf()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 15
rs 10
1
<?php
2
3
4
namespace talismanfr\geocode;
5
6
7
use talismanfr\geocode\exeptions\ProxyConfException;
8
use yii\helpers\ArrayHelper;
9
10
class Geocode implements \talismanfr\geocode\contracts\Geocode
11
{
12
13
    public $url = 'https://geocode-maps.yandex.ru/1.x/';
14
15
    public $apikey = null;
16
17
    public $useProxy = false;
18
19
    public $format = 'json';
20
21
    public $proxyConf = [
22
        'url' => '127.0.0.1',
23
        'port' => '3128',
24
        'login' => null,
25
        'password' => null
26
    ];
27
28
    /**
29
     * @param $query
30
     * @param array $params
31
     * @return string
32
     * @throws ProxyConfException
33
     */
34
    public function get($query, $params = []): string
35
    {
36
        $params['geocode'] = $query;
37
38
        $url = $this->buildUrl($params);
39
40
        $curl = curl_init();
41
        curl_setopt($curl, CURLOPT_URL, $url);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept 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

41
        curl_setopt(/** @scrutinizer ignore-type */ $curl, CURLOPT_URL, $url);
Loading history...
42
43
        curl_setopt_array($curl, $this->getCurlConf());
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept 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

43
        curl_setopt_array(/** @scrutinizer ignore-type */ $curl, $this->getCurlConf());
Loading history...
44
45
        $response = curl_exec($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_exec() does only seem to accept 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

45
        $response = curl_exec(/** @scrutinizer ignore-type */ $curl);
Loading history...
46
47
        return (string)$response;
48
    }
49
50
    /**
51
     * @return resource A stream context resource.
52
     * @throws ProxyConfException
53
     */
54
    private function getCurlConf(): array
55
    {
56
        $conf = [
57
            CURLOPT_RETURNTRANSFER => true,
58
            CURLOPT_HEADER => false,
59
            CURLOPT_FOLLOWLOCATION => false,
60
            CURLOPT_VERBOSE => false,
61
        ];
62
63
        if ($this->useProxy) {
64
65
            $conf = ArrayHelper::merge($conf, $this->buildProxyConf());
66
        }
67
68
        return $conf;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $conf returns the type array|array<integer,boolean> which is incompatible with the documented return type resource.
Loading history...
69
    }
70
71
    /**
72
     * Build array conf proxy for stream context
73
     * @return array
74
     * @throws ProxyConfException
75
     */
76
    private function buildProxyConf(): array
77
    {
78
        $url = ArrayHelper::getValue($this->proxyConf, 'url');
79
        if (empty($url)) {
80
            throw new ProxyConfException('url param is empty');
81
        }
82
        $port = ArrayHelper::getValue($this->proxyConf, 'port', '3128');
83
84
        $conf = [CURLOPT_PROXY => $url, CURLOPT_PROXYPORT => $port];
85
86
        $login = ArrayHelper::getValue($this->proxyConf, 'login');
87
        $password = ArrayHelper::getValue($this->proxyConf, 'password');
88
89
        if (!empty($login)) {
90
            $conf[CURLOPT_PROXYUSERNAME] = $login;
91
            $conf[CURLOPT_PROXYPASSWORD] = $password;
92
        }
93
94
        return $conf;
95
    }
96
97
    /**
98
     * Generate Url
99
     * @param $params
100
     * @return string
101
     */
102
    private function buildUrl(array $params)
103
    {
104
        $params['format'] = $this->format;
105
106
        if (!empty($this->apikey)) {
107
            $params['apikey'] = $this->apikey;
108
        }
109
110
        return $this->url . '?' . http_build_query($params);
111
    }
112
}