Qwanturank::compare()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 11
c 2
b 1
f 0
nc 2
nop 1
dl 0
loc 18
ccs 0
cts 13
cp 0
crap 6
rs 9.9
1
<?php
2
3
namespace PiedWeb\Qwanturank;
4
5
use rOpenDev\Google\SearchViaCurl;
6
use rOpenDev\Qwant\QwantSearchViaCurl;
7
8
class Qwanturank
9
{
10
    protected $googleResults;
11
    protected $qwantResults;
12
    protected $kw;
13
14
    public static function compare($kw = 'qwanturank')
15
    {
16
        $qwanturank = new self($kw);
17
18
        $results = [];
19
        $position = 0;
20
        foreach ($qwanturank->getQwantResults() as $item) {
21
            ++$position;
22
23
            $results[] = [
24
                'title' => $item['title'],
25
                'url' => $item['link'],
26
                'google' => $qwanturank->getGooglePositionFor($item['link']),
27
                'position' => $position,
28
            ];
29
        }
30
31
        return $results;
32
    }
33
34
    public function __construct($kw)
35
    {
36
        $this->kw = $kw;
37
    }
38
39
    public function getGooglePositionFor($url)
40
    {
41
        foreach ($this->getGoogleResults() as $k => $item) {
42
            if ($item['link'] == $url) {
43
                return $k + 1;
44
            }
45
        }
46
47
        return null;
48
    }
49
50
    public function getGoogleResults()
51
    {
52
        if ($this->googleResults) {
53
            return $this->googleResults;
54
        }
55
56
        $Google = new SearchViaCurl($this->kw);
57
58
        $Google->setTld('fr')
59
                 ->setLanguage('fr')
60
                 ->setCacheFolder(null) // to disable storing in the /tmp folder
61
                 ->setNbrPage(1) // Nbr de page à extraire
62
                 ->setParameter('num', 100) // to add a parameter in the search url
63
        ;
64
65
        $this->googleResults = $Google->extractResults();
66
67
        return $this->googleResults;
68
    }
69
70
    public function getQwantResults()
71
    {
72
        if ($this->qwantResults) {
73
            return $this->qwantResults;
74
        }
75
76
        $Qwant = new QwantSearchViaCurl($this->kw);
77
78
        $Qwant->setLanguage('fr')
79
                 ->setSleep(6)  // to wait between 2 requests on Qwant
80
                 ->setNbrPage(10) // Nbr de page à extraire
81
                 ->setCacheFolder(null)
82
        ;
83
84
        $this->qwantResults = $Qwant->extractResults();
85
86
        return $this->qwantResults;
87
    }
88
}
89