GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#688)
by
unknown
18:52
created

DateTime::hasSeconds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Form\Element;
4
5
use Exception;
6
use Carbon\Carbon;
7
8
class DateTime extends NamedFormElement
9
{
10
    use \SleepingOwl\Admin\Traits\DatePicker;
11
12
    /**
13
     * @var string
14
     */
15
    protected $format = 'Y-m-d H:i:s';
16
17
    /**
18
     * @var string
19
     */
20
    protected $timezone;
21
22
    /**
23
     * @var bool
24
     */
25
    protected $seconds = false;
26
27
    /**
28
     * @var string
29
     */
30
    protected $view = 'form.element.datetime';
31
32
    /**
33
     * @return string
34
     */
35 1
    public function getFormat()
36
    {
37 1
        return $this->format;
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function getTimezone()
44
    {
45
        if (is_null($this->timezone)) {
46
            $this->timezone = config('sleeping_owl.timezone');
47
        }
48
49
        return $this->timezone;
50
    }
51
52
    /**
53
     * @param string|null $format
54
     *
55
     * @return $this
56
     */
57 1
    public function setFormat($format)
58
    {
59 1
        $this->format = $format;
60
61 1
        return $this;
62
    }
63
64
    /**
65
     * @param string $timezone
66
     *
67
     * @return $this
68
     */
69
    public function setTimezone($timezone)
70
    {
71
        $this->timezone = $timezone;
72
73
        return $this;
74
    }
75
76
    /**
77
     * @return $this|NamedFormElement|mixed|null|string
78
     */
79
    public function getValueFromModel()
80
    {
81
        $value = parent::getValueFromModel();
82
        if (! empty($value)) {
83
            return $this->parseValue($value);
84
        }
85
    }
86
87
    /**
88
     * @return bool
89
     */
90
    public function hasSeconds()
91
    {
92
        return (bool) $this->seconds;
93
    }
94
95
    /**
96
     * @param bool $seconds
97
     *
98
     * @return $this
99
     */
100
    public function setSeconds($seconds)
101
    {
102
        $this->seconds = $seconds;
103
104
        return $this;
105
    }
106
107
    /**
108
     * @param mixed $value
109
     *
110
     * @return void
111
     */
112 View Code Duplication
    public function setModelAttribute($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...
113
    {
114
        $value = ! empty($value)
115
            ? Carbon::createFromFormat($this->getPickerFormat(), $value, $this->getTimezone())
116
                    ->timezone(config('app.timezone'))->format($this->getFormat())
117
            : null;
118
119
        parent::setModelAttribute($value);
120
    }
121
122
    /**
123
     * @return array
124
     */
125
    public function toArray()
126
    {
127
        $this->setHtmlAttributes([
128
            'data-date-format'     => $this->getJsPickerFormat(),
129
            'data-date-pickdate'   => 'true',
130
            'data-date-picktime'   => 'false',
131
            'data-date-useseconds' => $this->hasSeconds() ? 'true' : 'false',
132
            'class'                => 'form-control',
133
            'type'                 => 'text',
134
        ]);
135
136
        return parent::toArray() + [
137
                'seconds'      => $this->hasSeconds(),
138
                'format'       => $this->getFormat(),
139
                'pickerFormat' => $this->getJsPickerFormat(),
140
            ];
141
    }
142
143
    /**
144
     * @return string
145
     */
146 1
    public function getPickerFormat()
147
    {
148 1
        return $this->pickerFormat ?: config('sleeping_owl.datetimeFormat');
149
    }
150
151
    /**
152
     * @return $this
153
     *
154
     * SMELLS This function does more than it says.
155
     */
156
    public function setCurrentDate()
157
    {
158
        $this->defaultValue = Carbon::now()->timezone($this->getTimezone())->format($this->getFormat());
159
160
        return $this;
161
    }
162
163
    /**
164
     * @param string $value
165
     *
166
     * @return string|void
167
     */
168 View Code Duplication
    protected function parseValue($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...
169
    {
170
        try {
171
            $time = Carbon::parse($value);
172
        } catch (Exception $e) {
173
            \Log::info('unable to parse date, re-trying with given format', [
174
                'exception' => $e,
175
                'date'      => $value,
176
            ]);
177
            try {
178
                $time = Carbon::createFromFormat($this->getPickerFormat(), $value);
179
            } catch (Exception $e) {
180
                \Log::error('unable to parse date!', [
181
                    'exception'     => $e,
182
                    'pickerFormat'  => $this->getPickerFormat(),
183
                    'date'          => $value,
184
                ]);
185
186
                return;
187
            }
188
        }
189
190
        return $time->timezone($this->getTimezone())->format(
191
            $this->getPickerFormat()
192
        );
193
    }
194
}
195