Completed
Pull Request — master (#13)
by Peter
03:22 queued 54s
created

ChangeFreq::getByPriority()   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
2
declare(strict_types=1);
3
4
/**
5
 * GpsLab component.
6
 *
7
 * @author    Peter Gribanov <[email protected]>
8
 * @copyright Copyright (c) 2011, Peter Gribanov
9
 * @license   http://opensource.org/licenses/MIT
10
 */
11
12
namespace GpsLab\Component\Sitemap\Url;
13
14
final class ChangeFreq
15
{
16
    public const ALWAYS = 'always';
17
18
    public const HOURLY = 'hourly';
19
20
    public const DAILY = 'daily';
21
22
    public const WEEKLY = 'weekly';
23
24
    public const MONTHLY = 'monthly';
25
26
    public const YEARLY = 'yearly';
27
28
    public const NEVER = 'never';
29
30
    private const CHANGE_FREQ_PRIORITY = [
31
        '1.0' => self::HOURLY,
32
        '0.9' => self::DAILY,
33
        '0.8' => self::DAILY,
34
        '0.7' => self::WEEKLY,
35
        '0.6' => self::WEEKLY,
36
        '0.5' => self::WEEKLY,
37
        '0.4' => self::MONTHLY,
38
        '0.3' => self::MONTHLY,
39
        '0.2' => self::YEARLY,
40
        '0.1' => self::YEARLY,
41
        '0.0' => self::NEVER,
42
    ];
43
44
    /**
45
     * @param \DateTimeInterface $last_mod
46
     *
47
     * @return string|null
48
     */
49 16
    public static function getByLastMod(\DateTimeInterface $last_mod): ?string
50
    {
51 16
        if ($last_mod < new \DateTime('-1 year')) {
52 4
            return self::YEARLY;
53
        }
54
55 12
        if ($last_mod < new \DateTime('-1 month')) {
56 4
            return self::MONTHLY;
57
        }
58
59 8
        if ($last_mod < new \DateTime('-1 week')) {
60 4
            return self::WEEKLY;
61
        }
62
63 4
        return null;
64
    }
65
66
    /**
67
     * @param string $priority
68
     *
69
     * @return string|null
70
     */
71 41
    public static function getByPriority(string $priority): ?string
72
    {
73 41
        return self::CHANGE_FREQ_PRIORITY[$priority] ?? null;
74
    }
75
}
76