PublishableTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 73
rs 10
wmc 6
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A publish() 0 7 1
A togglePublishedState() 0 7 1
A unpublish() 0 7 1
A scopePublished() 0 4 1
A scopeUnpublished() 0 4 1
A isPublished() 0 4 1
1
<?php
2
3
namespace Yajra\CMS\Entities\Traits;
4
5
use Illuminate\Database\Eloquent\Builder;
6
7
trait PublishableTrait
8
{
9
    /**
10
     * Published an entity.
11
     *
12
     * @return \Illuminate\Database\Eloquent\Model
13
     */
14
    public function publish()
15
    {
16
        $this->published = true;
0 ignored issues
show
Bug introduced by
The property published does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
17
        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
18
19
        return $this;
20
    }
21
22
    /**
23
     * Published/Unpublished an entity.
24
     *
25
     * @return \Illuminate\Database\Eloquent\Model
26
     */
27
    public function togglePublishedState()
28
    {
29
        $this->published = ! $this->published;
30
        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
31
32
        return $this;
33
    }
34
35
    /**
36
     * Unpublished an entity.
37
     *
38
     * @return \Illuminate\Database\Eloquent\Model
39
     */
40
    public function unpublish()
41
    {
42
        $this->published = false;
43
        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
44
45
        return $this;
46
    }
47
48
    /**
49
     * Query scope for published articles.
50
     *
51
     * @param \Illuminate\Database\Eloquent\Builder $query
52
     * @return \Illuminate\Database\Eloquent\Builder
53
     */
54
    public function scopePublished($query) : Builder
55
    {
56
        return $query->where('published', true);
57
    }
58
59
    /**
60
     * Query scope for unpublished articles.
61
     *
62
     * @param \Illuminate\Database\Eloquent\Builder $query
63
     * @return \Illuminate\Database\Eloquent\Builder
64
     */
65
    public function scopeUnpublished($query) : Builder
66
    {
67
        return $query->where('published', false);
68
    }
69
70
    /**
71
     * Check if entity was published.
72
     *
73
     * @return bool
74
     */
75
    public function isPublished()
76
    {
77
        return $this->published;
78
    }
79
}