DateTimeType::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/*
3
* This file is a part of graphql-youshido project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 11/27/15 1:22 AM
7
*/
8
9
namespace Youshido\GraphQL\Type\Scalar;
10
11
class DateTimeType extends AbstractScalarType
12
{
13
14
    private $format;
15
16 9
    public function __construct($format = 'Y-m-d H:i:s')
17
    {
18 9
        $this->format = $format;
19 9
    }
20
21 1
    public function getName()
22
    {
23 1
        return 'DateTime';
24
    }
25
26 7 View Code Duplication
    public function isValidValue($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
    {
28 7
        if ((is_object($value) && $value instanceof \DateTimeInterface) || is_null($value)) {
29 6
            return true;
30 3
        } else if (is_string($value)) {
31 3
            $date = $this->createFromFormat($value);
32 3
        } else {
33
            $date = null;
34
        }
35
36 3
        return $date ? true : false;
37
    }
38
39 7 View Code Duplication
    public function serialize($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41 7
        $date = null;
42
43 7
        if (is_string($value)) {
44
            $date = $this->createFromFormat($value);
45 7
        } elseif ($value instanceof \DateTimeInterface) {
46 4
            $date = $value;
47 4
        }
48
49 7
        return $date ? $date->format($this->format) : null;
50
    }
51
52 6 View Code Duplication
    public function parseValue($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54 6
        if (is_string($value)) {
55 3
            $date = $this->createFromFormat($value);
56 6
        } elseif ($value instanceof \DateTimeInterface) {
57 1
            $date = $value;
58 1
        } else {
59 3
            $date = false;
60
        }
61
62 6
        return $date ?: null;
63
    }
64
65 3
    private function createFromFormat($value)
66
    {
67 3
        return \DateTime::createFromFormat($this->format, $value);
68
    }
69
70 1
    public function getDescription()
71
    {
72 1
        return sprintf('Representation of date and time in "%s" format', $this->format);
73
    }
74
75
}
76