Crawler::crawl()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 29
ccs 16
cts 16
cp 1
crap 3
rs 9.8333
1
<?php
2
3
namespace Pilipinews\Website\Inquirer;
4
5
use Pilipinews\Common\Client;
6
use Pilipinews\Common\Crawler as DomCrawler;
7
use Pilipinews\Common\Interfaces\CrawlerInterface;
8
9
/**
10
 * Inquirer News Crawler
11
 *
12
 * @package Pilipinews
13
 * @author  Rougin Gutib <[email protected]>
14
 */
15
class Crawler implements CrawlerInterface
16
{
17
    /**
18
     * @var string[]
19
     */
20
    protected $allowed = array('Headlines', 'Regions', 'Nation');
21
22
    /**
23
     * Returns an array of articles to scrape.
24
     *
25
     * @return string[]
26
     */
27 3
    public function crawl()
28
    {
29 3
        $link = 'https://newsinfo.inquirer.net/category/latest-stories';
30
31 3
        $response = Client::request($link);
32
33 3
        $allowed = (array) $this->allowed;
34
35 3
        $callback = function (DomCrawler $node) use ($allowed)
36
        {
37 3
            $category = $node->filter('#ch-cat')->first();
38
39 3
            if (! $category->count())
40 1
            {
41 3
                return null;
42
            }
43
44 3
            $allowed = in_array($category->text(), $allowed);
45
46 3
            $link = $node->filter('a')->first();
47
48 3
            return $allowed ? $link->attr('href') : null;
49 3
        };
50
51 1
        $crawler = new DomCrawler((string) $response);
52
53 1
        $news = $crawler->filter('#inq-channel-left > #ch-ls-box');
54
55 1
        return array_values(array_filter($news->each($callback)));
56
    }
57
}
58