Completed
Push — issue/280 ( 8103f1 )
by Alex
01:58
created

UpdateStats::getTimestamp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 UpdateStats
18
{
19
20
    private $intervals;
21
22 1
    public function __construct(FeedInterface $feed)
23
    {
24 1
        $this->intervals = $this->computeIntervals($this->extractDates($feed));
25 1
    }
26
27
    /**
28
     * @return array
29
     */
30 1
    public function getIntervals(): array
31
    {
32 1
        return $this->intervals;
33
    }
34
35 1
    public function getMinInterval(): \DateInterval
36
    {
37 1
        $value = min($this->intervals);
38 1
        return new \DateInterval("PT{$value}S");
39
    }
40
41 1
    private function computeIntervals(array $dates): array
42
    {
43 1
        rsort($dates);
44 1
        $intervals = [];
45 1
        $current = 0;
46 1
        foreach ($dates as $date) {
47 1
            if ( $current > 0) {
48 1
                $intervals[] = $current - $date;
49
            }
50 1
            $current = $date;
51
        }
52 1
        return $intervals;
53
    }
54
55 1
    private function extractDates(FeedInterface $feed): array
56
    {
57 1
        $dates = [];
58 1
        foreach ($feed as $item) {
59 1
            $dates[] = $this->getTimestamp($item);
60
        }
61 1
        return $dates;
62
    }
63
64 1
    private function getTimestamp(ItemInterface $item): ? int
65
    {
66 1
        return $item->getLastModified()->getTimestamp();
67
    }
68
}