Content::getPosterThumbUri()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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