-
Notifications
You must be signed in to change notification settings - Fork 54
/
Result.php
103 lines (85 loc) · 2.41 KB
/
Result.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
declare(strict_types=1);
namespace FeedIo\Reader;
use DateTime;
use FeedIo\Adapter\ResponseInterface;
use FeedIo\FeedInterface;
use FeedIo\Filter\Chain;
use FeedIo\Filter\Since;
use FeedIo\Reader\Result\UpdateStats;
/**
* Result of the read() operation
*
* a Result instance holds the following :
*
* - the Feed instance
* - Date and time of the request
* - value of the 'modifiedSince' header sent through the request
* - the raw response
* - the DOM document
* - URL of the feed
*/
class Result
{
protected DateTime $date;
protected ?UpdateStats $updateStats = null;
public function __construct(
protected Document $document,
protected FeedInterface $feed,
protected DateTime $modifiedSince,
protected ResponseInterface $response,
protected string $url
) {
$this->date = new DateTime();
}
public function getDate(): DateTime
{
return $this->date;
}
public function getDocument(): Document
{
return $this->document;
}
public function getFeed(): FeedInterface
{
return $this->feed;
}
public function getItemsSince(DateTime $since = null): iterable
{
$filter = new Chain();
$filter->add(new Since($since ?? $this->modifiedSince));
return $filter->filter($this->getFeed());
}
public function getFilteredItems(Chain $filterChain): iterable
{
return $filterChain->filter($this->feed);
}
public function getModifiedSince(): ?DateTime
{
return $this->modifiedSince;
}
public function getResponse(): ResponseInterface
{
return $this->response;
}
public function getUrl(): string
{
return $this->url;
}
public function getNextUpdate(
int $minDelay = UpdateStats::DEFAULT_MIN_DELAY,
int $sleepyDelay = UpdateStats::DEFAULT_SLEEPY_DELAY,
int $sleepyDuration = UpdateStats::DEFAULT_DURATION_BEFORE_BEING_SLEEPY,
float $marginRatio = UpdateStats::DEFAULT_MARGIN_RATIO
): DateTime {
$updateStats = $this->getUpdateStats();
return $updateStats->computeNextUpdate($minDelay, $sleepyDelay, $sleepyDuration, $marginRatio);
}
public function getUpdateStats(): UpdateStats
{
if (is_null($this->updateStats)) {
$this->updateStats = new UpdateStats($this->getFeed());
}
return $this->updateStats;
}
}