TimestampType   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 9
eloc 14
c 2
b 0
f 1
dl 0
loc 47
ccs 14
cts 15
cp 0.9333
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A convertToDatabaseValue() 0 4 3
A getSQLDeclaration() 0 3 1
A convertToPHPValue() 0 14 4
A getName() 0 3 1
1
<?php
2
/**
3
 * Licensed to CRATE Technology GmbH("Crate") under one or more contributor
4
 * license agreements.  See the NOTICE file distributed with this work for
5
 * additional information regarding copyright ownership.  Crate licenses
6
 * this file to you under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.  You may
8
 * 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, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
15
 * License for the specific language governing permissions and limitations
16
 * under the License.
17
 *
18
 * However, if you have executed another commercial license agreement
19
 * with Crate these terms will supersede the license and you may use the
20
 * software solely pursuant to the terms of the relevant commercial agreement.
21
 */
22
namespace Crate\DBAL\Types;
23
24
use DateTime;
25
use Doctrine\DBAL\Types\Type;
26
use Doctrine\DBAL\Platforms\AbstractPlatform;
27
28
/**
29
 * Type that maps a Crate SQL TIMESTAMP (aka Long) to a PHP DateTime object.
30
 */
31
32
class TimestampType extends Type
33
{
34
    const NAME = 'timestamp';
35
    const S_TO_MS = 1000;
36
37
    /**
38
     * Gets the name of this type.
39
     *
40
     * @return string
41
     */
42 54
    public function getName()
43
    {
44 54
        return self::NAME;
45
    }
46
47 48
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
48
    {
49 48
        return ($value !== null && $value instanceof DateTime)
50 48
            ? $value->getTimestamp()*self::S_TO_MS : null;
51
    }
52
53 6
    public function convertToPHPValue($value, AbstractPlatform $platform)
54
    {
55 6
        if ($value === null || $value instanceof DateTime) {
56 4
            return $value;
57
        }
58
59 4
        if (!is_int($value)) {
60
            return null;
61
        }
62
63 4
        $val = new DateTime();
64 4
        $val->setTimestamp($value/self::S_TO_MS);
65
66 4
        return $val;
67
    }
68
69
    /**
70
     * Gets the SQL declaration snippet for a field of this type.
71
     *
72
     * @return string
73
     * @param  array            $fieldDeclaration The field declaration.
74
     * @param  AbstractPlatform $platform         The currently used database platform.
75
     */
76 52
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
77
    {
78 52
        return $platform->getDateTimeTypeDeclarationSQL($fieldDeclaration);
79
    }
80
}
81