Passed
Push — master ( 37fbce...eb9638 )
by Mihail
09:07
created

Content::getPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 3
eloc 8
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
11
/**
12
 * Class Content. Active record object for content items with relation to category active record
13
 * @package Apps\ActiveRecord
14
 * @property int $id
15
 * @property string $title
16
 * @property string $text
17
 * @property string $path
18
 * @property int $category_id
19
 * @property int $author_id
20
 * @property string $poster
21
 * @property int $display
22
 * @property string $meta_title
23
 * @property string $meta_keywords
24
 * @property string $meta_description
25
 * @property int $views
26
 * @property int $rating
27
 * @property string $source
28
 * @property string $comment_hash
29
 * @property string $created_at
30
 * @property string $updated_at
31
 * @property string|null $deleted_at
32
 */
33
class Content extends ActiveModel
34
{
35
    use SoftDeletes, SearchableTrait;
36
37
    /** @var string $title */
38
39
    protected $searchable = [
40
        'columns' => [
41
            'title' => 3,
42
            'text' => 1
43
        ]
44
    ];
45
46
    /**
47
     * Get category relation of this content id
48
     * @return \Apps\ActiveRecord\ContentCategory|null
49
     */
50
    public function getCategory()
51
    {
52
        return ContentCategory::getById($this->category_id);
53
    }
54
55
    /**
56
     * Get item path URI - category/item
57
     * @return null|string
58
     */
59
    public function getPath()
60
    {
61
        if ($this->path === null) {
62
            return null;
63
        }
64
65
        // get category pathway
66
        $path = $this->getCategory()->path;
67
        if (!Str::likeEmpty($path)) {
68
            $path .= '/';
69
        }
70
        // add item path
71
        $path .= $this->path;
72
73
        return $path;
74
    }
75
76
    /**
77
     * Get poster URI like /upload/gallery/1/orig/9ds2jd1.png
78
     * @return null|string
79
     */
80 View Code Duplication
    public function getPosterUri()
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...
81
    {
82
        $pName = $this->poster;
83
        // check if poster is defined
84
        if ($pName === null || Str::likeEmpty($pName)) {
85
            return null;
86
        }
87
88
        // build path and check is file exists on disk
89
        $path = '/upload/gallery/' . $this->id . '/orig/' . $pName;
90
        if (!File::exist($path)) {
91
            return null;
92
        }
93
94
        return $path;
95
    }
96
97
    /**
98
     * Get poster thumbnail uri
99
     * @return null|string
100
     */
101 View Code Duplication
    public function getPosterThumbUri()
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...
102
    {
103
        $pName = $this->poster;
104
        if ($pName === null || Str::likeEmpty($pName)) {
105
            return null;
106
        }
107
108
        // remove extension, thumbs always in jpeg ;D
109
        $pName = Str::cleanExtension($pName);
110
        $path = '/upload/gallery/' . $this->id . '/thumb/' . $pName . '.jpg';
111
112
        if (!File::exist($path)) {
113
            return null;
114
        }
115
116
        return $path;
117
    }
118
}