Crawler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 54
ccs 17
cts 17
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A items() 0 18 1
A crawl() 0 12 2
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 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
        {
36 3
            $items = $this->items((string) $page);
37
38 3
            $result = array_merge($result, $items);
39 1
        }
40
41 3
        return $result;
42
    }
43
44
    /**
45
     * Returns an array of articles to scrape.
46
     *
47
     * @param  string $link
48
     * @return string[]
49
     */
50 3
    protected function items($link)
51
    {
52 3
        $crawler = new DomCrawler(Client::request($link));
53
54 3
        $pattern = '.search-inner > .outer-content';
55
56 3
        $news = $crawler->filter((string) $pattern);
57
58 3
        $items = $news->each(function (DomCrawler $node)
59
        {
60 3
            $pattern = '.inner-content > .title > a';
61
62 3
            $result = $node->filter((string) $pattern);
63
64 3
            return $result->first()->attr('href');
65 3
        });
66
67 1
        return array_reverse($items);
68
    }
69
}
70