Category   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 88
ccs 14
cts 15
cp 0.9333
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A posts() 0 4 1
A getSelectData() 0 21 2
1
<?php namespace Arcanesoft\Blog\Models;
2
3
use Arcanesoft\Blog\Blog;
4
use Arcanesoft\Blog\Events\Categories as CategoryEvents;
5
6
/**
7
 * Class     Category
8
 *
9
 * @package  Arcanesoft\Blog\Models
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class Category extends AbstractTaxonomy
13
{
14
    /* -----------------------------------------------------------------
15
     |  Constants
16
     | -----------------------------------------------------------------
17
     */
18
19
    const SELECT_CACHE_NAME = 'blog::categories.select-data';
20
21
    /* -----------------------------------------------------------------
22
     |  Properties
23
     | -----------------------------------------------------------------
24
     */
25
26
    /**
27
     * The database table used by the model
28
     *
29
     * @var string
30
     */
31
    protected $table = 'categories';
32
33
    /**
34
     * The event map for the model.
35
     *
36
     * @var array
37
     */
38
    protected $dispatchesEvents = [
39
        'creating'  => CategoryEvents\CategoryCreating::class,
40
        'created'   => CategoryEvents\CategoryCreated::class,
41
        'updating'  => CategoryEvents\CategoryUpdating::class,
42
        'updated'   => CategoryEvents\CategoryUpdated::class,
43
        'saving'    => CategoryEvents\CategorySaving::class,
44
        'saved'     => CategoryEvents\CategorySaved::class,
45
        'deleting'  => CategoryEvents\CategoryDeleting::class,
46
        'deleted'   => CategoryEvents\CategoryDeleted::class,
47
        'restoring' => CategoryEvents\CategoryRestoring::class,
48
        'restored'  => CategoryEvents\CategoryRestored::class,
49
    ];
50
51
    /* -----------------------------------------------------------------
52
     |  Relationships
53
     | -----------------------------------------------------------------
54
     */
55
56
    /**
57
     * Relationship with posts.
58
     *
59
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
60
     */
61 3
    public function posts()
62
    {
63 3
        return $this->hasMany(Post::class);
64
    }
65
66
    /* -----------------------------------------------------------------
67
     |  Main Methods
68
     | -----------------------------------------------------------------
69
     */
70
71
    /**
72
     * Get the categories options for select input.
73
     *
74
     * @param  bool  $placeholder
75
     *
76
     * @return \Illuminate\Support\Collection
77
     */
78 3
    public static function getSelectData($placeholder = true)
79
    {
80 3
        $minutes = config('arcanesoft.blog.cache.categories.select-data', 5);
81
82
        /** @var  \Illuminate\Database\Eloquent\Collection  $categories */
83 3
        return cache()->remember(self::SELECT_CACHE_NAME, $minutes, function () {
84 3
                $withTranslations = Blog::isTranslatable();
85
86 3
                return self::all()->mapWithKeys(function (Category $category) use ($withTranslations) {
87
                    return [
88 3
                        $category->id => $withTranslations
89
                            ? implode(' / ', $category->getTranslations('name'))
90 3
                            : $category->name
91
                    ];
92 3
                });
93 3
            })
94 3
            ->toBase()->when($placeholder, function ($categories) {
95
                /** @var  \Illuminate\Support\Collection  $categories */
96 3
                return $categories->prepend(trans('blog::categories.select-category'), 0);
97 3
            });
98
    }
99
}
100