Completed
Push — master ( 2b7730...10405b )
by wen
11:53
created

DateRange::getValue()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 8.7624
cc 6
eloc 13
nc 4
nop 0
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
/**
6
 * Form Element DateRange
7
 *
8
 * @package Sco\Admin\Form\Elements
9
 * @see http://element.eleme.io/#/en-US/component/date-picker
10
 */
11
class DateRange extends Date
12
{
13
    protected $type = 'daterange';
14
15
    protected $defaultValue = [];
16
17
    protected $pickerFormat = 'yyyy-MM-dd';
18
19
    protected $attributes = [];
20
21
    public function __construct($name, string $title)
22
    {
23
        $name = (array) $name;
24
25
        $this->attributes = $name;
26
27
        if (count($this->attributes) == 1) {
28
            $this->setCast('json');
29
        }
30
31
        parent::__construct(implode('_', $name), $title);
32
    }
33
34
    public function getValue()
35
    {
36
        $model = $this->getModel();
37
        $value = $this->getDefaultValue();
38
        if (is_null($model) || ! $model->exists) {
39
            return $value;
40
        }
41
42
        if (count($this->attributes) == 2) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
43
        } else {
44
            $value = $model->getAttribute(array_shift($this->attributes));
45
            if (($value = json_decode($value, true)) === false || is_null($value)) {
46
                $value = explode(',', $value);
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
47
            }
48
        }
49
50
        return [
51
            $this->dateToString($model->getAttribute($this->getStartName())),
52
            $this->dateToString($model->getAttribute($this->getEndName())),
53
        ];
54
    }
55
56
    protected function getDefaultValidationRules()
57
    {
58
        return [
59
            'array' => 'array',
60
        ];
61
    }
62
63
    public function save()
64
    {
65
        $model = $this->getModel();
66
67
        list($startValue, $endValue) = $this->getValueFromRequest();
68
        $model->setAttribute($this->getStartName(), $this->prepareValue($startValue));
69
        $model->setAttribute($this->getEndName(), $this->prepareValue($endValue));
70
    }
71
}
72