Scraper::scrape()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 3
b 0
f 0
nc 1
nop 1
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Pilipinews\Website\Pna;
4
5
use Pilipinews\Common\Article;
6
use Pilipinews\Common\Crawler as DomCrawler;
7
use Pilipinews\Common\Interfaces\ScraperInterface;
8
use Pilipinews\Common\Scraper as AbstractScraper;
9
10
/**
11
 * Philippine News Agency Scraper
12
 *
13
 * @package Pilipinews
14
 * @author  Rougin Gutib <[email protected]>
15
 */
16
class Scraper extends AbstractScraper implements ScraperInterface
17
{
18
    /**
19
     * Returns the contents of an article.
20
     *
21
     * @param  string $link
22
     * @return \Pilipinews\Common\Article
23
     */
24 36
    public function scrape($link)
25
    {
26 36
        $this->prepare(mb_strtolower($link));
27
28 36
        $title = (string) $this->title('h1');
29
30 36
        $body = $this->body('.page-content');
31
32 36
        $body = $this->image($body);
33
34 36
        $html = (string) $this->html($body);
35
36 36
        return new Article($title, $html, $link);
37
    }
38
39
    /**
40
     * Converts image elements to readable string.
41
     *
42
     * @param  \Pilipinews\Common\Crawler $crawler
43
     * @return \Pilipinews\Common\Crawler
44
     */
45
    protected function image(DomCrawler $crawler)
46
    {
47 36
        $callback = function (DomCrawler $crawler)
48
        {
49 9
            $result = $crawler->filter('img')->first();
50
51 9
            $image = (string) $result->attr('src');
52
53 9
            $text = $crawler->filter('figcaption')->first();
54
55 9
            $message = $image . ' - ' . $text->text();
56
57 9
            return '<p>PHOTO: ' . $message . '</p>';
58 36
        };
59
60
        return $this->replace($crawler, 'figure.image', $callback);
61
    }
62
}
63