Completed
Push — master ( 9c542a...5db7ed )
by ARCANEDEV
13s
created

PostPresenter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 58.33%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 1
cbo 4
dl 0
loc 143
ccs 14
cts 24
cp 0.5833
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getLocaleNativeAttribute() 0 14 2
A getContentAttribute() 0 4 1
A getStatuses() 0 4 1
isDraft() 0 1 ?
getAttributeFromArray() 0 1 ?
setAttribute() 0 1 ?
A getStatusAttribute() 0 4 2
A setStatusAttribute() 0 6 1
A getStatusNameAttribute() 0 6 1
A getShowUrl() 0 4 1
A getEditUrl() 0 4 1
1
<?php namespace Arcanesoft\Blog\Models\Presenters;
2
3
use Arcanesoft\Blog\Entities\PostStatus;
4
use Illuminate\Support\HtmlString;
5
6
/**
7
 * Trait PostPresenter
8
 *
9
 * @package  Arcanesoft\Blog\Models\Presenters
10
 * @author   ARCANEDEV <[email protected]>
11
 *
12
 * @property  string                          locale_native
13
 * @property  \Illuminate\Support\HtmlString  content
14
 * @property  string                          status
15
 * @property  string                          status_name
16
 */
17
trait PostPresenter
18
{
19
    /* -----------------------------------------------------------------
20
     |  Accessors
21
     | -----------------------------------------------------------------
22
     */
23
24
    /**
25
     * Get the locale's native name.
26
     *
27
     * @return string
28
     */
29
    public function getLocaleNativeAttribute()
30
    {
31
        $locale = $this->getAttributeFromArray('locale');
32
33
        try {
34
            return localization()
35
                ->getSupportedLocales()
36
                ->get($locale)
37
                ->native();
38
        }
39
        catch (\Exception $e) {
40
            return strtoupper($locale);
41
        }
42
    }
43
44
    /**
45
     * Get the content attribute.
46
     *
47
     * @return \Illuminate\Support\HtmlString
48
     */
49
    public function getContentAttribute()
50
    {
51
        return new HtmlString($this->getAttributeFromArray('content_html'));
52
    }
53
54
    /**
55
     * Get the status attribute.
56
     *
57
     * @return string
58
     */
59 2
    public function getStatusAttribute()
60
    {
61 2
        return $this->isDraft() ? PostStatus::STATUS_DRAFT : PostStatus::STATUS_PUBLISHED;
62
    }
63
64
    /**
65
     * Set the status attribute.
66
     *
67
     * @param  string  $status
68
     *
69
     * @return self
70
     */
71 2
    public function setStatusAttribute($status)
72
    {
73 2
        $this->setAttribute('is_draft', $status === PostStatus::STATUS_DRAFT);
74
75 2
        return $this;
76
    }
77
78
    /**
79
     * Get the status name attribute.
80
     *
81
     * @return string|null
82
     */
83 2
    public function getStatusNameAttribute()
84
    {
85 2
        return PostStatus::get(
86 2
            $this->getStatusAttribute()
87
        );
88
    }
89
90
    /**
91
     * Get the post statuses.
92
     *
93
     * @return \Illuminate\Support\Collection
94
     */
95 4
    public static function getStatuses()
96
    {
97 4
        return PostStatus::all();
98
    }
99
100
    /* -----------------------------------------------------------------
101
     |  Other Methods
102
     | -----------------------------------------------------------------
103
     */
104
105
    /**
106
     * Check if the post's status is "draft".
107
     *
108
     * @return bool
109
     */
110
    abstract public function isDraft();
111
112
    /* -----------------------------------------------------------------
113
     |  URL Accessors
114
     | -----------------------------------------------------------------
115
     */
116
117
    /**
118
     * Get the show URL.
119
     *
120
     * @return string
121
     */
122 2
    public function getShowUrl()
123
    {
124 2
        return route('admin::blog.posts.show', [$this]);
125
    }
126
127
    /**
128
     * Get the edit URL.
129
     *
130
     * @return string
131
     */
132 2
    public function getEditUrl()
133
    {
134 2
        return route('admin::blog.posts.edit', [$this]);
135
    }
136
137
    /* -----------------------------------------------------------------
138
     |  Eloquent Methods
139
     | -----------------------------------------------------------------
140
     */
141
142
    /**
143
     * Get an attribute from the $attributes array.
144
     *
145
     * @param  string  $key
146
     * @return mixed
147
     */
148
    abstract protected function getAttributeFromArray($key);
149
150
    /**
151
     * Set a given attribute on the model.
152
     *
153
     * @param  string  $key
154
     * @param  mixed   $value
155
     *
156
     * @return self
157
     */
158
    abstract public function setAttribute($key, $value);
159
}
160