Passed
Push — master ( aa2b5c...1efe0e )
by Rougin
02:39
created

Collector::collect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.2109

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 14
ccs 5
cts 8
cp 0.625
crap 2.2109
rs 10
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