Passed
Push — master ( 5bdfe8...9d312b )
by Vincent
04:50
created

DateTimeToTimestampTransformer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 16
c 1
b 0
f 0
dl 0
loc 60
ccs 17
cts 17
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A transformToHttp() 0 17 5
A transformFromHttp() 0 7 2
1
<?php
2
3
namespace Bdf\Form\Leaf\Date\Transformer;
4
5
use Bdf\Form\ElementInterface;
6
use Bdf\Form\Leaf\Date\DateTimeElement;
7
use Bdf\Form\Transformer\TransformerInterface;
8
use DateTime;
9
use DateTimeInterface;
10
use DateTimeZone;
11
12
/**
13
 * Transform a DateTime instance from a form element to a timestamp to a model
14
 */
15
final class DateTimeToTimestampTransformer implements TransformerInterface
16
{
17
    /**
18
     * @var class-string<DateTimeInterface>|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<DateTimeInterface>|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<DateTimeInterface>|null.
Loading history...
19
     */
20
    private $className;
21
22
    /**
23
     * @var DateTimeZone|null
24
     */
25
    private $timezone;
26
27
28
    /**
29
     * DateTimeToTimestampTransformer constructor.
30
     *
31
     * @param class-string<DateTimeInterface>|null $className The date time class name to use when retrieving value from model. If null, will use the class defined in the input element
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<DateTimeInterface>|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<DateTimeInterface>|null.
Loading history...
32
     * @param DateTimeZone|null $timezone The timezone to set when retrieving value from model. If null will use the element's timezone
33
     */
34 4
    public function __construct(?string $className = null, ?DateTimeZone $timezone = null)
35
    {
36 4
        $this->className = $className;
37 4
        $this->timezone = $timezone;
38 4
    }
39
40
    /**
41
     * {@inheritdoc}
42
     *
43
     * @psalm-suppress UndefinedInterfaceMethod
44
     * @psalm-suppress PossiblyUndefinedMethod
45
     */
46 4
    public function transformToHttp($value, ElementInterface $input): ?DateTimeInterface
47
    {
48 4
        if ($value === null) {
49 1
            return null;
50
        }
51
52 4
        $className = $this->className ?? ($input instanceof DateTimeElement ? $input->dateTimeClassName() : DateTime::class);
53 4
        $timezone = $this->timezone ?? ($input instanceof DateTimeElement ? $input->timezone() : null);
54
55
        /** @var DateTimeInterface $dateTime */
56 4
        $dateTime = new $className;
57
58 4
        if ($timezone) {
59 1
            $dateTime = $dateTime->setTimezone($timezone);
60
        }
61
62 4
        return $dateTime->setTimestamp($value);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 1
    public function transformFromHttp($value, ElementInterface $input): ?int
69
    {
70 1
        if (!$value instanceof DateTimeInterface) {
71 1
            return null;
72
        }
73
74 1
        return $value->getTimestamp();
75
    }
76
}
77