Completed
Push — master ( 5c7dc1...da075f )
by Dev
03:25
created

QwantSearchViaCurl::requestGoogle()   B

Complexity

Conditions 10
Paths 145

Size

Total Lines 58
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 10
eloc 34
c 1
b 0
f 1
nc 145
nop 1
dl 0
loc 58
rs 7.2916

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace rOpenDev\Qwant;
4
5
use rOpenDev\curl\CurlRequest;
6
7
class QwantSearchViaCurl extends QwantSearch
8
{
9
    protected $referrer;
10
    protected $cookie;
11
12
    /**
13
     * @return string|false Contenu html de la page
14
     */
15
    protected function requestGoogle(string $url)
16
    {
17
        $cache = $this->getCache($url);
18
        if (false !== $cache) {
19
            return $cache;
20
        }
21
22
        $curl = new CurlRequest($url);
23
        $curl->setDefaultGetOptions()->setReturnHeader()->setEncodingGzip();
24
25
        if (isset($this->language)) {
26
            $curl->setOpt(CURLOPT_HTTPHEADER, ['Accept-Language: '.$this->language]);
27
        }
28
        if (isset($this->userAgent)) {
29
            $curl->setUserAgent($this->userAgent);
30
        } else {
31
            if ($this->mobile) {
32
                $curl->setDestkopUserAgent();
33
            } else {
34
                $curl->setMobileUserAgent();
35
            }
36
        }
37
38
        if (isset($this->proxy)) {
39
            $curl->setProxy($this->proxy);
40
        }
41
        if (isset($this->referrer)) {
42
            $curl->setReferrer($this->referrer);
43
        }
44
        if (isset($this->cookie)) {
45
            $curl->setCookie($this->cookie);
46
        }
47
48
        $output = $curl->execute();
49
50
        /* Erreur lors de l'éxecution du cURL **/
51
        if ($curl->hasError()) {
52
            $this->cErrors = $curl->getErrors();
53
            $this->error = 1;
54
55
            return false;
56
        }
57
58
        $amIKicked = $this->amIKickedByGoogleThePowerful($output);
59
        if (false !== $amIKicked) {
0 ignored issues
show
introduced by
The condition false !== $amIKicked is always false.
Loading history...
60
            $this->error = $amIKicked;
61
62
            return false;
63
        }
64
65
        /* Tout est Ok, on enregistre et on renvoit le html **/
66
        $this->setCache($url, $output);
67
68
        $this->cookie = $curl->getCookies();
69
        $this->referrer = $curl->getEffectiveUrl();
70
        $this->execSleep();
71
72
        return $output;
73
    }
74
}
75