Completed
Push — master ( 838849...00c88c )
by Rémi
10s
created

SearchResultCrawler::getNbAds()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

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