Test Failed
Push — master ( 138499...684014 )
by Dev
02:21
created

SearchViaCurl::requestGoogle()   B

Complexity

Conditions 10
Paths 145

Size

Total Lines 58
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

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

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