Completed
Push — master ( c7e879...390e33 )
by Fèvre
02:26
created

DiscussCategory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 4
dl 0
loc 76
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 9 1
A slugStrategy() 0 4 1
A conversations() 0 4 1
A lastConversation() 0 4 1
A pluckLocked() 0 8 3
1
<?php
2
namespace Xetaravel\Models;
3
4
use Eloquence\Behaviours\Sluggable;
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Facades\Auth;
7
use Xetaravel\Models\Presenters\DiscussCategoryPresenter;
8
9
class DiscussCategory extends Model
10
{
11
    use Sluggable,
12
        DiscussCategoryPresenter;
13
14
    /**
15
     * The accessors to append to the model's array form.
16
     *
17
     * @var array
18
     */
19
    protected $appends = [
20
        'category_url'
21
    ];
22
23
    /**
24
     * The "booting" method of the model.
25
     *
26
     * @return void
27
     */
28
    protected static function boot()
29
    {
30
        parent::boot();
31
32
        // Generated the slug before updating.
33
        static::updating(function ($model) {
34
            $model->generateSlug();
35
        });
36
    }
37
38
    /**
39
     * Return the field to slug.
40
     *
41
     * @return string
42
     */
43
    public function slugStrategy(): string
44
    {
45
        return 'title';
46
    }
47
48
    /**
49
     * Get the conversations for the category.
50
     *
51
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
52
     */
53
    public function conversations()
54
    {
55
        return $this->hasMany(DiscussConversation::class);
56
    }
57
58
    /**
59
     * Get the last conversation of the category.
60
     *
61
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
62
     */
63
    public function lastConversation()
64
    {
65
        return $this->hasOne(DiscussConversation::class, 'id', 'last_conversation_id');
66
    }
67
68
    /**
69
     * Pluck the categories by the given fields and the locked state.
70
     *
71
     * @param string $value
72
     * @param string|null $column
73
     *
74
     * @return \Illuminate\Support\Collection
75
     */
76
    public static function pluckLocked($value, $column = null): Collection
77
    {
78
        if (Auth::user() && Auth::user()->hasPermission('manage.discuss.conversations')) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Authenticatable as the method hasPermission() does only exist in the following implementations of said interface: Xetaravel\Models\User.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
79
            return self::pluck($value, $column);
80
        }
81
82
        return self::where('is_locked', false)->pluck($value, $column);
83
    }
84
}
85