Passed
Pull Request — master (#209)
by
unknown
10:30
created

AbstractEntity::getDeletedAt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * Abstract Entity.
4
 *
5
 * @package App\Entities
6
 *
7
 * @author    Nick Menke <[email protected]>
8
 * @copyright 2018-2020 Nick Menke
9
 *
10
 * @link https://github.com/nlmenke/vertebrae
11
 */
12
13
declare(strict_types=1);
14
15
namespace App\Entities;
16
17
use Carbon\Carbon;
18
use Illuminate\Database\Eloquent\Factories\HasFactory;
19
use Illuminate\Database\Eloquent\Model;
20
use Illuminate\Database\Eloquent\SoftDeletes;
21
22
/**
23
 * The base entity class.
24
 *
25
 * This class contains any functions that would otherwise be duplicated in
26
 * other models. All other entities should extend this class.
27
 *
28
 * @since x.x.x introduced
29
 */
30
abstract class AbstractEntity extends Model
31
{
32
    use HasFactory;
33
34
    /**
35
     * Retrieves the `created_at` attribute.
36
     *
37
     * If the table has timestamps, we can pull the creation time of a given
38
     * model. Otherwise, return a null value.
39
     *
40
     * @return Carbon|null
41
     */
42
    public function getCreatedAt(): ?Carbon
43
    {
44
        return $this->usesTimestamps() ? $this->getAttribute($this->getCreatedAtColumn()) : null;
45
    }
46
47
    /**
48
     * Retrieves the `deleted_at` attribute.
49
     *
50
     * If the table employs soft deletes, we can pull the deletion time of a
51
     * given mode. Otherwise, return a null value.
52
     *
53
     * @return Carbon|null
54
     */
55
    public function getDeletedAt(): ?Carbon
56
    {
57
        return $this->usesSoftDeletes() ? $this->getAttribute(app(SoftDeletes::class)->getDeletedAtColumn()) : null;
58
    }
59
60
    /**
61
     * Retrieves the `id` attribute.
62
     *
63
     * All models should have an `id` identifier. We can pull that value using
64
     * this method.
65
     *
66
     * @return int
67
     */
68 24
    public function getId(): int
69
    {
70 24
        return $this->getAttribute('id');
71
    }
72
73
    /**
74
     * Retrieves the `updated_at` attribute.
75
     *
76
     * If the table has timestamps, we can pull the update time of a given
77
     * model. Otherwise, return a null value.
78
     *
79
     * @return Carbon|null
80
     */
81
    public function getUpdatedAt(): ?Carbon
82
    {
83
        return $this->usesTimestamps() ? $this->getAttribute($this->getUpdatedAtColumn()) : null;
84
    }
85
86
    /**
87
     * Determines if the model uses soft deletes.
88
     *
89
     * Rather than permanently deleting records, we can "soft delete" models
90
     * by adding a `deleted_at` field. This method will determine if the model
91
     * utilizes the SoftDeletes trait.
92
     *
93
     * @return bool
94
     */
95
    protected function usesSoftDeletes(): bool
96
    {
97
        return in_array('SoftDeletingTrait', class_uses(self::class), true);
98
    }
99
}
100