Completed
Push — master ( fd4926...705397 )
by Rémi
10s
created

SearchResultCrawler::getNbAdsPerPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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