1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TreeHouse\IoBundle\Item\Modifier\Item\Filter; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
6
|
|
|
use TreeHouse\Feeder\Exception\FilterException; |
7
|
|
|
use TreeHouse\Feeder\Modifier\Item\Filter\FilterInterface; |
8
|
|
|
use TreeHouse\IoBundle\Import\Feed\FeedItemBag; |
9
|
|
|
use TreeHouse\IoBundle\Item\ItemBag; |
10
|
|
|
use TreeHouse\IoBundle\Model\SourceInterface; |
11
|
|
|
use TreeHouse\IoBundle\Scrape\ScrapedItemBag; |
12
|
|
|
use TreeHouse\IoBundle\Source\Manager\CachedSourceManager; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Filters out items in the feed that have a modification date |
16
|
|
|
* before its corresponding source's modification date. |
17
|
|
|
*/ |
18
|
|
View Code Duplication |
class ModifiedItemFilter implements FilterInterface |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var CachedSourceManager |
22
|
|
|
*/ |
23
|
|
|
protected $sourceManager; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param CachedSourceManager $sourceManager |
27
|
|
|
*/ |
28
|
10 |
|
public function __construct(CachedSourceManager $sourceManager) |
29
|
|
|
{ |
30
|
10 |
|
$this->sourceManager = $sourceManager; |
31
|
10 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
* |
36
|
|
|
* @param ItemBag $item |
37
|
|
|
*/ |
38
|
10 |
|
public function filter(ParameterBag $item) |
39
|
|
|
{ |
40
|
|
|
/** @var FeedItemBag $item */ |
41
|
|
|
// if source does not exist yet, by all means process it |
42
|
10 |
|
if (null === $source = $this->findSource($item)) { |
|
|
|
|
43
|
4 |
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// first try modification date |
47
|
8 |
|
if (null !== $mutationDate = $item->getDatetimeModified()) { |
|
|
|
|
48
|
6 |
|
if ($source->getDatetimeImported() > $mutationDate) { |
49
|
4 |
|
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
|
4 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param ItemBag $item |
58
|
|
|
* |
59
|
|
|
* @return null|SourceInterface |
60
|
|
|
*/ |
61
|
10 |
|
protected function findSource(ItemBag $item) |
62
|
|
|
{ |
63
|
10 |
|
if ($item instanceof FeedItemBag) { |
64
|
10 |
|
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
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.