GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#22)
by Roelof Jan
03:00
created

Article   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 151
ccs 39
cts 42
cp 0.9286
rs 10
c 0
b 0
f 0
wmc 15

10 Methods

Rating   Name   Duplication   Size   Complexity  
A image() 0 11 2
A thumbnail() 0 11 3
A getImagesUrlPath() 0 3 1
A description() 0 3 1
A canonicalLink() 0 3 1
A externalUrl() 0 3 1
A getThumbnailFromPath() 0 7 1
A slug() 0 3 1
A getDescriptionFromContent() 0 13 2
A title() 0 11 2
1
<?php
2
3
4
namespace AloiaCms\Models;
5
6
use AloiaCms\HtmlParser;
7
use AloiaCms\Models\Contracts\ModelInterface;
8
use AloiaCms\Models\Contracts\PublishInterface;
9
use AloiaCms\Models\Traits\Postable;
10
use AloiaCms\Models\Traits\Publishable;
11
use AloiaCms\Models\Traits\Updatable;
12
use Illuminate\Support\Facades\Config;
13
14
class Article extends Model implements ModelInterface, PublishInterface
15
{
16 1
    use Publishable, Postable, Updatable;
17
18
    protected $folder = 'articles';
19
20
    protected $required_fields = [
21
        'post_date'
22
    ];
23
24
    /**
25
     * Get the title of this article
26
     *
27
     * @return string
28
     * @throws \Exception
29
     */
30 2
    public function title(): string
31
    {
32 2
        if (isset($this->matter['title'])) {
33 2
            return $this->matter['title'];
34
        }
35
36
        $file_content = $this->body();
37
38
        $titles = HtmlParser::getTextBetweenTags($file_content, 'h1');
39
40
        return $titles[0] ?? "Untitled article";
41
    }
42
43
    /**
44
     * Get the slug of this article
45
     *
46
     * @return string
47
     */
48 1
    public function slug(): string
49
    {
50 1
        return $this->file_name;
51
    }
52
53
    /**
54
     * Get the main image of this article
55
     *
56
     * @return string
57
     * @throws \Exception
58
     */
59 4
    public function image(): string
60
    {
61 4
        if (isset($this->matter['image'])) {
62 1
            return $this->matter['image'];
63
        }
64
65 3
        $file_content = $this->body();
66
67 3
        $images = HtmlParser::getTagAttribute($file_content, 'img', 'src');
68
69 3
        return $images[0] ?? "";
70
    }
71
72
    /**
73
     * Get the path to the thumbnail of this article
74
     *
75
     * @return string
76
     * @throws \Exception
77
     */
78 3
    public function thumbnail(): string
79
    {
80 3
        if (isset($this->matter['thumbnail'])) {
81 1
            return $this->matter['thumbnail'];
82
        }
83
84 2
        if (!empty($this->image())) {
85 1
            return "/{$this->getImagesUrlPath()}/{$this->getThumbnailFromPath($this->image())}";
86
        }
87
88 1
        return "";
89
    }
90
91
    /**
92
     * @param string $path
93
     * @param int $width
94
     * @return string
95
     */
96 1
    private function getThumbnailFromPath(string $path, int $width = 300): string
97
    {
98 1
        $basename = basename($path);
99 1
        $extension = pathinfo($path, PATHINFO_EXTENSION);
100 1
        $filename = str_replace(".{$extension}", "", $basename);
101
102 1
        return "{$filename}_w{$width}.{$extension}";
103
    }
104
105
    /**
106
     * Get the path of the folder than contains all articles
107
     *
108
     * @return string
109
     */
110 1
    private function getImagesUrlPath(): string
111
    {
112 1
        return Config::get('aloiacms.articles.image_path');
113
    }
114
115
    /**
116
     * Get the description of this article
117
     *
118
     * @return string
119
     * @throws \Exception
120
     */
121 2
    public function description(): string
122
    {
123 2
        return $this->matter['description'] ?? $this->getDescriptionFromContent();
124
    }
125
126
    /**
127
     * Generate a description for the content
128
     *
129
     * @return bool|string
130
     * @throws \Exception
131
     */
132 2
    protected function getDescriptionFromContent(): string
133
    {
134 2
        $paragraphs = HtmlParser::getTextBetweenTags($this->body(), 'p');
135
136 2
        $paragraphs_with_text_content = array_filter($paragraphs, function ($paragraph) {
137 1
            return !empty(strip_tags($paragraph));
138 2
        });
139
140 2
        if (count($paragraphs_with_text_content) > 0) {
141 1
            return substr(head($paragraphs_with_text_content), 0, 160);
142
        }
143
144 1
        return "";
145
    }
146
147
    /**
148
     * Get the canonical if it's set
149
     *
150
     * @return null|string
151
     */
152 2
    public function canonicalLink(): ?string
153
    {
154 2
        return $this->matter['canonical'] ?? null;
155
    }
156
157
    /**
158
     * Get the external URL if it's set
159
     *
160
     * @return null|string
161
     */
162 2
    public function externalUrl(): ?string
163
    {
164 2
        return $this->matter['external_url'] ?? null;
165
    }
166
}
167