DatabaseItem   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 99
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
getDbDateFormat() 0 1 ?
A getCreatedAt() 0 11 4
A getUpdatedAt() 0 11 4
A setCreatedAtImpl() 0 6 1
A setUpdatedAtImpl() 0 6 1
A parseDateTime() 0 4 1
A hasDynamicProperty() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Passport\Entities;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use DateTimeImmutable;
22
use DateTimeInterface;
23
use function property_exists;
24
25
/**
26
 * @package Limoncello\Passport
27
 */
28
abstract class DatabaseItem
29
{
30
    /**
31
     * @return string
32
     */
33
    abstract protected function getDbDateFormat(): string;
34
35
    /** Field name */
36
    const FIELD_CREATED_AT = 'created_at';
37
38
    /** Field name */
39
    const FIELD_UPDATED_AT = 'updated_at';
40
41
    /**
42
     * @var DateTimeInterface|null
43
     */
44
    private $createdAtField;
45
46
    /**
47
     * @var DateTimeInterface|null
48
     */
49
    private $updatedAtField;
50
51
    /**
52
     * @inheritdoc
53
     */
54 3
    public function getCreatedAt(): ?DateTimeInterface
55
    {
56 3
        if ($this->createdAtField === null &&
57 3
            $this->hasDynamicProperty(static::FIELD_CREATED_AT) === true &&
58 3
            ($createdAt = $this->{static::FIELD_CREATED_AT}) !== null
59
        ) {
60 3
            $this->setCreatedAtImpl($this->parseDateTime($createdAt));
61
        }
62
63 3
        return $this->createdAtField;
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69 4
    public function getUpdatedAt(): ?DateTimeInterface
70
    {
71 4
        if ($this->updatedAtField === null &&
72 4
            $this->hasDynamicProperty(static::FIELD_UPDATED_AT) === true &&
73 4
            ($updatedAt = $this->{static::FIELD_UPDATED_AT}) !== null
74
        ) {
75 4
            $this->setUpdatedAtImpl($this->parseDateTime($updatedAt));
76
        }
77
78 4
        return $this->updatedAtField;
79
    }
80
81
    /**
82
     * @param DateTimeInterface $createdAt
83
     *
84
     * @return DatabaseItem
85
     */
86 25
    protected function setCreatedAtImpl(DateTimeInterface $createdAt): DatabaseItem
87
    {
88 25
        $this->createdAtField = $createdAt;
89
90 25
        return $this;
91
    }
92
93
    /**
94
     * @param DateTimeInterface $updatedAt
95
     *
96
     * @return DatabaseItem
97
     */
98 5
    protected function setUpdatedAtImpl(DateTimeInterface $updatedAt): DatabaseItem
99
    {
100 5
        $this->updatedAtField = $updatedAt;
101
102 5
        return $this;
103
    }
104
105
    /**
106
     * @param string $createdAt
107
     *
108
     * @return DateTimeInterface
109
     *
110
     * @SuppressWarnings(PHPMD.StaticAccess)
111
     */
112 4
    protected function parseDateTime(string $createdAt): DateTimeInterface
113
    {
114 4
        return DateTimeImmutable::createFromFormat($this->getDbDateFormat(), $createdAt);
115
    }
116
117
    /**
118
     * @param string $name
119
     *
120
     * @return bool
121
     */
122 39
    protected function hasDynamicProperty(string $name): bool
123
    {
124 39
        return property_exists($this, $name);
125
    }
126
}
127