Passed
Pull Request — master (#32)
by Douglas
02:39
created

Timestamp::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the AntiMattr MongoDB Migrations Library, a library by Matthew Fitzgerald.
5
 *
6
 * (c) 2014 Matthew Fitzgerald
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace AntiMattr\MongoDB\Migrations\Configuration;
13
14
/**
15
 * This class is to normalise the potential values from the 't' version
16
 * attribute.
17
 *
18
 * @author Douglas Reith <[email protected]>
19
 */
20
class Timestamp
21
{
22
    private $t;
23
24
    /**
25
     * __construct.
26
     *
27
     * @param mixed $t
28
     */
29 6
    public function __construct($t)
30
    {
31 6
        $this->t = $t;
32 6
    }
33
34
    /**
35
     * __invoke.
36
     *
37
     * @param mixed $t
38
     *
39
     * @return int
40
     */
41
    public function __invoke($t): int
42
    {
43
        return (new static($t))->getTimestamp();
44
    }
45
46
    /**
47
     * getTimestamp.
48
     *
49
     * Normalise based on the different options for backward/forward
50
     * compatibility
51
     *
52
     * @return int Time in seconds since 1970
53
     */
54 6
    public function getTimestamp(): int
55
    {
56 6
        $supportedClasses = implode(
57 6
            ', ',
58
            [
59 6
                '\MongoTimestamp',
60
                '\MongoDate',
61
                '\MongoDB\BSON\Timestamp',
62
                '\MongoDB\BSON\UTCDateTime',
63
                '\DateTimeInterface',
64
            ]
65
        );
66
67 6
        if (!$this->t || !is_object($this->t)) {
68 1
            throw new \DomainException(
69 1
                'The timestamp to normalise must be one of ' . $supportedClasses . ' but it is not an object'
70
            );
71
        }
72
73 5
        switch (get_class($this->t)) {
74 5
            case 'MongoTimestamp':
75
                return (int) $this->t->__toString();
76
77 5
            case 'MongoDate':
78
                return $this->t->sec;
79
80 5
            case 'MongoDB\BSON\Timestamp':
81 1
                return $this->t->getTimestamp();
82
83 4
            case 'MongoDB\BSON\UTCDateTime':
84 1
                return (int) $this->t->toDateTime()->format('U');
85
        }
86
87 3
        if ($this->t instanceof \DateTimeInterface) {
88 2
            return $this->t->getTimestamp();
89
        }
90
91 1
        throw new \DomainException(
92 1
            'The normalised timestamp must be one of ' . $supportedClasses
93
        );
94
    }
95
96
    /**
97
     * __toString.
98
     *
99
     * @return string
100
     */
101 1
    public function __toString(): string
102
    {
103 1
        return (string) $this->getTimestamp();
104
    }
105
}
106