Completed
Push — master ( e4d2f2...1909f4 )
by Alex
14s queued 11s
created

HttpLastModified::isInvalid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
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\Reader\Fixer;
12
13
use FeedIo\FeedInterface;
14
use FeedIo\Reader\FixerAbstract;
15
use FeedIo\Reader\Result;
16
17
class HttpLastModified extends FixerAbstract
18
{
19
20
    /**
21
     * @param Result $result
22
     * @return FixerAbstract
23
     */
24 3
    public function correct(Result $result): FixerAbstract
25
    {
26 3
        $feed = $result->getFeed();
27 3
        $response = $result->getResponse();
28
29 3
        if ($this->isInvalid($feed) && $response->getLastModified() instanceof \DateTime) {
30 1
            $this->logger->debug("found last modified: " . $response->getLastModified()->format(\DateTime::RSS));
31 1
            $feed->setLastModified($response->getLastModified());
32 1
            $this->correctItems($feed);
33
        }
34
35 3
        return $this;
36
    }
37
38 1
    protected function correctItems(FeedInterface $feed): void
39
    {
40 1
        foreach ($feed as $item) {
41
            $item->setLastModified($feed->getLastModified());
42
        }
43 1
    }
44
45
    /**
46
     * @param  FeedInterface $feed
47
     * @return bool
48
     */
49 3
    protected function isInvalid(FeedInterface $feed): bool
50
    {
51 3
        return is_null($feed->getLastModified()) || $feed->getLastModified() == new \DateTime('@0');
52
    }
53
}
54