Crawler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A verify() 0 23 3
A crawl() 0 9 1
1
<?php
2
3
namespace Pilipinews\Website\Gma;
4
5
use Pilipinews\Common\Client;
6
use Pilipinews\Common\Crawler as DomCrawler;
7
use Pilipinews\Common\Interfaces\CrawlerInterface;
8
9
/**
10
 * GMA 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('news/nation', 'news/regions');
21
22
    /**
23
     * @var string
24
     */
25
    protected $link = 'https://data.gmanetwork.com/gno/pages/home_1a_json.gz';
26
27
    /**
28
     * Returns an array of articles to scrape.
29
     *
30
     * @return string[]
31
     */
32 3
    public function crawl()
33
    {
34 3
        $response = Client::request($this->link);
35
36 3
        $json = json_decode($response, true);
37
38 3
        $items = $json['story_lists_just_in'];
39
40 3
        return $this->verify((array) $items);
41
    }
42
43
    /**
44
     * Returns the allowed article URLs to scrape.
45
     *
46
     * @param  string[] $items
47
     * @return string[]
48
     */
49 3
    protected function verify($items)
50
    {
51 3
        $base = 'https://www.gmanetwork.com/news/';
52
53 3
        $allowed = (array) $this->allowed;
54
55 3
        $callback = function ($item) use ($base, $allowed)
56
        {
57 3
            $link = null;
58
59 3
            foreach ((array) $allowed as $keyword)
60
            {
61 3
                $result = strpos($item['link'], (string) $keyword);
62
63 3
                $result !== false && $link = $base . $item['link'];
64 1
            }
65
66 3
            return (string) $link;
67 3
        };
68
69 1
        $result = array_map($callback, (array) $items);
70
71 1
        return array_values(array_filter($result));
72
    }
73
}
74