Passed
Push — master ( b75422...951cb3 )
by Rougin
02:22
created

Collector   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 45
ccs 9
cts 12
cp 0.75
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A collect() 0 14 2
1
<?php
2
3
namespace Pilipinews\Common;
4
5
use Pilipinews\Common\Interfaces\CrawlerInterface;
6
use Pilipinews\Common\Interfaces\ScraperInterface;
7
8
/**
9
 * Collector
10
 *
11
 * @package Pilipinews
12
 * @author  Rougin Gutib <[email protected]>
13
 */
14
class Collector
15
{
16
    /**
17
     * @var \Pilipinews\Common\Interfaces\CrawlerInterface
18
     */
19
    protected $crawler;
20
21
    /**
22
     * @var \Pilipinews\Common\Interfaces\ScraperInterface
23
     */
24
    protected $scraper;
25
26
    /**
27
     * Initializes the collector instance.
28
     *
29
     * @param \Pilipinews\Common\Interfaces\CrawlerInterface $crawler
30
     * @param \Pilipinews\Common\Interfaces\ScraperInterface $scraper
31
     */
32 3
    public function __construct(CrawlerInterface $crawler, ScraperInterface $scraper)
33
    {
34 3
        $this->crawler = $crawler;
35
36 3
        $this->scraper = $scraper;
37 3
    }
38
39
    /**
40
     * Returns an array of article instances.
41
     *
42
     * @param  callable $callback
43
     * @return \Pilipinews\Common\Article[]
44
     */
45 3
    public function collect($callback)
46
    {
47 3
        $items = $this->crawler->crawl();
48
49 3
        $articles = array();
50
51 3
        foreach ((array) $items as $item)
52
        {
53 3
            $article = $this->scraper->scrape($item);
54
55
            $articles[] = $callback($article, $item);
56
        }
57
58
        return $articles;
59
    }
60
}
61