DateTimeMicroType::convertToDatabaseValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
namespace Simplex\Quickstart\Shared\Doctrine\Type;
4
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use Doctrine\DBAL\Types\ConversionException;
7
use Doctrine\DBAL\Types\Type;
8
9
/**
10
 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
11
 */
12
final class DateTimeMicroType extends Type
13
{
14
    public const NAME = 'datetime_micro';
15
16
    private const FORMAT = 'Y-m-d H:i:s.u';
17
18
    public function getName(): string
19
    {
20
        return self::NAME;
21
    }
22
23
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
24
    {
25
        return "DATETIME(6) COMMENT '(DC2Type:" . self::NAME  . ")'";
26
    }
27
28
    /** @param \DateTime|null $value */
29
    public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
30
    {
31
        return ($value !== null) ? $value->format(self::FORMAT) : null;
32
    }
33
34
    /** @param \DateTime|null|string $value */
35
    public function convertToPHPValue($value, AbstractPlatform $platform): ?\DateTime
36
    {
37
        if ($value === null || $value instanceof \DateTime) {
38
            return $value;
39
        }
40
41
        $value = \DateTime::createFromFormat(self::FORMAT, $value);
42
43
        if (!$value) {
44
            throw ConversionException::conversionFailedFormat($value, $this->getName(), self::FORMAT);
0 ignored issues
show
Bug introduced by
$value of type false|DateTime is incompatible with the type string expected by parameter $value of Doctrine\DBAL\Types\Conv...onversionFailedFormat(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
            throw ConversionException::conversionFailedFormat(/** @scrutinizer ignore-type */ $value, $this->getName(), self::FORMAT);
Loading history...
45
        }
46
47
        return $value;
48
    }
49
}
50