Passed
Push — dev ( 824cd4...d410ef )
by Greg
12:51
created

TimestampFactory::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2021 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Factories;
21
22
use Fisharebest\Webtrees\Auth;
23
use Fisharebest\Webtrees\Contracts\TimestampFactoryInterface;
24
use Fisharebest\Webtrees\Contracts\TimestampInterface;
25
use Fisharebest\Webtrees\Contracts\UserInterface;
26
use Fisharebest\Webtrees\I18N;
27
use Fisharebest\Webtrees\Site;
28
use Fisharebest\Webtrees\Timestamp;
29
use LogicException;
30
31
use function date;
32
use function date_create_from_format;
33
use function time;
34
35
/**
36
 * Create a timestamp object.
37
 */
38
class TimestampFactory implements TimestampFactoryInterface
39
{
40
    /**
41
     * @param int                $timestamp
42
     * @param UserInterface|null $user
43
     *
44
     * @return TimestampInterface
45
     */
46
    public function make(int $timestamp, UserInterface $user = null): TimestampInterface
47
    {
48
        $user     ??= Auth::user();
49
        $timezone = $user->getPreference(UserInterface::PREF_TIME_ZONE, Site::getPreference('TIMEZONE'));
50
        $locale   = I18N::locale()->code();
51
52
        return new Timestamp($timestamp, $timezone, $locale);
53
    }
54
55
    /**
56
     * @param string|null        $string YYYY-MM-DD HH:MM:SS (as provided by SQL).
57
     * @param string             $format
58
     * @param UserInterface|null $user
59
     *
60
     * @return TimestampInterface
61
     */
62
    public function fromString(?string $string, string $format = 'Y-m-d H:i:s', UserInterface $user = null): TimestampInterface
63
    {
64
        $string    ??= date($format);
65
        $timestamp = date_create_from_format($format, $string);
66
67
        if ($timestamp === false) {
68
            throw new LogicException('date/time "' . $string . '" does not match pattern "' . $format . '"');
69
        }
70
71
        return $this->make($timestamp->getTimestamp(), $user);
72
    }
73
74
    /**
75
     * @param UserInterface|null $user
76
     *
77
     * @return TimestampInterface
78
     */
79
    public function now(UserInterface $user = null): TimestampInterface
80
    {
81
        return $this->make(time(), $user);
82
    }
83
}
84