Completed
Push — master ( 1a67d2...dfab50 )
by Neomerx
11:35 queued 22s
created

TypeTrait::convertToJsonApiDateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
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 DateTimeInterface;
20
use Doctrine\DBAL\Types\ConversionException;
21
use Limoncello\Flute\Types\DateTime as JsonApiDateTime;
22
23
/**
24
 * @package Limoncello\Flute
25
 */
26
trait TypeTrait
27
{
28
    /**
29
     * @param DateTimeInterface $dateTime
30
     *
31
     * @return JsonApiDateTime
32
     */
33 36
    private function convertToJsonApiDateTime(DateTimeInterface $dateTime): JsonApiDateTime
34
    {
35 36
        $utcTimestamp = $dateTime->getTimestamp();
36
37
        // yes, PHP DateTime constructor can only accept timestamp as a string ¯\_( ͡° ͜ʖ ͡°)_/¯
38 36
        return new JsonApiDateTime("@$utcTimestamp");
39
    }
40
41
    /**
42
     * @param string|DateTimeInterface $value
43
     * @param string                   $nonJsonFormat
44
     * @param string                   $typeName
45
     *
46
     * @return DateTimeInterface|null
47
     *
48
     * @throws ConversionException
49
     *
50
     * @SuppressWarnings(PHPMD.StaticAccess)
51
     * @SuppressWarnings(PHPMD.ElseExpression)
52
     */
53 17
    private function convertToDateTimeFromString(
54
        $value,
55
        string $nonJsonFormat,
56
        string $typeName
57
    ): ?DateTimeInterface {
58 17
        if ($value instanceof DateTimeInterface || $value === null) {
59 4
            $result = $value;
60 13
        } elseif (is_string($value) === true) {
61 11
            $result = JsonApiDateTime::createFromFormat($nonJsonFormat, $value);
62 11
            $result = $result !== false ?
63 11
                $result : JsonApiDateTime::createFromFormat(JsonApiDateTime::JSON_API_FORMAT, $value);
64 11
            if ($result === false) {
65 11
                throw ConversionException::conversionFailed($value, $typeName);
66
            }
67
        } else {
68 2
            throw ConversionException::conversionFailedInvalidType(
69 2
                $value,
70 2
                DateTimeInterface::class,
71 2
                [DateTimeInterface::class, JsonApiDateTime::class, 'string']
72
            );
73
        }
74
75 13
        return $result;
76
    }
77
}
78