Tag   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 109
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A link() 0 6 1
A posts() 0 3 1
A layout() 0 5 2
A addNeededTags() 0 16 3
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class Tag extends Model
8
{
9
    /**
10
     * The database table used by the model.
11
     *
12
     * @var string
13
     */
14
    protected $table = 'tags';
15
16
    /**
17
     * The attributes that are not mass assignable.
18
     *
19
     * @var array
20
     */
21
    protected $guarded = [
22
        'id',
23
    ];
24
25
    /**
26
     * Fillable fields for a Profile.
27
     *
28
     * @var array
29
     */
30
    protected $fillable = [
31
        'tag',
32
        'title',
33
        'subtitle',
34
        'post_image',
35
        'meta_description',
36
        'reverse_direction',
37
    ];
38
39
    /**
40
     * Typecasting is awesome.
41
     *
42
     * @var array
43
     */
44
    protected $casts = [
45
        'tag'               => 'string',
46
        'title'             => 'string',
47
        'subtitle'          => 'string',
48
        'post_image'        => 'string',
49
        'meta_description'  => 'string',
50
        'reverse_direction' => 'boolean',
51
    ];
52
53
    /**
54
     * The many-to-many relationship between tags and posts.
55
     *
56
     * @return BelongsToMany
0 ignored issues
show
Bug introduced by
The type App\Models\BelongsToMany was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
57
     */
58
    public function posts()
59
    {
60
        return $this->belongsToMany('App\Models\Post', 'post_tag_pivot');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->belongsToM...ost', 'post_tag_pivot') returns the type Illuminate\Database\Eloq...Relations\BelongsToMany which is incompatible with the documented return type App\Models\BelongsToMany.
Loading history...
61
    }
62
63
    /**
64
     * Return a tag link.
65
     *
66
     * @param string $base
67
     *
68
     * @return string
69
     */
70
    public function link($base = '/?tag=%TAG%')
71
    {
72
        $url = str_replace('%TAG%', urlencode($this->tag), $base);
73
        $tagLink = '<a href="'.$url.'">'.e($this->tag).'</a>';
74
75
        return $tagLink;
76
    }
77
78
    /**
79
     * Add any tags needed from the list.
80
     *
81
     * @param array $tags List of tags to check/add
82
     */
83
    public static function addNeededTags(array $tags)
84
    {
85
        if (count($tags) === 0) {
86
            return;
87
        }
88
89
        $found = static::whereIn('tag', $tags)->pluck('tag')->all();
90
91
        foreach (array_diff($tags, $found) as $tag) {
92
            static::create([
93
                'tag'               => $tag,
94
                'title'             => $tag,
95
                'subtitle'          => 'Articles tagged: '.$tag,
96
                'post_image'        => '',
97
                'meta_description'  => '',
98
                'reverse_direction' => false,
99
            ]);
100
        }
101
    }
102
103
    /**
104
     * Return the index layout to use for a tag.
105
     *
106
     * @param string $tag
107
     * @param string $default
108
     *
109
     * @return string
110
     */
111
    public static function layout($tag, $default = 'blog.roll-layouts.home')
112
    {
113
        $layout = static::whereTag($tag)->pluck('layout');
114
115
        return $layout[0] ?: $default;
116
    }
117
}
118