Passed
Push — master ( 88138d...af6eb6 )
by Rougin
01:54
created

Crawler::items()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Pilipinews\Website\Sunstar;
4
5
use Pilipinews\Common\Crawler as DomCrawler;
6
use Pilipinews\Common\Interfaces\CrawlerInterface;
7
8
/**
9
 * Sunstar News Crawler
10
 *
11
 * @package Pilipinews
12
 * @author  Rougin Royce Gutib <[email protected]>
13
 */
14
class Crawler implements CrawlerInterface
15
{
16
    /**
17
     * @var string[]
18
     */
19
    protected $pages = array(
20
        'https://www.sunstar.com.ph/morearticles/Manila/Local-news',
21
        'https://www.sunstar.com.ph/morearticles/Cebu/Local-news',
22
        'https://www.sunstar.com.ph/morearticles/Davao/Local-news',
23
    );
24
25
    /**
26
     * Returns an array of articles to scrape.
27
     *
28
     * @return string[]
29
     */
30 3
    public function crawl()
31
    {
32 3
        $result = array();
33
34 3
        foreach ((array) $this->pages as $page) {
35 3
            $items = $this->items((string) $page);
36
37 3
            $result = array_merge($result, $items);
38 2
        }
39
40 3
        return $result;
41
    }
42
43
    /**
44
     * Returns an array of articles to scrape.
45
     *
46
     * @param  string $link
47
     * @return string[]
48
     */
49 3
    protected function items($link)
50
    {
51 3
        $crawler = new DomCrawler(Client::request($link));
52
53 3
        $pattern = '.search-inner > .outer-content';
54
55 3
        $news = $crawler->filter((string) $pattern);
56
57 3
        $items = $news->each(function (DomCrawler $node) {
58 3
            $pattern = '.inner-content > .title > a';
59
60 3
            $result = $node->filter((string) $pattern);
61
62 3
            return $result->first()->attr('href');
63 3
        });
64
65 2
        return array_reverse($items);
66
    }
67
}
68