Passed
Push — master ( e9591a...c0a685 )
by Rutger
13:20
created

DateTimeBehavior   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 95.83%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 24
c 1
b 0
f 0
dl 0
loc 62
ccs 23
cts 24
cp 0.9583
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeSave() 0 12 5
A events() 0 8 1
A afterFind() 0 14 4
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\models\behaviors;
4
5
use yii\base\Behavior;
6
use yii\db\ActiveRecord;
7
use yii\db\BaseActiveRecord;
8
use yii\db\Schema;
9
10
/**
11
 * @property BaseActiveRecord $owner
12
 */
13
class DateTimeBehavior extends Behavior
14
{
15
    /**
16
     * The DateTime format to convert to before inserting the DateTime object into the database.
17
     * @var string
18
     */
19
    public $dateTimeFormat = 'Y-m-d H:i:s';
20
21
    /**
22
     * @inheritDoc
23
     */
24 89
    public function events()
25
    {
26 89
        return [
27 89
            ActiveRecord::EVENT_BEFORE_INSERT => 'beforeSave',
28 89
            ActiveRecord::EVENT_BEFORE_UPDATE => 'beforeSave',
29 89
            ActiveRecord::EVENT_AFTER_INSERT => 'afterFind',
30 89
            ActiveRecord::EVENT_AFTER_UPDATE => 'afterFind',
31 89
            ActiveRecord::EVENT_AFTER_FIND => 'afterFind',
32 89
        ];
33
    }
34
35
    /**
36
     * Convert DateTime objects to string before writing the database.
37
     * @throws \yii\base\InvalidConfigException
38
     * @since 1.0.0
39
     */
40 1
    public function beforeSave()
41
    {
42
        /** @var ActiveRecord $activeRecord */
43 1
        $activeRecord = $this->owner;
44
45 1
        foreach ($activeRecord->getTableSchema()->columns as $column) {
46 1
            $value = $this->owner[$column->name];
47
            if (
48 1
                in_array($column->type, [Schema::TYPE_DATE, Schema::TYPE_DATETIME, Schema::TYPE_TIMESTAMP])
49 1
                && ($value instanceof \DateTime || $value instanceof \DateTimeImmutable)
50
            ) {
51
                $this->owner[$column->name] = $value->format($this->dateTimeFormat);
52
            }
53
        }
54
    }
55
56
    /**
57
     * Convert strings to DateTime objects after loading them from the database.
58
     * @throws \yii\base\InvalidConfigException
59
     * @since 1.0.0
60
     */
61 14
    public function afterFind()
62
    {
63
        /** @var ActiveRecord $activeRecord */
64 14
        $activeRecord = $this->owner;
65
66 14
        foreach ($activeRecord->getTableSchema()->columns as $column) {
67 14
            $value = $this->owner[$column->name];
68
            if (
69 14
                in_array($column->type, [Schema::TYPE_DATETIME, Schema::TYPE_DATETIME, Schema::TYPE_TIMESTAMP])
70 14
                && !empty($value)
71
            ) {
72 6
                $dateTime = \DateTimeImmutable::createFromFormat($this->dateTimeFormat, $value);
73 6
                $this->owner[$column->name] = $dateTime;
74 6
                $this->owner->setOldAttribute($column->name, $dateTime);
75
            }
76
        }
77
    }
78
}
79