Completed
Push — manage-articles ( 1c60ef...9e1578 )
by Fèvre
02:30
created

Article::boot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 0
1
<?php
2
namespace Xetaravel\Models;
3
4
use Xetaravel\Models\Category;
5
use Xetaravel\Models\User;
6
use Xetaravel\Models\Scopes\DisplayScope;
7
use Eloquence\Behaviours\CountCache\Countable;
8
use Eloquence\Behaviours\Sluggable;
9
use Illuminate\Support\Facades\App;
10
use Illuminate\Support\Facades\Auth;
11
use Illuminate\Support\Facades\Route;
12
13
class Article extends Model
14
{
15
    use Countable,
16
        Sluggable;
17
18
    /**
19
     * The "booting" method of the model.
20
     *
21
     * @return void
22
     */
23
    protected static function boot()
24
    {
25
        parent::boot();
26
27
        // Don't apply the scope to the admin part.
28
        $result = strpos(Route::getFacadeRoot()->current()->getPrefix(), 'admin');
29
30
        if ($result === false) {
31
            static::addGlobalScope(new DisplayScope);
32
        }
33
34
        // Generated the slug before updating.
35
        static::updating(function ($model) {
36
            $model->generateSlug();
37
        });
38
39
        // Set the user id to the new article before saving it.
40
        static::creating(function ($model) {
41
            $model->user_id = Auth::user()->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
42
        });
43
    }
44
45
    /**
46
     * Return the field to slug.
47
     *
48
     * @return string
49
     */
50
    public function slugStrategy(): string
51
    {
52
        return 'title';
53
    }
54
55
    /**
56
     * Return the count cache configuration.
57
     *
58
     * @return array
59
     */
60
    public function countCaches(): array
61
    {
62
        return [
63
            User::class,
64
            Category::class
65
        ];
66
    }
67
68
    /**
69
     * Get the category that owns the article.
70
     *
71
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
72
     */
73
    public function category()
74
    {
75
        return $this->belongsTo(Category::class);
76
    }
77
78
    /**
79
     * Get the user that owns the article.
80
     *
81
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
82
     */
83
    public function user()
84
    {
85
        return $this->belongsTo(User::class);
86
    }
87
88
    /**
89
     * Get the comments for the article.
90
     *
91
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
92
     */
93
    public function comments()
94
    {
95
        return $this->hasMany(Comment::class);
96
    }
97
}
98