Test Setup Failed
Pull Request — master (#150)
by Nick
14:47 queued 06:28
created

AbstractEntity   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 20%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
eloc 6
c 1
b 0
f 1
dl 0
loc 66
ccs 2
cts 10
cp 0.2
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A usesSoftDeletes() 0 3 1
A getUpdatedAt() 0 3 2
A getDeletedAt() 0 3 2
A getId() 0 3 1
A getCreatedAt() 0 3 2
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\Model;
19
use Illuminate\Database\Eloquent\SoftDeletes;
20
21
/**
22
 * The base entity class.
23
 *
24
 * This class contains any functions that would otherwise be duplicated in
25
 * other models. All other entities should extend this class.
26
 *
27
 * @since x.x.x introduced
28
 */
29
abstract class AbstractEntity extends Model
30
{
31
    /**
32
     * Retrieves the `created_at` attribute.
33
     *
34
     * If the table has timestamps, we can pull the creation time of a given
35
     * model. Otherwise, return a null value.
36
     *
37
     * @return Carbon|null
38
     */
39
    public function getCreatedAt(): ?Carbon
40
    {
41
        return $this->usesTimestamps() ? $this->getAttribute($this->getCreatedAtColumn()) : null;
42
    }
43
44
    /**
45
     * Retrieves the `deleted_at` attribute.
46
     *
47
     * If the table employs soft deletes, we can pull the deletion time of a
48
     * given mode. Otherwise, return a null value.
49
     *
50
     * @return Carbon|null
51
     */
52
    public function getDeletedAt(): ?Carbon
53
    {
54
        return $this->usesSoftDeletes() ? $this->getAttribute(app(SoftDeletes::class)->getDeletedAtColumn()) : null;
55
    }
56
57
    /**
58
     * Retrieves the `id` attribute.
59
     *
60
     * All models should have an `id` identifier. We can pull that value using
61
     * this method.
62
     *
63
     * @return int
64
     */
65 24
    public function getId(): int
66
    {
67 24
        return $this->getAttribute('id');
68
    }
69
70
    /**
71
     * Retrieves the `updated_at` attribute.
72
     *
73
     * If the table has timestamps, we can pull the update time of a given
74
     * model. Otherwise, return a null value.
75
     *
76
     * @return Carbon|null
77
     */
78
    public function getUpdatedAt(): ?Carbon
79
    {
80
        return $this->usesTimestamps() ? $this->getAttribute($this->getUpdatedAtColumn()) : null;
81
    }
82
83
    /**
84
     * Determines if the model uses soft deletes.
85
     *
86
     * Rather than permanently deleting records, we can "soft delete" models
87
     * by adding a `deleted_at` field. This method will determine if the model
88
     * utilizes the SoftDeletes trait.
89
     *
90
     * @return bool
91
     */
92
    protected function usesSoftDeletes(): bool
93
    {
94
        return in_array('SoftDeletingTrait', class_uses(self::class), true);
95
    }
96
}
97