Completed
Push — master ( 29d040...599906 )
by Neomerx
04:36
created

JsonApiDateTimeType   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 3
dl 0
loc 44
rs 10
c 0
b 0
f 0
ccs 8
cts 8
cp 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A convertToPHPValue() 0 6 2
B convertToDatabaseValue() 0 20 6
1
<?php namespace Limoncello\Flute\Types;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use DateTime;
20
use DateTimeInterface;
21
use Doctrine\DBAL\Platforms\AbstractPlatform;
22
use Doctrine\DBAL\Types\ConversionException;
23
use Doctrine\DBAL\Types\DateTimeType;
24
25
/**
26
 * @package Limoncello\Flute
27
 */
28
class JsonApiDateTimeType extends DateTimeType
29
{
30
    /** Type name */
31
    const NAME = 'limoncelloDateTime';
32
33
    /** DateTime format */
34
    const JSON_API_FORMAT = DateBaseType::JSON_API_FORMAT;
35
36 1
    /**
37
     * @inheritdoc
38 1
     */
39
    public function convertToPHPValue($value, AbstractPlatform $platform)
40 1
    {
41
        $dateTimeOrNot = parent::convertToPHPValue($value, $platform);
42
43
        return $dateTimeOrNot instanceof DateTimeInterface ? new JsonApiDateTime($dateTimeOrNot) : $dateTimeOrNot;
44
    }
45
46
    /**
47
     * @inheritdoc
48 2
     *
49
     * @SuppressWarnings(PHPMD.StaticAccess)
50 2
     */
51 2
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
52
    {
53 1
        if ($value instanceof DateTimeInterface || $value === null) {
54
            $dateTime = $value;
55
        } elseif ($value instanceof JsonApiDateTime) {
56 1
            $dateTime = $value->getValue();
57
        } elseif (is_string($value) === true) {
58
            if (($dateTime = DateTime::createFromFormat(self::JSON_API_FORMAT, $value)) === false) {
59
                throw ConversionException::conversionFailed($value, $this->getName());
60
            }
61
        } else {
62
            throw ConversionException::conversionFailedInvalidType(
63
                $value,
64
                DateTimeInterface::class,
65
                [DateTimeInterface::class, JsonApiDateTime::class, 'string']
66
            );
67
        }
68
69
        return parent::convertToDatabaseValue($dateTime, $platform);
70
    }
71
}
72