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

Article::boot()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 12
nc 3
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
        // The Route::getFacadeRoot() is undefined in the testing environment for mysterious reasons.
28
        if (App::environment() !== 'testing') {
29
            // Don't apply the scope to the admin part.
30
            $result = strpos(Route::getFacadeRoot()->current()->getPrefix(), 'admin');
31
32
            if ($result === false) {
33
                static::addGlobalScope(new DisplayScope);
34
            }
35
        } else {
36
            static::addGlobalScope(new DisplayScope);
37
        }
38
39
        // Generated the slug before updating.
40
        static::updating(function ($model) {
41
            $model->generateSlug();
42
        });
43
44
        // Set the user id to the new article before saving it.
45
        static::creating(function ($model) {
46
            $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...
47
        });
48
    }
49
50
    /**
51
     * Return the field to slug.
52
     *
53
     * @return string
54
     */
55
    public function slugStrategy(): string
56
    {
57
        return 'title';
58
    }
59
60
    /**
61
     * Return the count cache configuration.
62
     *
63
     * @return array
64
     */
65
    public function countCaches(): array
66
    {
67
        return [
68
            User::class,
69
            Category::class
70
        ];
71
    }
72
73
    /**
74
     * Get the category that owns the article.
75
     *
76
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
77
     */
78
    public function category()
79
    {
80
        return $this->belongsTo(Category::class);
81
    }
82
83
    /**
84
     * Get the user that owns the article.
85
     *
86
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
87
     */
88
    public function user()
89
    {
90
        return $this->belongsTo(User::class);
91
    }
92
93
    /**
94
     * Get the comments for the article.
95
     *
96
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
97
     */
98
    public function comments()
99
    {
100
        return $this->hasMany(Comment::class);
101
    }
102
}
103