Completed
Pull Request — master (#6)
by ARCANEDEV
04:53
created

PostPresenter::setStatusAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 3
cp 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php namespace Arcanesoft\Blog\Models\Presenters;
2
3
use Illuminate\Support\HtmlString;
4
5
/**
6
 * Trait PostPresenter
7
 *
8
 * @package  Arcanesoft\Blog\Models\Presenters
9
 * @author   ARCANEDEV <[email protected]>
10
 *
11
 * @property  string                          locale_native
12
 * @property  \Illuminate\Support\HtmlString  content
13
 * @property  string                          status
14
 * @property  string                          status_name
15
 */
16
trait PostPresenter
17
{
18
    /* -----------------------------------------------------------------
19
     |  Accessors
20
     | -----------------------------------------------------------------
21
     */
22
23
    /**
24
     * Get the locale's native name.
25
     *
26
     * @return string
27
     */
28
    public function getLocaleNativeAttribute()
29
    {
30
        return localization()->getSupportedLocales()->get($this->locale)->native();
0 ignored issues
show
Bug introduced by
The property locale does not seem to exist. Did you mean locale_native?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
31
    }
32
33
    /**
34
     * Get the content attribute.
35
     *
36
     * @return \Illuminate\Support\HtmlString
37
     */
38
    public function getContentAttribute()
39
    {
40
        return new HtmlString($this->content_html);
0 ignored issues
show
Bug introduced by
The property content_html does not seem to exist. Did you mean content?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
41
    }
42
43
    /**
44
     * Get the status attribute.
45
     *
46
     * @return string
47
     */
48
    public function getStatusAttribute()
49
    {
50
        return $this->isDraft() ? self::STATUS_DRAFT : self::STATUS_PUBLISHED;
0 ignored issues
show
Bug introduced by
It seems like isDraft() 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...
51
    }
52
53
    /**
54
     * Set the status attribute.
55
     *
56
     * @param  string  $status
57
     *
58
     * @return self
59
     */
60
    public function setStatusAttribute($status)
61
    {
62
        $this->attributes['is_draft'] = ($status === self::STATUS_DRAFT);
0 ignored issues
show
Bug introduced by
The property attributes 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...
63
64
        return $this;
65
    }
66
67
    /**
68
     * Get the status name attribute.
69
     *
70
     * @return string|null
71
     */
72
    public function getStatusNameAttribute()
73
    {
74
        return self::getStatuses()->get(
75
            $this->isDraft() ? self::STATUS_DRAFT : self::STATUS_PUBLISHED
0 ignored issues
show
Bug introduced by
It seems like isDraft() 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...
76
        );
77
    }
78
79
    /* -----------------------------------------------------------------
80
     |  URL Accessors
81
     | -----------------------------------------------------------------
82
     */
83
84
    /**
85
     * Get the show URL.
86
     *
87
     * @return string
88
     */
89
    public function getShowUrl()
90
    {
91
        return route('admin::blog.posts.show', $this);
92
    }
93
94
    /**
95
     * Get the edit URL.
96
     *
97
     * @return string
98
     */
99
    public function getEditUrl()
100
    {
101
        return route('admin::blog.posts.edit', $this);
102
    }
103
}
104