Completed
Push — develop ( 362d71...d2639f )
by
unknown
14s
created

DateConverter::getDateTimeFromString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * DateConverter class file
4
 */
5
6
namespace Graviton\DocumentBundle\Service;
7
8
use JsonSchema\Rfc3339;
9
10
/**
11
 * Date converter
12
 *
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  https://opensource.org/licenses/MIT MIT License
15
 * @link     http://swisscom.ch
16
 */
17
class DateConverter
18
{
19
20
    /**
21
     * @var string
22
     */
23
    private $dateFormat;
24
25
    /**
26
     * @var string
27
     */
28
    private $timezone;
29
30
    /**
31
     * Constructor
32
     *
33
     * @param string $dateFormat date format
34
     * @param string $timezone   timezone
35
     */
36
    public function __construct($dateFormat, $timezone)
37
    {
38
        $this->dateFormat = $dateFormat;
39
        $this->timezone = $timezone;
40
    }
41
42
    /**
43
     * get DateFormat
44
     *
45
     * @return string DateFormat
46
     */
47
    public function getDateFormat()
48
    {
49
        return $this->dateFormat;
50
    }
51
52
    /**
53
     * get Timezone
54
     *
55
     * @return string Timezone
56
     */
57
    public function getTimezone()
58
    {
59
        return $this->timezone;
60
    }
61
62
    /**
63
     * Returns a DateTime instance from a string representation
64
     *
65
     * @param string $data string Rfc3339 date
66
     *
67
     * @return \DateTime datetime
0 ignored issues
show
Documentation introduced by
Should the return type not be \DateTime|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
68
     */
69
    public function getDateTimeFromString($data)
70
    {
71
        return Rfc3339::createFromString($data);
72
    }
73
74
    /**
75
     * formats a DateTime to the defined default format
76
     *
77
     * @param \DateTime $dateTime datetime
78
     *
79
     * @return string formatted date
80
     */
81
    public function formatDateTime(\DateTime $dateTime)
82
    {
83
        return $dateTime->format($this->dateFormat);
84
    }
85
86
    /**
87
     * accepts a Rfc3339 datetime string and converts it to the defined default format
88
     *
89
     * @param string $data Rfc3339 datetime
90
     *
91
     * @return string date in configured format
92
     */
93
    public function getDateTimeStringInFormat($data)
94
    {
95
        return $this->formatDateTime(
96
            $this->getDateTimeFromString($data)
0 ignored issues
show
Bug introduced by
It seems like $this->getDateTimeFromString($data) can be null; however, formatDateTime() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
97
        );
98
    }
99
}
100