LocalDateMapper   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 95.83%

Importance

Changes 0
Metric Value
dl 0
loc 78
c 0
b 0
f 0
wmc 13
lcom 0
cbo 2
ccs 23
cts 24
cp 0.9583
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getOptions() 0 4 1
A slumber() 0 19 3
B awake() 0 22 8
1
<?php
2
/**
3
 * File was created 30.09.2015 07:32
4
 */
5
6
namespace PeekAndPoke\Component\Slumber\Data\MongoDb\Types;
7
8
use MongoDB\BSON\UTCDateTime;
9
use PeekAndPoke\Component\Psi\Psi\IsDateString;
10
use PeekAndPoke\Component\Slumber\Annotation\Slumber\AsLocalDate;
11
use PeekAndPoke\Component\Slumber\Core\Codec\Awaker;
12
use PeekAndPoke\Component\Slumber\Core\Codec\Property\AbstractPropertyMapper;
13
use PeekAndPoke\Component\Slumber\Core\Codec\Slumberer;
14
use PeekAndPoke\Types\LocalDate;
15
16
/**
17
 * @author Karsten J. Gerber <[email protected]>
18
 */
19
class LocalDateMapper extends AbstractPropertyMapper
20
{
21
    /** @var AsLocalDate */
22
    private $options;
23
24
    /**
25
     * C'tor.
26
     *
27
     * @param AsLocalDate $options
28
     */
29 23
    public function __construct(AsLocalDate $options)
30
    {
31 23
        $this->options = $options;
32 23
    }
33
34
    /**
35
     * @return AsLocalDate
36
     */
37 1
    public function getOptions()
38
    {
39 1
        return $this->options;
40
    }
41
42
    /**
43
     * @param Slumberer $slumberer
44
     * @param mixed     $value
45
     *
46
     * @return array
47
     */
48 33
    public function slumber(Slumberer $slumberer, $value)
49
    {
50 33
        if (! $value instanceof LocalDate) {
51 11
            return null;
52
        }
53
54 22
        $date = $value->getDate();
55
56 22
        if (! $date instanceof \DateTimeInterface) {
57
            return null;
58
        }
59
60 22
        $millis = ($date->getTimestamp() * 1000) + ((int) ($date->format('u') / 1000));
61
62
        return [
63 22
            'date' => new UTCDateTime($millis),
64 22
            'tz'   => $value->getTimezone()->getName(),
65
        ];
66
    }
67
68
    /**
69
     * @param Awaker $awaker
70
     * @param mixed  $value
71
     *
72
     * @return LocalDate
73
     */
74 29
    public function awake(Awaker $awaker, $value)
75
    {
76
        // default cases first
77 29
        $canAccess     = $value instanceof \ArrayAccess || \is_array($value);
78 29
        $hasComponents = $canAccess && isset($value['date'], $value['tz']);
79
80 29
        if ($canAccess && $hasComponents && $value['date'] instanceof \DateTime) {
81 20
            return new LocalDate($value['date'], $value['tz']);
82
        }
83
84
        // compatibility in case a SimpleDate was changed to a LocalDate
85 9
        if ($value instanceof \DateTime) {
86 1
            return LocalDate::raw($value);
87
        }
88
89
        // compatibility in case a string was change to a LocalDate
90 8
        if (IsDateString::isValidDateString($value)) {
91 2
            return LocalDate::raw(new \DateTime($value));
92
        }
93
94 6
        return null;
95
    }
96
}
97