Passed
Push — master ( 8bb76a...f86e9c )
by Rougin
06:57
created

Scraper::scrape()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.4218

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 15
ccs 2
cts 8
cp 0.25
crap 1.4218
rs 10
1
<?php
2
3
namespace Pilipinews\Website\Gma;
4
5
use Pilipinews\Common\Article;
6
use Pilipinews\Common\Client;
7
use Pilipinews\Common\Converter;
8
use Pilipinews\Common\Crawler as DomCrawler;
9
use Pilipinews\Common\Interfaces\ScraperInterface;
10
use Pilipinews\Common\Scraper as AbstractScraper;
11
12
/**
13
 * GMA News Scraper
14
 *
15
 * @package Pilipinews
16
 * @author  Rougin Gutib <[email protected]>
17
 */
18
class Scraper extends AbstractScraper implements ScraperInterface
19
{
20
    /**
21
     * Returns the contents of an article.
22
     *
23
     * @param  string $link
24
     * @return \Pilipinews\Common\Article
25
     */
26 30
    public function scrape($link)
27
    {
28 30
        $this->prepare(mb_strtolower($link));
29
30
        $title = $this->json['story']['title'];
31
32
        $converter = new Converter;
33
34
        $title = $converter->convert($title);
35
36
        $body = $this->tweet($this->crawler);
37
38
        $html = (string) $this->html($body);
39
40
        return new Article($title, $html, $link);
41
    }
42
43
    /**
44
     * Initializes the crawler instance.
45
     *
46
     * @param  string $link
47
     * @return void
48
     */
49 30
    protected function prepare($link)
50
    {
51 30
        $response = (string) Client::request((string) $link);
52
53 30
        $html = trim(preg_replace('/\s+/', ' ', $response));
54
55 30
        $html = str_replace('<p> <strong>', '<p><strong>', $html);
56
57 30
        $html = str_replace('<br /> ', '<br />', $html);
58
59 30
        preg_match('/var initialData = {(.*?)};/i', $html, $match);
60
61 30
        $this->json = json_decode('{' . $match[1] . '}', true);
0 ignored issues
show
Bug Best Practice introduced by
The property json does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
62
63
        $content = (string) $this->json['story']['main'];
64
65
        $this->crawler = new DomCrawler((string) $content);
66
    }
67
}
68