Series   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 1
dl 0
loc 157
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A scopeExcludeCurrent() 0 4 1
A getModelUrlParams() 0 6 1
1
<?php
2
3
namespace GinoPane\BlogTaxonomy\Models;
4
5
use System\Models\File;
6
use RainLab\Blog\Models\Post;
7
use GinoPane\BlogTaxonomy\Plugin;
8
use October\Rain\Database\Builder;
9
use October\Rain\Database\Traits\Sluggable;
10
use October\Rain\Database\Traits\Validation;
11
12
/**
13
 * Class Series
14
 *
15
 * @property string title
16
 * @property string slug
17
 * @property string description
18
 *
19
 * @package GinoPane\BlogTaxonomy\Models
20
 */
21
class Series extends ModelAbstract
22
{
23
    const STATUS_ACTIVE = 'active';
24
    const STATUS_INACTIVE = 'inactive';
25
26
    use Sluggable;
27
    use Validation;
28
29
    const TABLE_NAME = 'ginopane_blogtaxonomy_series';
30
31
    const RELATED_SERIES_TABLE_NAME = 'ginopane_blogtaxonomy_related_series';
32
33
    /**
34
     * The database table used by the model
35
     *
36
     * @var string
37
     */
38
    public $table = self::TABLE_NAME;
39
40
    /**
41
     * Specifying of implemented behaviours as strings is convenient when
42
     * the target behaviour could be missing due to disabled or not installed
43
     * plugin. You won't get an error, the plugin would simply work without model
44
     *
45
     * @var array
46
     */
47
    public $implement = ['@RainLab.Translate.Behaviors.TranslatableModel'];
48
49
    /**
50
     * Translatable properties, indexed property will be available in queries
51
     *
52
     * @var array
53
     */
54
    public $translatable = [
55
        'title',
56
        [
57
            'slug',
58
            'index' => true
59
        ],
60
        'description'
61
    ];
62
63
    /**
64
     * Has-many relations
65
     *
66
     * @var array
67
     */
68
    public $hasMany = [
69
        'posts' => [
70
            Post::class,
71
            'key' => self::TABLE_NAME . "_id"
72
        ],
73
    ];
74
75
    public $morphToMany = [
76
        'tags' => [Tag::class, 'name' => Tag::PIVOT_COLUMN]
77
    ];
78
79
    /**
80
     * Belongs-to-many relations
81
     *
82
     * @var array
83
     */
84
    public $belongsToMany = [
85
        'related_series' => [
86
            Series::class,
87
            'table' => Series::RELATED_SERIES_TABLE_NAME,
88
            'order' => 'id',
89
            'key' => 'series_id',
90
            'otherKey' => 'related_series_id'
91
        ]
92
    ];
93
94
    public $attachMany = [
95
        'featured_images' => [
96
            File::class,
97
            'order' => 'sort_order',
98
            'delete' => true
99
        ]
100
    ];
101
102
    public $attachOne = [
103
        'cover_image' => [
104
            File::class,
105
            'delete' => true
106
        ]
107
    ];
108
109
    /**
110
     * Validation rules
111
     *
112
     * @var array
113
     */
114
    public $rules = [
115
        'title' => 'required|unique:' . self::TABLE_NAME . '|min:3',
116
        'slug'  => 'required|unique:' . self::TABLE_NAME . '|min:3|regex:/^[\w\-]+$/iu'
117
    ];
118
119
    /**
120
     * Validation messages
121
     *
122
     * @var array
123
     */
124
    public $customMessages = [
125
        'title.required' => Plugin::LOCALIZATION_KEY . 'form.series.title_required',
126
        'title.unique'   => Plugin::LOCALIZATION_KEY . 'form.series.title_unique',
127
        'title.min'      => Plugin::LOCALIZATION_KEY . 'form.series.title_too_short',
128
129
        'slug.required' => Plugin::LOCALIZATION_KEY . 'form.series.slug_required',
130
        'slug.unique'   => Plugin::LOCALIZATION_KEY . 'form.series.slug_unique',
131
        'slug.regex'    => Plugin::LOCALIZATION_KEY . 'form.series.slug_invalid',
132
        'slug.min'      => Plugin::LOCALIZATION_KEY . 'form.series.slug_too_short',
133
    ];
134
135
    /**
136
     * The attributes on which the post list can be ordered
137
     *
138
     * @var array
139
     */
140
    public static $sortingOptions = [
141
        'title asc' => Plugin::LOCALIZATION_KEY . 'order_options.title_asc',
142
        'title desc' => Plugin::LOCALIZATION_KEY . 'order_options.title_desc',
143
        'created_at asc' => Plugin::LOCALIZATION_KEY . 'order_options.created_at_asc',
144
        'created_at desc' => Plugin::LOCALIZATION_KEY . 'order_options.created_at_desc',
145
        'posts_count asc' => Plugin::LOCALIZATION_KEY . 'order_options.post_count_asc',
146
        'posts_count desc' => Plugin::LOCALIZATION_KEY . 'order_options.post_count_desc',
147
        'random' => Plugin::LOCALIZATION_KEY . 'order_options.random'
148
    ];
149
150
    /**
151
     * @var array
152
     */
153
    protected $slugs = ['slug' => 'title'];
154
155
    /**
156
     * @param Builder $query
157
     * @param Series  $current
158
     *
159
     * @return Builder
160
     */
161
    public function scopeExcludeCurrent(Builder $query, Series $current): Builder
162
    {
163
        return $query->where('id', '!=', $current->id);
164
    }
165
166
    /**
167
     * @param array $params
168
     *
169
     * @return array
170
     */
171
    protected function getModelUrlParams(array $params): array
172
    {
173
        return [
174
            array_get($params, 'series', 'series') => $this->slug
175
        ];
176
    }
177
}
178