|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the feed-io package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Alexandre Debril <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace FeedIo\Reader; |
|
12
|
|
|
|
|
13
|
|
|
use FeedIo\Adapter\ResponseInterface; |
|
14
|
|
|
use FeedIo\FeedInterface; |
|
15
|
|
|
use FeedIo\Reader\Result\UpdateStats; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Result of the read() operation |
|
19
|
|
|
* |
|
20
|
|
|
* a Result instance holds the following : |
|
21
|
|
|
* |
|
22
|
|
|
* - the Feed instance |
|
23
|
|
|
* - Date and time of the request |
|
24
|
|
|
* - value of the 'modifiedSince' header sent through the request |
|
25
|
|
|
* - the raw response |
|
26
|
|
|
* - the DOM document |
|
27
|
|
|
* - URL of the feed |
|
28
|
|
|
*/ |
|
29
|
|
|
class Result |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var \DateTime |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $modifiedSince; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var \DateTime |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $date; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var \FeedIo\FeedInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $feed; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var \FeedIo\Adapter\ResponseInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $response; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var \FeedIo\Reader\Result\UpdateStats |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $updateStats; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var Document |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $document; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $url; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param Document $document |
|
69
|
|
|
* @param FeedInterface $feed |
|
70
|
|
|
* @param \DateTime $modifiedSince |
|
71
|
|
|
* @param ResponseInterface $response |
|
72
|
|
|
* @param $url |
|
73
|
|
|
*/ |
|
74
|
11 |
|
public function __construct( |
|
75
|
|
|
Document $document, |
|
76
|
|
|
FeedInterface $feed, |
|
77
|
|
|
\DateTime $modifiedSince, |
|
78
|
|
|
ResponseInterface $response, |
|
79
|
|
|
string $url |
|
80
|
|
|
) { |
|
81
|
11 |
|
$this->date = new \DateTime(); |
|
82
|
11 |
|
$this->document = $document; |
|
83
|
11 |
|
$this->feed = $feed; |
|
84
|
11 |
|
$this->modifiedSince = $modifiedSince; |
|
85
|
11 |
|
$this->response = $response; |
|
86
|
11 |
|
$this->url = $url; |
|
87
|
11 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return \DateTime |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function getDate() : \DateTime |
|
93
|
|
|
{ |
|
94
|
1 |
|
return $this->date; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return Document |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function getDocument() : Document |
|
101
|
|
|
{ |
|
102
|
1 |
|
return $this->document; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return FeedInterface |
|
107
|
|
|
*/ |
|
108
|
9 |
|
public function getFeed() : FeedInterface |
|
109
|
|
|
{ |
|
110
|
9 |
|
return $this->feed; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return \DateTime|null |
|
115
|
|
|
*/ |
|
116
|
2 |
|
public function getModifiedSince() : ? \DateTime |
|
117
|
|
|
{ |
|
118
|
2 |
|
return $this->modifiedSince; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return ResponseInterface |
|
123
|
|
|
*/ |
|
124
|
4 |
|
public function getResponse() : ResponseInterface |
|
125
|
|
|
{ |
|
126
|
4 |
|
return $this->response; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
1 |
|
public function getUrl() : string |
|
133
|
|
|
{ |
|
134
|
1 |
|
return $this->url; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param int $minDelay |
|
139
|
|
|
* @param int $sleepyDelay |
|
140
|
|
|
* @param int $sleepyDuration |
|
141
|
|
|
* @param float $marginRatio |
|
142
|
|
|
* @return \DateTime |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getNextUpdate( |
|
145
|
|
|
int $minDelay = UpdateStats::DEFAULT_MIN_DELAY, |
|
146
|
|
|
int $sleepyDelay = UpdateStats::DEFAULT_SLEEPY_DELAY, |
|
147
|
|
|
int $sleepyDuration = UpdateStats::DEFAULT_DURATION_BEFORE_BEING_SLEEPY, |
|
148
|
|
|
float $marginRatio = UpdateStats::DEFAULT_MARGIN_RATIO |
|
149
|
|
|
): \DateTime { |
|
150
|
|
|
$updateStats = $this->getUpdateStats(); |
|
151
|
|
|
return $updateStats->computeNextUpdate($minDelay, $sleepyDelay, $sleepyDuration, $marginRatio); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @return UpdateStats |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getUpdateStats(): UpdateStats |
|
158
|
|
|
{ |
|
159
|
|
|
if (is_null($this->updateStats)) { |
|
160
|
|
|
$this->updateStats = new UpdateStats($this->getFeed()); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $this->updateStats; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|