Passed
Push — master ( 6d6669...feea48 )
by Andrey
03:52
created

DateTimes::getDateTimeFromTo()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 7
nc 5
nop 0
dl 0
loc 12
rs 9.6111
c 1
b 0
f 0
1
<?php
2
3
namespace Daaner\NovaPoshta\Traits;
4
5
use Carbon\Carbon;
6
use Exception;
7
use Illuminate\Support\Facades\Date;
8
9
trait DateTimes
10
{
11
    protected $dateTime;
12
    protected $dateTimeFrom;
13
    protected $dateBegin;
14
    protected $dateEnd;
15
    protected $dateTimeTo;
16
    protected $format = 'd.m.Y';
17
    protected $formatTime = 'd.m.Y H:i:s';
18
19
    /**
20
     * @param  string|Carbon|date  $dateTime Указание даты
21
     * @return void
22
     */
23
    public function setDateTime($dateTime): void
24
    {
25
        $this->dateTime = $this->checkDate($dateTime);
26
    }
27
28
    /**
29
     * @param  string|Carbon|date  $dateTimeFrom С текущей даты
30
     * @return void
31
     */
32
    public function setDateTimeFrom($dateTimeFrom): void
33
    {
34
        $this->dateTimeFrom = $this->checkDate($dateTimeFrom);
35
    }
36
37
    /**
38
     * @param  string|Carbon|date  $dateTimeTo До текущей даты
39
     * @return void
40
     */
41
    public function setDateTimeTo($dateTimeTo): void
42
    {
43
        $this->dateTimeTo = $this->checkDate($dateTimeTo);
44
    }
45
46
    /**
47
     * @return void
48
     */
49
    public function getDateTime(): void
50
    {
51
        if ($this->dateTime && (! $this->dateTimeFrom || ! $this->dateTimeTo)) {
52
            $this->methodProperties['DateTime'] = $this->dateTime;
53
        }
54
55
        if (! $this->dateTime) {
56
            $this->dateTime = Carbon::now()->format($this->format);
57
            $this->methodProperties['DateTime'] = $this->dateTime;
58
        }
59
    }
60
61
    /**
62
     * @return void
63
     */
64
    public function getDateTimeFromTo(): void
65
    {
66
        if ($this->dateTimeFrom || $this->dateTimeTo) {
67
            if (! $this->dateTimeTo) {
68
                $this->dateTimeTo = Carbon::now()->format($this->format);
69
            }
70
            if (! $this->dateTimeFrom) {
71
                $this->dateTimeFrom = $this->dateTimeTo;
72
            }
73
74
            $this->methodProperties['DateTimeFrom'] = $this->dateTimeFrom;
75
            $this->methodProperties['DateTimeTo'] = $this->dateTimeTo;
76
        }
77
    }
78
79
    /**
80
     * Странно, но тут с минутами и секундами.
81
     *
82
     * @param  string|Carbon|date|null  $from С текущей даты
83
     * @param  string|Carbon|date|null  $to До текущей даты
84
     * @return void
85
     */
86
    public function getDateFromTo($from = null, $to = null): void
87
    {
88
        // DateFrom
89
        if ($from) {
90
            $from = $this->checkDate($from, $this->formatTime);
91
        } else {
92
            $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

92
            $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...
93
        }
94
95
        // DateTo
96
        if ($to) {
97
            $to = $this->checkDate($to, $this->formatTime);
98
        } else {
99
            $to = Carbon::now()->format($this->formatTime);
100
        }
101
102
        $this->methodProperties['DateFrom'] = $from;
103
        $this->methodProperties['DateTo'] = $to;
104
    }
105
106
    /**
107
     * Проверка даты на валидность.
108
     *
109
     * @param string|Carbon|date $date Дата
110
     * @param string|null $format Формат даты
111
     * @return string $date
112
     */
113
    public function checkDate($date, ?string $format = null): string
114
    {
115
        if (! $format) {
116
            $format = $this->format;
117
        }
118
119
        if ($date instanceof Carbon) {
120
            $date = $date->format($format);
121
        } else {
122
            try {
123
                $date = Carbon::parse($date)->format($format);
124
            } catch (Exception $e) {
125
                $date = Carbon::now()->format($format);
126
            }
127
        }
128
129
        return $date;
130
    }
131
132
    /**
133
     * @param string|Carbon|date $dateBegin Дата начала
134
     */
135
    public function setDateBegin($dateBegin)
136
    {
137
        $this->dateBegin = $this->checkDate($dateBegin, $this->format);
138
    }
139
140
    /**
141
     * @param string|Carbon|date $dateEnd Дата окончания
142
     */
143
    public function setDateEnd($dateEnd)
144
    {
145
        $this->dateEnd = $this->checkDate($dateEnd, $this->format);
146
    }
147
148
149
    public function getDateBeginEnd()
150
    {
151
        if($this->dateBegin) {
152
            $this->methodProperties['BeginDate'] = $this->dateBegin;
153
        }
154
155
        if($this->dateEnd) {
156
            $this->methodProperties['EndDate'] = $this->dateEnd;
157
        }
158
    }
159
160
161
}
162