Completed
Push — master ( 631b00...2fe979 )
by Alex
01:43
created

CheckReadSince::perform()   A

Complexity

Conditions 4
Paths 17

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 4
nc 17
nop 3
1
<?php declare(strict_types=1);
2
3
4
namespace FeedIo\Check;
5
6
use FeedIo\Feed;
7
use FeedIo\FeedIo;
8
9
/**
10
 * Class CheckReadSince
11
 * @codeCoverageIgnore
12
 */
13
class CheckReadSince implements CheckInterface
14
{
15
    public function perform(FeedIo $feedIo, Feed $feed, Result $result): bool
16
    {
17
        try {
18
            $dates = $result->getItemDates();
19
            $count = count($dates);
20
            $last = $dates[$count - 1];
21
            if ($last != $dates[0]) {
22
                $pick = intval($count / 2);
23
                $lastModified = $dates[$pick];
24
            } else {
25
                $lastModified = $last->sub(new \DateInterval('P1D'));
26
            }
27
            $secondFeed = $feedIo->readSince($feed->getUrl(), $lastModified)->getFeed();
28
            if (0 === count($secondFeed)) {
29
                $result->setNotUpdateable();
30
                return false;
31
            }
32
33
            return true;
34
        } catch (\Throwable $exception) {
35
            $result->setNotUpdateable();
36
            return false;
37
        }
38
    }
39
}
40