Completed
Push — issue/update-date ( 841ed9 )
by Alex
02:05
created

NextUpdate::getTimestamp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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\Result;
12
13
14
use FeedIo\Feed\ItemInterface;
15
use FeedIo\FeedInterface;
16
17
class NextUpdate
18
{
19
20
    private $intervals;
21
22
    public function __construct(FeedInterface $feed)
23
    {
24
        $this->intervals = $this->computeIntervals($this->extractDates($feed));
25
    }
26
27
    private function computeIntervals(array $dates): array
28
    {
29
        rsort($dates);
30
        $intervals = [];
31
        $current = 0;
32
        foreach ($dates as $date) {
33
            if ( $current > 0) {
34
                $intervals[] = $current - $date;
35
            }
36
            $current = $date;
37
        }
38
        return $intervals;
39
    }
40
41
    private function extractDates(FeedInterface $feed): array
42
    {
43
        $dates = [];
44
        foreach ($feed as $item) {
45
            $dates[] = $this->getTimestamp($item);
46
        }
47
        return $dates;
48
    }
49
50
    private function getTimestamp(ItemInterface $item): ? int
51
    {
52
        return $item->getLastModified()->getTimestamp();
53
    }
54
}