@@ -31,14 +31,22 @@ class UpdateStats
3131
3232 protected array $ intervals = [];
3333
34+ protected int $ newestItemDate = 0 ;
35+
3436 /**
3537 * UpdateStats constructor.
3638 * @param FeedInterface $feed
3739 */
3840 public function __construct (
3941 protected FeedInterface $ feed
4042 ) {
41- $ this ->intervals = $ this ->computeIntervals ($ this ->extractDates ($ feed ));
43+ $ dates = $ this ->extractDates ($ feed );
44+ if (count ($ dates ) > 0 ) {
45+ $ this ->newestItemDate = max ($ dates );
46+ } else {
47+ $ this ->newestItemDate = $ this ->getFeedTimestamp ();
48+ }
49+ $ this ->intervals = $ this ->computeIntervals ($ dates );
4250 }
4351
4452 /**
@@ -57,7 +65,6 @@ public function computeNextUpdate(
5765 if ($ this ->isSleepy ($ sleepyDuration , $ marginRatio )) {
5866 return (new \DateTime ())->setTimestamp (time () + $ sleepyDelay );
5967 }
60- $ feedTimeStamp = $ this ->getFeedTimestamp ();
6168 $ now = time ();
6269 $ intervals = [
6370 $ this ->getAverageInterval (),
@@ -66,7 +73,7 @@ public function computeNextUpdate(
6673 sort ($ intervals );
6774 $ newTimestamp = $ now + $ minDelay ;
6875 foreach ($ intervals as $ interval ) {
69- $ computedTimestamp = $ this ->addInterval ($ feedTimeStamp , $ interval , $ marginRatio );
76+ $ computedTimestamp = $ this ->addInterval ($ this -> newestItemDate , $ interval , $ marginRatio );
7077 if ($ computedTimestamp > $ now ) {
7178 $ newTimestamp = $ computedTimestamp ;
7279 break ;
@@ -82,7 +89,7 @@ public function computeNextUpdate(
8289 */
8390 public function isSleepy (int $ sleepyDuration , float $ marginRatio ): bool
8491 {
85- return time () > $ this ->addInterval ($ this ->getFeedTimestamp () , $ sleepyDuration , $ marginRatio );
92+ return time () > $ this ->addInterval ($ this ->newestItemDate , $ sleepyDuration , $ marginRatio );
8693 }
8794
8895 /**
@@ -171,6 +178,14 @@ public function getMedianInterval(): int
171178 }
172179 }
173180
181+ /**
182+ * @return int
183+ */
184+ public function getNewestItemDate (): int
185+ {
186+ return $ this ->newestItemDate ;
187+ }
188+
174189 private function computeIntervals (array $ dates ): array
175190 {
176191 rsort ($ dates );
0 commit comments