Code Duplication    Length = 52-56 lines in 2 locations

src/TreeHouse/IoBundle/Item/Modifier/Item/Filter/BlockedSourceFilter.php 1 location

@@ 17-68 (lines=52) @@
14
/**
15
 * Filters out items in the feed that correspond to blocked sources.
16
 */
17
class BlockedSourceFilter implements FilterInterface
18
{
19
    /**
20
     * @var CachedSourceManager
21
     */
22
    protected $sourceManager;
23
24
    /**
25
     * @param CachedSourceManager $sourceManager
26
     */
27
    public function __construct(CachedSourceManager $sourceManager)
28
    {
29
        $this->sourceManager = $sourceManager;
30
    }
31
32
    /**
33
     * @inheritdoc
34
     *
35
     * @param ItemBag $item
36
     */
37
    public function filter(ParameterBag $item)
38
    {
39
        $item->getOriginalId();
40
41
        // check if source already exists and is blocked
42
        if (null === $source = $this->findSource($item)) {
43
            return;
44
        }
45
46
        if ($source->isBlocked() === true) {
47
            throw new FilterException('Source is blocked');
48
        }
49
    }
50
51
    /**
52
     * @param ItemBag $item
53
     *
54
     * @return null|SourceInterface
55
     */
56
    protected function findSource(ItemBag $item)
57
    {
58
        if ($item instanceof FeedItemBag) {
59
            return $this->sourceManager->findSourceByFeed($item->getFeed(), $item->getOriginalId());
60
        }
61
62
        if ($item instanceof ScrapedItemBag) {
63
            return $this->sourceManager->findSourceByScraper($item->getScraper(), $item->getOriginalId());
64
        }
65
66
        return null;
67
    }
68
}
69

src/TreeHouse/IoBundle/Item/Modifier/Item/Filter/ModifiedItemFilter.php 1 location

@@ 18-73 (lines=56) @@
15
 * Filters out items in the feed that have a modification date
16
 * before its corresponding source's modification date.
17
 */
18
class ModifiedItemFilter implements FilterInterface
19
{
20
    /**
21
     * @var CachedSourceManager
22
     */
23
    protected $sourceManager;
24
25
    /**
26
     * @param CachedSourceManager $sourceManager
27
     */
28
    public function __construct(CachedSourceManager $sourceManager)
29
    {
30
        $this->sourceManager = $sourceManager;
31
    }
32
33
    /**
34
     * @inheritdoc
35
     *
36
     * @param ItemBag $item
37
     */
38
    public function filter(ParameterBag $item)
39
    {
40
        /** @var FeedItemBag $item */
41
        // if source does not exist yet, by all means process it
42
        if (null === $source = $this->findSource($item)) {
43
            return;
44
        }
45
46
        // first try modification date
47
        if (null !== $mutationDate = $item->getDatetimeModified()) {
48
            if ($source->getDatetimeImported() > $mutationDate) {
49
                throw new FilterException('Item is not modified');
50
            }
51
        }
52
53
        // item is modified or we don't have enough information to determine that, either way continue.
54
    }
55
56
    /**
57
     * @param ItemBag $item
58
     *
59
     * @return null|SourceInterface
60
     */
61
    protected function findSource(ItemBag $item)
62
    {
63
        if ($item instanceof FeedItemBag) {
64
            return $this->sourceManager->findSourceByFeed($item->getFeed(), $item->getOriginalId());
65
        }
66
67
        if ($item instanceof ScrapedItemBag) {
68
            return $this->sourceManager->findSourceByScraper($item->getScraper(), $item->getOriginalId());
69
        }
70
71
        return null;
72
    }
73
}
74