SlugObserver::getElementsLastId()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of laravel.su package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
declare(strict_types=1);
10
11
namespace App\Models\Article;
12
13
use App\Models\Article;
14
use Illuminate\Support\Str;
15
16
/**
17
 * Class SlugObserver.
18
 */
19
class SlugObserver
20
{
21
    /**
22
     * @param Article $article
23
     */
24
    public function creating(Article $article)
25
    {
26
        $article->slug = Str::slug($article->title) . '-' . ($this->getElementsLastId() + 1);
27
    }
28
29
    /**
30
     * @return int
31
     */
32
    private function getElementsLastId(): int
33
    {
34
        $lastArticle = Article::orderBy('id', 'desc')->first();
35
36
        return $lastArticle ? $lastArticle->id : 0;
37
    }
38
}
39