Completed
Push — issue/300 ( fd6ec3 )
by Alex
01:38
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
7
use FeedIo\Feed;
8
use FeedIo\FeedIo;
9
10
/**
11
 * Class CheckReadSince
12
 * @codeCoverageIgnore
13
 */
14
class CheckReadSince implements CheckInterface
15
{
16
    public function perform(FeedIo $feedIo, Feed $feed, Result $result): bool
17
    {
18
        try {
19
            $dates = $result->getItemDates();
20
            $count = count($dates);
21
            $last = $dates[$count - 1];
22
            if ($last != $dates[0]) {
23
                $pick = intval($count / 2);
24
                $lastModified = $dates[$pick];
25
            } else {
26
                $lastModified = $last->sub(new \DateInterval('P1D'));
27
            }
28
            $secondFeed = $feedIo->readSince($feed->getUrl(), $lastModified)->getFeed();
29
            if (0 === count($secondFeed)) {
30
                $result->setNotUpdateable();
31
                return false;
32
            }
33
34
            return true;
35
        } catch (\Throwable $exception) {
36
            $result->setNotUpdateable();
37
            return false;
38
        }
39
    }
40
41
}