Publishable::initializePublishable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Bmatovu\Publishable;
4
5
/**
6
 * @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder withDrafts()
7
 * @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder onlyDrafts()
8
 * @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder onlyPublished()
9
 */
10
trait Publishable
11
{
12
    /**
13
     * Boot the has-drafts trait for a model.
14
     *
15
     * @return void
16
     */
17 3
    public static function bootPublishable()
18
    {
19 3
        static::addGlobalScope(new PublishedScope);
20 3
    }
21
22
    /**
23
     * Initialize this trait for an instance.
24
     *
25
     * @return void
26
     */
27 3
    public function initializePublishable()
28
    {
29 3
        $this->dates[] = $this->getPublishedAtColumn();
0 ignored issues
show
Bug introduced by
The property dates 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...
30 3
    }
31
32
    /**
33
     * Save instance of this model as published.
34
     *
35
     * @param array $options
36
     *
37
     * @return bool
38
     */
39 1 View Code Duplication
    public function publish(array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41
        // https://laravel-news.com/laravel-model-events-getting-started
42
        // https://gist.github.com/scrubmx/7fc20663ce2b3ac103a2879915b572be
43
        // https://www.google.com/search?q=laravel+register+fire+model+event&oq=laravel+register+fire+model+event
44
45
        // If the "publishing" event returns false we bail out immediately and return false,
46
        // indicating that the save failed. This provides a chance for any
47
        // listeners to cancel save operations if validations fail or whatever.
48 1
        if ($this->fireModelEvent('publishing') === false) {
0 ignored issues
show
Bug introduced by
It seems like fireModelEvent() 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...
49
            return false;
50
        }
51
52 1
        $this->{$this->getPublishedAtColumn()} = $this->freshTimestamp();
0 ignored issues
show
Bug introduced by
It seems like freshTimestamp() 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...
53
54 1
        $saved = parent::save($options);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (save() instead of publish()). Are you sure this is correct? If so, you might want to change this to $this->save().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
55
56 1
        if ($saved) {
57 1
            $this->fireModelEvent('published', false);
0 ignored issues
show
Bug introduced by
It seems like fireModelEvent() 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...
58
        }
59
60 1
        return $saved;
61
    }
62
63
    /**
64
     * Toogle model instance state to non-published.
65
     *
66
     * @param array $options
67
     *
68
     * @return bool
69
     */
70 1 View Code Duplication
    public function unpublish(array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72 1
        if ($this->fireModelEvent('unpublishing') === false) {
0 ignored issues
show
Bug introduced by
It seems like fireModelEvent() 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...
73
            return false;
74
        }
75
76 1
        $this->{$this->getPublishedAtColumn()} = null;
77
78 1
        $saved = parent::save($options);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (save() instead of unpublish()). Are you sure this is correct? If so, you might want to change this to $this->save().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
79
80 1
        if ($saved) {
81 1
            $this->fireModelEvent('unpublished', false);
0 ignored issues
show
Bug introduced by
It seems like fireModelEvent() 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...
82
        }
83
84 1
        return $saved;
85
    }
86
87
    /**
88
     * Save instance of this model as a draft.
89
     *
90
     * @param array $options
91
     *
92
     * @return bool
93
     */
94
    public function draft(array $options = [])
95
    {
96
        return $this->unpublish($options);
97
    }
98
99
    /**
100
     * Register a "publishing" model event callback with the dispatcher.
101
     *
102
     * @param \Closure|string $callback
103
     *
104
     * @return void
105
     */
106
    public static function publishing($callback)
107
    {
108
        static::registerModelEvent('publishing', $callback);
109
    }
110
111
    /**
112
     * Register a "published" model event callback with the dispatcher.
113
     *
114
     * @param \Closure|string $callback
115
     *
116
     * @return void
117
     */
118
    public static function published($callback)
119
    {
120
        static::registerModelEvent('published', $callback);
121
    }
122
123
    /**
124
     * Register a "unpublishing" model event callback with the dispatcher.
125
     *
126
     * @param \Closure|string $callback
127
     *
128
     * @return void
129
     */
130
    public static function unpublishing($callback)
131
    {
132
        static::registerModelEvent('unpublishing', $callback);
133
    }
134
135
    /**
136
     * Register a "unpublished" model event callback with the dispatcher.
137
     *
138
     * @param \Closure|string $callback
139
     *
140
     * @return void
141
     */
142
    public static function unpublished($callback)
143
    {
144
        static::registerModelEvent('unpublished', $callback);
145
    }
146
147
    /**
148
     * Determine if the model instance is published.
149
     *
150
     * @return bool
151
     */
152 1
    public function isPublished()
153
    {
154 1
        return ! is_null($this->{$this->getPublishedAtColumn()});
155
    }
156
157
    /**
158
     * Get the name of the "published at" column.
159
     *
160
     * @return string
161
     */
162 3
    public function getPublishedAtColumn()
163
    {
164 3
        return defined('static::PUBLISHED_AT') ? static::PUBLISHED_AT : 'published_at';
165
    }
166
167
    /**
168
     * Get the fully qualified "published at" column.
169
     *
170
     * @return string
171
     */
172 1
    public function getQualifiedPublishedAtColumn()
173
    {
174 1
        return $this->qualifyColumn($this->getPublishedAtColumn());
0 ignored issues
show
Bug introduced by
It seems like qualifyColumn() 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...
175
    }
176
}
177