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
Push — analysis-qMkPe2 ( 6a4fd8 )
by butschster
09:14
created

DateTime::setFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Form\Element;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Facades\Log;
7
use SleepingOwl\Admin\Traits\DateFormat;
8
use SleepingOwl\Admin\Traits\DatePicker;
9
10
class DateTime extends NamedFormElement
11
{
12
    use DatePicker, DateFormat;
13
14
    /**
15
     * @var string
16
     */
17
    protected $format = 'Y-m-d H:i:s';
18
19
    /**
20
     * @var string
21
     */
22
    protected $timezone;
23
24
    /**
25
     * @var bool
26
     */
27
    protected $seconds = false;
28
29
    /**
30
     * @var string
31
     */
32
    protected $view = 'form.element.datetime';
33
34
    /**
35
     * @return $this|NamedFormElement|mixed|null|string
36
     */
37
    public function getValueFromModel()
38
    {
39
        $value = parent::getValueFromModel();
40
        if (! empty($value)) {
41
            return $this->parseValue($value);
42
        }
43
    }
44
45
    /**
46
     * @return bool
47
     */
48
    public function hasSeconds()
49
    {
50
        return (bool) $this->seconds;
51
    }
52
53
    /**
54
     * @param bool $seconds
55
     *
56
     * @return $this
57
     */
58
    public function setSeconds($seconds)
59
    {
60
        $this->seconds = $seconds;
61
62
        return $this;
63
    }
64
65
    /**
66
     * @param mixed $value
67
     *
68
     * @return void
69
     */
70
    public function setModelAttribute($value)
71
    {
72
        $value = ! empty($value)
73
            ? Carbon::createFromFormat($this->getPickerFormat(), $value, $this->getTimezone())
74
                ->timezone(config('app.timezone'))->format($this->getFormat())
75
            : null;
76
77
        parent::setModelAttribute($value);
78
    }
79
80
    /**
81
     * @return array
82
     */
83
    public function toArray()
84
    {
85
        $this->setHtmlAttributes([
86
            'data-date-format' => $this->getJsPickerFormat(),
87
            'data-date-pickdate' => 'true',
88
            'data-date-picktime' => 'false',
89
            'data-date-useseconds' => $this->hasSeconds() ? 'true' : 'false',
90
            'class' => 'form-control',
91
            'type' => 'text',
92
        ]);
93
94
        return parent::toArray() + [
95
                'seconds' => $this->hasSeconds(),
96
                'format' => $this->getFormat(),
97
                'pickerFormat' => $this->getJsPickerFormat(),
98
            ];
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getPickerFormat()
105
    {
106
        return $this->pickerFormat ?: config('sleeping_owl.datetimeFormat');
107
    }
108
109
    /**
110
     * @return $this
111
     *
112
     * SMELLS This function does more than it says.
113
     */
114
    public function setCurrentDate()
115
    {
116
        $this->defaultValue = Carbon::now()->timezone($this->getTimezone())->format($this->getFormat());
117
118
        return $this;
119
    }
120
121
    /**
122
     * @param $value mixed
123
     * @return string|void
124
     */
125
    public function parseValue($date)
126
    {
127
        if (empty($date)) {
128
            return;
129
        }
130
        if (! $date instanceof Carbon) {
131
            try {
132
                $date = Carbon::parse($date);
133
            } catch (\Exception $e) {
134
                try {
135
                    $date = Carbon::createFromFormat($this->getPickerFormat(), $date);
136
                } catch (\Exception $e) {
137
                    Log::error('Unable to parse date!', [
138
                        'format' => $this->getPickerFormat(),
139
                        'date' => $date,
140
                        'exception' => $e,
141
                    ]);
142
143
                    return;
144
                }
145
            }
146
        }
147
148
        return $date->timezone($this->getTimezone())->format($this->getPickerFormat());
149
    }
150
}
151