Failed Conditions
Push — master ( e75200...fab761 )
by Adrien
02:43
created

ChronosType::convertToDatabaseValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\DBAL\Types;
6
7
use Cake\Chronos\Chronos;
1 ignored issue
show
Bug introduced by
The type Cake\Chronos\Chronos was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use DateTimeInterface;
9
use Doctrine\DBAL\Platforms\AbstractPlatform;
10
use Doctrine\DBAL\Types\ConversionException;
11
use Doctrine\DBAL\Types\DateTimeType;
12
13
final class ChronosType extends DateTimeType
14
{
15
    /**
16
     * @return ($value is null ? null : string)
17
     */
18 2
    public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string
19
    {
20 2
        if ($value === null) {
21 1
            return $value;
22
        }
23
24 2
        if ($value instanceof DateTimeInterface) {
25 1
            return $value->format($platform->getDateTimeFormatString());
26
        }
27
28 1
        throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'Chronos']);
29
    }
30
31
    /**
32
     * @return ($value is null ? null : Chronos)
33
     */
34 2
    public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?Chronos
35
    {
36 2
        if ($value === null || $value instanceof Chronos) {
37 1
            return $value;
38
        }
39
40 2
        if (!is_string($value) && !$value instanceof DateTimeInterface) {
41 1
            throw ConversionException::conversionFailedFormat(
42 1
                $value,
43 1
                $this->getName(),
44 1
                $platform->getDateTimeFormatString(),
45 1
            );
46
        }
47
48 1
        $val = new Chronos($value);
49
50 1
        return $val;
51
    }
52
}
53