Completed
Push — develop ( de7f4b...5c2193 )
by Neomerx
09:05
created

TypeTrait::convertToDateTimeFromString()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

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