Passed
Push — feature/job-builder/step-03-ui ( dabf2e...dabf2e )
by Xander
20:05 queued 13s
created

BaseModel::asDateTime()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 46
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8.0877

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 46
ccs 16
cts 18
cp 0.8889
rs 8.4444
c 0
b 0
f 0
cc 8
nc 7
nop 1
crap 8.0877
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model as Eloquent;
6
use Illuminate\Support\Facades\Config;
7
use Carbon\Carbon;
8
use DateTimeInterface;
9
use Jenssegers\Date\Date;
10
use DateTime;
11
12
abstract class BaseModel extends Eloquent {
13
    //Override date functions to return Jenssegers Data instead of Carbon
0 ignored issues
show
Coding Style introduced by
No space found before comment text; expected "// Override date functions to return Jenssegers Data instead of Carbon" but found "//Override date functions to return Jenssegers Data instead of Carbon"
Loading history...
14
15
    /**
16
     * Get a fresh timestamp for the model.
17
     *
18
     * @return Date
19
     */
20 71
    public function freshTimestamp()
0 ignored issues
show
introduced by
Method \App\Models\BaseModel::freshTimestamp() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Date".
Loading history...
21
    {
22 71
        return new Date;
23
    }
24
25
    /**
26
     * Return a timestamp as DateTime object.
27
     *
28
     * @param  mixed $value
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
29
     * @return Date
30
     */
31 71
    protected function asDateTime($value)
0 ignored issues
show
introduced by
Method \App\Models\BaseModel::asDateTime() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Date".
Loading history...
32
    {
33 71
        $timezone = Config::get('app.timezone');
34
35
        // If this value is already a Carbon instance, we shall just return it as is.
36
        // This prevents us having to re-instantiate a Carbon instance when we know
37
        // it already is one, which wouldn't be fulfilled by the DateTime check.
38 71
        if ($value instanceof Carbon) {
39 71
            return Date::parse($value, $timezone);
40
        }
41 71
        if ($value instanceof Date) {
42
            return $value;
43
        }
44
        // If the value is already a DateTime instance, we will just skip the rest of
45
        // these checks since they will be a waste of time, and hinder performance
46
        // when checking the field. We will just return the DateTime right away.
47 71
        if ($value instanceof DateTimeInterface) {
48 34
            return new Date(
49
                //$value->format('Y-m-d H:i:s.u'), $value->getTimeZone()
0 ignored issues
show
Coding Style introduced by
No space found before comment text; expected "// $value->format('Y-m-d H:i:s.u'), $value->getTimeZone()" but found "//$value->format('Y-m-d H:i:s.u'), $value->getTimeZone()"
Loading history...
50 34
                $value->format('Y-m-d H:i:s.u'), $timezone
51
            );
52
        }
53
        // If this value is an integer, we will assume it is a UNIX timestamp's value
54
        // and format a Carbon object from this timestamp. This allows flexibility
55
        // when defining your date fields as they might be UNIX timestamps here.
56 71
        if (is_numeric($value)) {
57
            return Date::createFromTimestamp($value, $timezone);
58
        }
59
        // If the value is in simply year, month, day format, we will instantiate the
60
        // Carbon instances from that format. Again, this provides for simple date
61
        // fields on the database, while still supporting Carbonized conversion.
62 71
        if (preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $value)) {
63 3
            return Date::createFromFormat('Y-m-d', $value, $timezone)->startOfDay();
64
        }
65
66
        // If the date follows the api configured date format, use that.
67 71
        $apiFormat = Config::get('app.api_datetime_format');
68 71
        $date = DateTime::createFromFormat($apiFormat, $value);
69 71
        if ($date && $date->format($apiFormat) == $value) {
70 2
            return $date;
71
        }
72
73
        // Finally, we will just assume this date is in the format used by default on
74
        // the database connection and use that format to create the Carbon object
75
        // that is returned back out to the developers after we convert it here.
76 71
        return Date::createFromFormat($this->getDateFormat(), $value, $timezone);
77
    }
78
79
    /**
80
     * // Ensure that models serialized using toArray() or toJson() use the api-specific date format
81
     *
82
     * @param DateTimeInterface $date
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
83
     * @return void
1 ignored issue
show
Coding Style introduced by
Function return type is void, but function contains return statement
Loading history...
84
     */
85 71
    protected function serializeDate(DateTimeInterface $date)
0 ignored issues
show
introduced by
Method \App\Models\BaseModel::serializeDate() does not have return type hint for its return value but it should be possible to add it based on @return annotation "void".
Loading history...
86
    {
87 71
        return $date->format(Config::get('app.api_datetime_format'));
88
    }
89
}
90