Passed
Push — master ( 7bfbbf...99049c )
by Rougin
10:53
created

Scraper::image()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 15
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 30
     */
24
    public function scrape($link)
25 30
    {
26
        $this->prepare(mb_strtolower($link));
27 30
28
        $title = (string) $this->title('h1');
29 30
30
        $body = $this->body('.page-content');
31 30
32
        $body = $this->image($body);
33
34
        return new Article($title, $this->html($body));
35
    }
36
37
    /**
38
     * Converts image elements to readable string.
39
     *
40
     * @param  \Pilipinews\Common\Crawler $crawler
41
     * @return \Pilipinews\Common\Crawler
42
     */
43
    protected function image(DomCrawler $crawler)
44
    {
45
        $callback = function (DomCrawler $crawler) {
46
            $result = $crawler->filter('img')->first();
47
48
            $image = (string) $result->attr('src');
49
50
            $text = $crawler->filter('p')->first();
51
52
            $message = $image . ' - ' . $text->html();
53
54
            return '<p>PHOTO: ' . $message . '</p>';
55
        };
56
57
        return $this->replace($crawler, 'figure.image', $callback);
58
    }
59
}
60