DateTimeMicroType   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 0
cts 13
cp 0
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSQLDeclaration() 0 3 1
A getName() 0 3 1
A convertToDatabaseValue() 0 3 2
A convertToPHPValue() 0 13 4
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