Incrementable   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 98
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A bootIncrementable() 0 6 1
A setIncrementableValue() 0 6 1
A getIncrementableField() 0 8 2
A getNextIncrementableValue() 0 4 1
A getHighestIncrementableValue() 0 10 1
A buildIncrementableGroupQuery() 0 10 2
A buildIncrementableQuery() 0 8 2
1
<?php
2
3
namespace ByTestGear\Incrementable\Traits;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
use ByTestGear\Incrementable\Exceptions\MissingIncrementableDefinition;
8
9
trait Incrementable
10
{
11
    /**
12
     * Boot the Incrementable trait for a model.
13
     *
14
     * @return void
15
     */
16
    public static function bootIncrementable()
17
    {
18
        static::creating(function ($model) {
19
            $model->setIncrementableValue();
20
        });
21
    }
22
23
    /**
24
     * Determine and set the increment value.
25
     */
26
    public function setIncrementableValue()
27
    {
28
        $incrementableField = $this->getIncrementableField();
29
30
        $this->{$incrementableField} = $this->getNextIncrementableValue();
31
    }
32
33
    /**
34
     * Get the increment field, throws an exception when missing.
35
     *
36
     * @return string
37
     *
38
     * @throws \ByTestGear\Incrementable\Exceptions\MissingIncrementableDefinition
39
     */
40
    protected function getIncrementableField(): string
41
    {
42
        if (! property_exists($this, 'incrementable')) {
43
            throw MissingIncrementableDefinition::create(get_class($this));
44
        }
45
46
        return $this->incrementable;
0 ignored issues
show
Bug introduced by
The property incrementable does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
47
    }
48
49
    /**
50
     * Gets the next available value for a new model.
51
     *
52
     * @return int
53
     */
54
    protected function getNextIncrementableValue(): int
55
    {
56
        return $this->getHighestIncrementableValue() + 1;
57
    }
58
59
    /**
60
     * Gets the highest available value currently available.
61
     *
62
     * @return int
63
     */
64
    protected function getHighestIncrementableValue(): int
65
    {
66
        $incrementableField = $this->getIncrementableField();
67
68
        return (int) $this->buildIncrementableQuery()
69
            ->where(function (Builder $query) {
70
                $this->buildIncrementableGroupQuery($query);
71
            })
72
            ->max($incrementableField);
73
    }
74
75
    /**
76
     * Build incrementable query group.
77
     *
78
     * @param \Illuminate\Database\Eloquent\Builder $query
79
     *
80
     * @return \Illuminate\Database\Eloquent\Builder
81
     */
82
    public function buildIncrementableGroupQuery(Builder $query)
83
    {
84
        if (! property_exists($this, 'incrementableGroup')) {
85
            return $query;
86
        }
87
88
        collect($this->incrementableGroup)->each(function ($group) use ($query) {
0 ignored issues
show
Bug introduced by
The property incrementableGroup does not seem to exist. Did you mean incrementable?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
89
            $query->where($group, '=', $this->$group);
90
        });
91
    }
92
93
    /**
94
     * Build incrementable query. Supports soft-deletes.
95
     *
96
     * @return \Illuminate\Database\Eloquent\Builder
97
     */
98
    public function buildIncrementableQuery()
99
    {
100
        if (collect(class_uses(__CLASS__))->contains(SoftDeletes::class)) {
101
            return static::query()->withTrashed();
102
        }
103
104
        return static::query();
105
    }
106
}
107