Completed
Push — master ( 00c88c...56f5c3 )
by Rémi
13s
created

SearchResultCrawler::setUrlParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Lbc\Crawler;
4
5
use Lbc\Parser\SearchResultUrlParser;
6
7
/**
8
 * Class SearchResultCrawler
9
 * @package Lbc\node
10
 */
11
class SearchResultCrawler extends CrawlerAbstract
12
{
13
    /**
14
     * @param $url
15
     * @return SearchResultUrlParser
16
     */
17 12
    protected function setUrlParser($url)
18
    {
19 12
        $this->url = new SearchResultUrlParser($url);
20 12
    }
21
22
    /**
23
     * Return the total number of ads of the search
24
     *
25
     * @return int
26
     */
27 6
    public function getNbAds()
28
    {
29 6
        $nbAds = $this->node
30 6
            ->filter('a.tabsSwitch span.tabsSwitchNumbers')
31 6
            ->first();
32
33 6
        if ($nbAds->count()) {
34 6
            $nbAds = preg_replace('/\s+/', '', $nbAds->text());
35 6
            return (int) $nbAds;
36
        }
37
38
        return 0;
39
    }
40
41
    /**
42
     * Return the number of ads per page.
43
     *
44
     * Could be dynamically guessed in future, if Leboncoin change it frequently
45
     * Or if they add the ability for user to change it on result pages.
46
     *
47
     *
48
     * @return int
49
     */
50 6
    public function getNbAdsPerPage()
51
    {
52 6
        return 35;
53
    }
54
55
    /**
56
     * Return the number of page
57
     *
58
     * @return int
59
     */
60 6
    public function getNbPages()
61
    {
62 6
        return (int) ceil($this->getNbAds() / $this->getNbAdsPerPage());
63
    }
64
65
    /**
66
     * Get an array containing the ads of the current result page
67
     *
68
     * @return array
69
     */
70 8
    public function getAds()
71
    {
72 8
        $ads = array();
73
74 8
        $this->node->filter('[itemtype="http://schema.org/Offer"]')
75 8
            ->each(function ($node) use (&$ads) {
76 8
                $ad = (new SearchResultAdCrawler(
77 8
                    $node,
78 8
                    $node->filter('a')->attr('href')
79 8
                ))->getAll();
80
81 8
                $ads [$ad->id] = $ad;
82 8
            });
83
84 8
        return $ads;
85
    }
86
87
    /**
88
     * Return the Ads's ID only
89
     *
90
     * @return array
91
     */
92 4
    public function getAdsId()
93
    {
94 4
        return array_keys($this->getAds());
95
    }
96
}
97