PrefixedModel::getPrefix()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
nc 2
dl 0
loc 4
ccs 2
cts 2
cp 1
c 0
b 0
f 0
cc 2
nop 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\Support\Database;
6
7
use Illuminate\Database\Eloquent\Model;
8
9
/**
10
 * Class     PrefixedModel
11
 *
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
abstract class PrefixedModel extends Model
15
{
16
    /* -----------------------------------------------------------------
17
     |  Properties
18
     | -----------------------------------------------------------------
19
     */
20
21
    /**
22
     * The table prefix.
23
     *
24
     * @var string|null
25
     */
26
    protected $prefix;
27
28
    /* -----------------------------------------------------------------
29
     |  Getters & Setters
30
     | -----------------------------------------------------------------
31
     */
32
33
    /**
34
     * Get the table associated with the model.
35
     *
36
     * @return string
37
     */
38 12
    public function getTable()
39
    {
40 12
        return $this->getPrefix().parent::getTable();
41
    }
42
43
    /**
44
     * Get the prefix table associated with the model.
45
     *
46
     * @return string
47
     */
48 12
    public function getPrefix(): string
49
    {
50 12
        return $this->isPrefixed() ? $this->prefix : '';
51
    }
52
53
    /**
54
     * Set the prefix table associated with the model.
55
     *
56
     * @param  string|null  $prefix
57
     *
58
     * @return $this
59
     */
60 6
    public function setPrefix(?string $prefix)
61
    {
62 6
        $this->prefix = $prefix;
63
64 6
        return $this;
65
    }
66
67
    /* -----------------------------------------------------------------
68
     |  Check Methods
69
     | -----------------------------------------------------------------
70
     */
71
72
    /**
73
     * Check if table is prefixed.
74
     *
75
     * @return bool
76
     */
77 12
    public function isPrefixed(): bool
78
    {
79 12
        return ! is_null($this->prefix);
80
    }
81
}
82