Passed
Push — master ( 345dac...c57047 )
by Mihail
09:15
created

Content   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 137
rs 10
c 0
b 0
f 0
wmc 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A ratings() 0 3 1
A getPath() 0 15 3
A getPosterUri() 0 15 4
A commentPosts() 0 4 1
A user() 0 3 1
A category() 0 3 1
A tags() 0 3 1
A getPosterThumbUri() 0 16 4
1
<?php
2
3
namespace Apps\ActiveRecord;
4
5
use Ffcms\Core\Arch\ActiveModel;
6
use Ffcms\Core\Helper\FileSystem\File;
7
use Ffcms\Core\Helper\Type\Str;
8
use Ffcms\Core\Traits\SearchableTrait;
9
use Illuminate\Database\Eloquent\SoftDeletes;
10
use Illuminate\Support\Collection;
11
12
/**
13
 * Class Content. Active record object for content items with relation to category active record
14
 * @package Apps\ActiveRecord
15
 * @property int $id
16
 * @property array $title
17
 * @property array $text
18
 * @property string $path
19
 * @property int $category_id
20
 * @property int $author_id
21
 * @property string $poster
22
 * @property bool $display
23
 * @property bool $important
24
 * @property array $meta_title
25
 * @property array $meta_keywords
26
 * @property array $meta_description
27
 * @property int $views
28
 * @property int $rating
29
 * @property string $source
30
 * @property string $comment_hash
31
 * @property string $created_at
32
 * @property string $updated_at
33
 * @property string|null $deleted_at
34
 * @property ContentCategory $category
35
 * @property ContentRating[] $ratings
36
 * @property ContentTag[] $tags
37
 * @property User $user
38
 * @property CommentPost[]|Collection $commentPosts
39
 */
40
class Content extends ActiveModel
41
{
42
    use SoftDeletes, SearchableTrait;
43
44
    protected $searchable = [
45
        'columns' => [
46
            'title' => 10,
47
            'text' => 2
48
        ]
49
    ];
50
51
    protected $casts = [
52
        'id' => 'integer',
53
        'title' => 'serialize',
54
        'text' => 'serialize',
55
        'path' => 'string',
56
        'category_id' => 'integer',
57
        'author_id' => 'integer',
58
        'poster' => 'string',
59
        'display' => 'boolean',
60
        'meta_title' => 'serialize',
61
        'meta_keywords' => 'serialize',
62
        'meta_description' => 'serialize',
63
        'views' => 'integer',
64
        'rating' => 'integer',
65
        'source' => 'string',
66
        'important' => 'boolean'
67
    ];
68
69
    /**
70
     * Get content category object relation
71
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
72
     */
73
    public function category()
74
    {
75
        return $this->belongsTo('Apps\ActiveRecord\ContentCategory', 'category_id');
76
    }
77
78
    /**
79
     * Get content rating objects relation
80
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
81
     */
82
    public function ratings()
83
    {
84
        return $this->hasMany('Apps\ActiveRecord\ContentRating', 'content_id');
85
    }
86
87
    /**
88
     * Get content tag objects relation
89
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
90
     */
91
    public function tags()
92
    {
93
        return $this->hasMany('Apps\ActiveRecord\ContentTag', 'content_id');
94
    }
95
96
    /**
97
     * Get user object relation
98
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
99
     */
100
    public function user()
101
    {
102
        return $this->belongsTo('Apps\ActiveRecord\User', 'author_id');
103
    }
104
105
    /**
106
     * Get comments objects relation
107
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
108
     */
109
    public function commentPosts()
110
    {
111
        return $this->hasMany(CommentPost::class, 'app_relation_id')
112
            ->where('app_name', 'content');
113
    }
114
115
    /**
116
     * Get item path URI - category/item
117
     * @return null|string
118
     */
119
    public function getPath(): ?string
120
    {
121
        if (!$this->path) {
122
            return null;
123
        }
124
125
        // get category pathway
126
        $path = $this->category->path;
127
        if (!Str::likeEmpty($path)) {
128
            $path .= '/';
129
        }
130
        // add item path
131
        $path .= $this->path;
132
133
        return $path;
134
    }
135
136
    /**
137
     * Get poster URI like /upload/gallery/1/orig/9ds2jd1.png
138
     * @return null|string
139
     */
140
    public function getPosterUri(): ?string
141
    {
142
        $pName = $this->poster;
143
        // check if poster is defined
144
        if (!$pName || Str::likeEmpty($pName)) {
145
            return null;
146
        }
147
148
        // build path and check is file exists on disk
149
        $path = '/upload/gallery/' . $this->id . '/orig/' . $pName;
150
        if (!File::exist($path)) {
151
            return null;
152
        }
153
154
        return $path;
155
    }
156
157
    /**
158
     * Get poster thumbnail uri
159
     * @return null|string
160
     */
161
    public function getPosterThumbUri(): ?string
162
    {
163
        $pName = $this->poster;
164
        if (!$pName || Str::likeEmpty($pName)) {
165
            return null;
166
        }
167
168
        // remove extension, thumbs always in jpeg ;D
169
        $pName = Str::cleanExtension($pName);
170
        $path = '/upload/gallery/' . $this->id . '/thumb/' . $pName . '.jpg';
171
172
        if (!File::exist($path)) {
173
            return null;
174
        }
175
176
        return $path;
177
    }
178
}
179