Completed
Push — master ( 582431...151715 )
by Karsten
02:40
created

LocalDateMapper::awake()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 22
Code Lines 10

Duplication

Lines 3
Ratio 13.64 %

Code Coverage

Tests 5
CRAP Score 13.125

Importance

Changes 0
Metric Value
dl 3
loc 22
ccs 5
cts 10
cp 0.5
rs 6.9811
c 0
b 0
f 0
cc 7
eloc 10
nc 8
nop 2
crap 13.125
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\Functions\Unary\Matcher\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
    public function __construct(AsLocalDate $options)
30
    {
31
        $this->options = $options;
32
    }
33
34
    /**
35
     * @return AsLocalDate
36
     */
37
    public function getOptions()
38
    {
39
        return $this->options;
40
    }
41
42
    /**
43
     * @param Slumberer $slumberer
44
     * @param mixed     $value
45
     *
46
     * @return array
47
     */
48 21 View Code Duplication
    public function slumber(Slumberer $slumberer, $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...
49
    {
50 21
        if (! $value instanceof LocalDate) {
51 4
            return null;
52
        }
53
54
        return [
55 17
            'date' => new UTCDateTime($value->getTimestamp() * 1000),
56 17
            'tz'   => $value->getTimezone()->getName(),
57
        ];
58
    }
59
60
    /**
61
     * @param Awaker $awaker
62
     * @param mixed  $value
63
     *
64
     * @return LocalDate
65
     */
66 17
    public function awake(Awaker $awaker, $value)
67
    {
68
        // default cases first
69 17
        $canAccess     = $value instanceof \ArrayAccess || is_array($value);
70 17
        $hasComponents = isset($value['date'], $value['tz']);
71
72 17 View Code Duplication
        if ($canAccess && $hasComponents && $value['date'] instanceof \DateTime) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
73 17
            return new LocalDate($value['date'], $value['tz']);
74
        }
75
76
        // compatibility in case a SimpleDate was changed to a LocalDate
77
        if ($value instanceof \DateTime) {
78
            return LocalDate::raw($value);
79
        }
80
81
        // compatibility in case a string was change to a LocalDate
82
        if (IsDateString::isValidDateString($value)) {
83
            return LocalDate::raw(new \DateTime($value));
84
        }
85
86
        return null;
87
    }
88
}
89