Issues (1)

src/Traits/Incrementable.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace TestMonitor\Incrementable\Traits;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
use TestMonitor\Incrementable\Exceptions\MissingIncrementableDefinition;
8
9
trait Incrementable
10
{
11
    /**
12
     * Boot the Incrementable trait for a model.
13
     *
14
     * @return void
15
     */
16 9
    public static function bootIncrementable()
17
    {
18 9
        static::creating(function ($model) {
19 9
            $model->setIncrementableValue();
20 9
        });
21
    }
22
23
    /**
24
     * Determine and set the increment value.
25
     */
26 9
    public function setIncrementableValue()
27
    {
28 9
        $incrementableField = $this->getIncrementableField();
29
30 8
        $this->{$incrementableField} = $this->getNextIncrementableValue();
31
    }
32
33
    /**
34
     * Get the increment field, throws an exception when missing.
35
     *
36
     * @throws \TestMonitor\Incrementable\Exceptions\MissingIncrementableDefinition
37
     *
38
     * @return string
39
     */
40 9
    protected function getIncrementableField(): string
41
    {
42 9
        if (! property_exists($this, 'incrementable')) {
43 1
            throw MissingIncrementableDefinition::create(get_class($this));
44
        }
45
46 8
        return $this->incrementable;
47
    }
48
49
    /**
50
     * Gets the next available value for a new model.
51
     *
52
     * @return int
53
     */
54 8
    protected function getNextIncrementableValue(): int
55
    {
56 8
        return $this->getHighestIncrementableValue() + 1;
57
    }
58
59
    /**
60
     * Gets the highest available value currently available.
61
     *
62
     * @return int
63
     */
64 8
    protected function getHighestIncrementableValue(): int
65
    {
66 8
        $incrementableField = $this->getIncrementableField();
67
68 8
        return (int) $this->buildIncrementableQuery()
69 8
            ->where(function (Builder $query) {
70 8
                $this->buildIncrementableGroupQuery($query);
71 8
            })
72 8
            ->max($incrementableField);
73
    }
74
75
    /**
76
     * Build incrementable query group.
77
     *
78
     * @param \Illuminate\Database\Eloquent\Builder $query
79
     * @return \Illuminate\Database\Eloquent\Builder
80
     */
81 8
    public function buildIncrementableGroupQuery(Builder $query)
82
    {
83 8
        if (! property_exists($this, 'incrementableGroup')) {
84 4
            return $query;
85
        }
86
87 4
        collect($this->incrementableGroup)->each(function ($group) use ($query) {
88 4
            $query->where($group, '=', $this->$group);
89 4
        });
90
    }
91
92
    /**
93
     * Build incrementable query. Supports soft-deletes.
94
     *
95
     * @return \Illuminate\Database\Eloquent\Builder
96
     */
97 8
    public function buildIncrementableQuery()
98
    {
99 8
        if (collect(class_uses(__CLASS__))->contains(SoftDeletes::class)) {
0 ignored issues
show
class_uses(__CLASS__) of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

99
        if (collect(/** @scrutinizer ignore-type */ class_uses(__CLASS__))->contains(SoftDeletes::class)) {
Loading history...
100 1
            return static::query()->withTrashed();
101
        }
102
103 7
        return static::query();
104
    }
105
}
106