Versionable   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 116
rs 10
c 0
b 0
f 0
wmc 14

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getVersionTable() 0 3 1
A newEloquentBuilder() 0 3 1
A getQualifiedVersionColumn() 0 3 1
A getQualifiedLatestVersionColumn() 0 3 1
A getVersionKeyName() 0 3 1
A getVersionedAttributeNames() 0 3 2
A getVersionColumn() 0 3 2
A getQualifiedVersionKeyName() 0 3 1
A getLatestVersionColumn() 0 3 2
A bootVersionable() 0 3 1
A newFromBuilder() 0 6 1
1
<?php
2
3
namespace ProAI\Versioning;
4
5
trait Versionable
6
{
7
    /**
8
     * Boot the versionable trait for a model.
9
     *
10
     * @return void
11
     */
12
    public static function bootVersionable()
13
    {
14
        static::addGlobalScope(new VersioningScope);
15
    }
16
17
    /**
18
     * Create a new model instance that is existing.
19
     *
20
     * @param  array  $attributes
21
     * @param  string|null  $connection
22
     * @return static
23
     */
24
    public function newFromBuilder($attributes = array(), $connection = null)
25
    {
26
        // hide ref_id from model, because ref_id == id
27
        $attributes = array_except((array) $attributes, $this->getVersionKeyName());
28
29
        return parent::newFromBuilder($attributes, $connection);
30
    }
31
32
    /**
33
     * Create a new Eloquent query builder for the model.
34
     *
35
     * @param  \Illuminate\Database\Query\Builder $query
36
     * @return \ProAI\Versioning\Builder|static
37
     */
38
    public function newEloquentBuilder($query)
39
    {
40
        return new Builder($query);
41
    }
42
43
    /**
44
     * Get the names of the attributes that are versioned.
45
     *
46
     * @return array
47
     */
48
    public function getVersionedAttributeNames()
49
    {
50
        return (! empty($this->versioned)) ? $this->versioned : [];
51
    }
52
53
    /**
54
     * Get the version key name.
55
     *
56
     * @return string
57
     */
58
    public function getVersionKeyName()
59
    {
60
        return 'ref_' . $this->getKeyName();
0 ignored issues
show
Bug introduced by
It seems like getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
        return 'ref_' . $this->/** @scrutinizer ignore-call */ getKeyName();
Loading history...
61
    }
62
63
    /**
64
     * Get the version table associated with the model.
65
     *
66
     * @return string
67
     */
68
    public function getVersionTable()
69
    {
70
        return $this->getTable() . '_version';
0 ignored issues
show
Bug introduced by
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        return $this->/** @scrutinizer ignore-call */ getTable() . '_version';
Loading history...
71
    }
72
73
    /**
74
     * Get the table qualified version key name.
75
     *
76
     * @return string
77
     */
78
    public function getQualifiedVersionKeyName()
79
    {
80
        return $this->getVersionTable().'.'.$this->getVersionKeyName();
81
    }
82
83
    /**
84
     * Get the name of the "latest version" column.
85
     *
86
     * @return string
87
     */
88
    public function getLatestVersionColumn()
89
    {
90
        return defined('static::LATEST_VERSION') ? static::LATEST_VERSION : 'latest_version';
0 ignored issues
show
Bug introduced by
The constant ProAI\Versioning\Versionable::LATEST_VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
91
    }
92
93
    /**
94
     * Get the fully qualified "latest version" column.
95
     *
96
     * @return string
97
     */
98
    public function getQualifiedLatestVersionColumn()
99
    {
100
        return $this->getTable().'.'.$this->getLatestVersionColumn();
101
    }
102
103
    /**
104
     * Get the name of the "version" column.
105
     *
106
     * @return string
107
     */
108
    public function getVersionColumn()
109
    {
110
        return defined('static::VERSION') ? static::VERSION : 'version';
0 ignored issues
show
Bug introduced by
The constant ProAI\Versioning\Versionable::VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
111
    }
112
113
    /**
114
     * Get the fully qualified "version" column.
115
     *
116
     * @return string
117
     */
118
    public function getQualifiedVersionColumn()
119
    {
120
        return $this->getVersionTable().'.'.$this->getVersionColumn();
121
    }
122
123
}
124