Completed
Pull Request — master (#257)
by
unknown
01:57
created

LastModified   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 37
ccs 0
cts 13
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A correct() 0 12 3
A searchLastModified() 0 12 3
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
16
class LastModified extends FixerAbstract
17
{
18
19
    /**
20
     * @param  FeedInterface $feed
21
     * @return FixerAbstract
22
     */
23
    public function correct(FeedInterface $feed) : FixerAbstract
24
    {
25
        $date = new \DateTime('@0');
26
        if (is_null($feed->getLastModified()) || $feed->getLastModified() == $date) {
27
            $this->logger->notice("correct last modified date for feed {$feed->getTitle()}");
28
            $feed->setLastModified(
29
                $this->searchLastModified($feed)
30
            );
31
        }
32
33
        return $this;
34
    }
35
36
    /**
37
     * @param  FeedInterface $feed
38
     * @return \DateTime
39
     */
40
    public function searchLastModified(FeedInterface $feed) : \DateTime
41
    {
42
        $latest = new \DateTime('@0');
43
44
        foreach ($feed as $item) {
45
            if ($item->getLastModified() > $latest) {
46
                $latest = $item->getLastModified();
47
            }
48
        }
49
50
        return $latest;
51
    }
52
}
53