Passed
Push — master ( 3713cc...07ea52 )
by Andrey
04:19
created

DateTimes::getDateFromTo()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 23
c 1
b 0
f 0
nc 16
nop 2
dl 0
loc 36
rs 8.6186
1
<?php
2
3
namespace Daaner\NovaPoshta\Traits;
4
5
use Carbon\Carbon;
6
7
trait DateTimes
8
{
9
    protected $dateTime;
10
    protected $dateTimeFrom;
11
    protected $dateTimeTo;
12
    protected $format = 'd.m.Y';
13
    protected $formatTime = 'd.m.Y H:i:s';
14
15
    /**
16
     * @param string|Carbon|date $dateTime
17
     * @return this
18
     */
19
    public function setDateTime($dateTime)
20
    {
21
        $this->dateTime = $this->checkDate($dateTime);
22
23
        return $this;
24
    }
25
26
    /**
27
     * @param string|Carbon|date $dateTimeFrom
28
     * @return this
29
     */
30
    public function setDateTimeFrom($dateTimeFrom)
31
    {
32
        $this->dateTimeFrom = $this->checkDate($dateTimeFrom);
33
34
        return $this;
35
    }
36
37
    /**
38
     * @param string|Carbon|date $dateTimeTo
39
     * @return this
40
     */
41
    public function setDateTimeTo($dateTimeTo)
42
    {
43
        $this->dateTimeTo = $this->checkDate($dateTimeTo);
44
45
        return $this;
46
    }
47
48
    /**
49
     * @return this
50
     */
51
    public function getDateTime()
52
    {
53
        if ($this->dateTime && (! $this->dateTimeFrom || ! $this->dateTimeTo)) {
54
            $this->methodProperties['DateTime'] = $this->dateTime;
55
        }
56
57
        if (! $this->dateTime) {
58
            $this->dateTime = Carbon::now()->format($this->format);
59
            $this->methodProperties['DateTime'] = $this->dateTime;
60
        }
61
62
        return $this;
63
    }
64
65
    /**
66
     * @return this
67
     */
68
    public function getDateTimeFromTo()
69
    {
70
        if ($this->dateTimeFrom || $this->dateTimeTo) {
71
            if (! $this->dateTimeTo) {
72
                $this->dateTimeTo = Carbon::now()->format($this->format);
73
            }
74
            if (! $this->dateTimeFrom) {
75
                $this->dateTimeFrom = $this->dateTimeTo;
76
            }
77
78
            $this->methodProperties['DateTimeFrom'] = $this->dateTimeFrom;
79
            $this->methodProperties['DateTimeTo'] = $this->dateTimeTo;
80
        }
81
82
        return $this;
83
    }
84
85
    /**
86
     * Странно, но тут с минутами и секундами
87
     * @param string|Carbon|date|null $from
88
     * @param string|Carbon|date|null $to
89
     * @return this
90
     */
91
    public function getDateFromTo($from = null, $to = null)
92
    {
93
        // DateFrom
94
        if ($from) {
95
            if ($from instanceof Carbon) {
96
                $from = $from->format($this->formatTime);
97
            } else {
98
                try {
99
                    $from = Carbon::parse($from)->format($this->formatTime);
100
                } catch (\Exception $e) {
101
                    $from = Carbon::now()->addMonth(-3)->format($this->formatTime);
0 ignored issues
show
Unused Code introduced by
The call to Carbon\Carbon::addMonth() has too many arguments starting with -3. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
                    $from = Carbon::now()->/** @scrutinizer ignore-call */ addMonth(-3)->format($this->formatTime);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
102
                }
103
            }
104
        } else {
105
            $from = Carbon::now()->addMonth(-3)->format($this->formatTime);
106
        }
107
108
        // DateTo
109
        if ($to) {
110
            if ($to instanceof Carbon) {
111
                $to = $to->format($this->formatTime);
112
            } else {
113
                try {
114
                    $to = Carbon::parse($to)->format($this->formatTime);
115
                } catch (\Exception $e) {
116
                    $to = Carbon::now()->format($this->formatTime);
117
                }
118
            }
119
        } else {
120
            $to = Carbon::now()->format($this->formatTime);
121
        }
122
123
        $this->methodProperties['DateFrom'] = $from;
124
        $this->methodProperties['DateTo'] = $to;
125
126
        return $this;
127
    }
128
129
    /**
130
     * @param string|Carbon $date
131
     * @return string $date
132
     */
133
    public function checkDate($date)
134
    {
135
        if ($date instanceof Carbon) {
136
            $date = $date->format($this->format);
137
        } else {
138
            try {
139
                $date = Carbon::parse($date)->format($this->format);
140
            } catch (\Exception $e) {
141
                $date = Carbon::now()->format($this->format);
142
            }
143
        }
144
145
        return $date;
146
    }
147
}
148