Meta::addExtra()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php namespace Arcanedev\LaravelSeo\Models;
2
3
use Arcanedev\LaravelSeo\Seo;
4
use Illuminate\Support\Arr;
5
6
/**
7
 * Class     Meta
8
 *
9
 * @package  Arcanedev\LaravelSeo\Models
10
 * @author   ARCANEDEV <[email protected]>
11
 *
12
 * @property  int                                  id
13
 * @property  int                                  seoable_id
14
 * @property  string                               seoable_type
15
 * @property  string                               title
16
 * @property  string                               description
17
 * @property  \Illuminate\Support\Collection       keywords
18
 * @property  boolean                              noindex
19
 * @property  \Illuminate\Support\Collection       extras
20
 * @property  \Carbon\Carbon                       created_at
21
 * @property  \Carbon\Carbon                       updated_at
22
 *
23
 * @property  \Illuminate\Database\Eloquent\Model  seoable
24
 */
25
class Meta extends AbstractModel
26
{
27
    /* -----------------------------------------------------------------
28
     |  Traits
29
     | -----------------------------------------------------------------
30
     */
31
32
    use Presenters\MetaPresenter;
33
34
    /* -----------------------------------------------------------------
35
     |  Properties
36
     | -----------------------------------------------------------------
37
     */
38
39
    /**
40
     * The attributes that are mass assignable.
41
     *
42
     * @var array
43
     */
44
    protected $fillable = ['title', 'description', 'keywords', 'noindex', 'extras'];
45
46
    /**
47
     * The attributes that should be casted to native types.
48
     *
49
     * @var array
50
     */
51
    protected $casts = [
52
        'id'         => 'integer',
53
        'seoable_id' => 'integer',
54
        'keywords'   => 'collection',
55
        'noindex'    => 'boolean',
56
        'extras'     => 'collection',
57
    ];
58
59
    /* -----------------------------------------------------------------
60
     |  Constructor
61
     | -----------------------------------------------------------------
62
     */
63
64
    /**
65
     * Meta constructor.
66
     *
67
     * @param  array  $attributes
68
     */
69 22
    public function __construct(array $attributes = [])
70
    {
71 22
        parent::__construct($attributes);
72
73 22
        $this->setTable(Seo::getConfig('metas.table', 'metas'));
74 22
    }
75
76
    /* -----------------------------------------------------------------
77
     |  Relationships
78
     | -----------------------------------------------------------------
79
     */
80
81
    /**
82
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
83
     */
84 4
    public function seoable()
85
    {
86 4
        return $this->morphTo();
87
    }
88
89
    /* -----------------------------------------------------------------
90
     |  Other Methods
91
     | -----------------------------------------------------------------
92
     */
93
94
    /**
95
     * Prepare the attributes.
96
     *
97
     * @param  array  $attributes
98
     *
99
     * @return array
100
     */
101 20
    public static function prepareAttributes(array $attributes)
102
    {
103
        return [
104 20
            'title'       => Arr::get($attributes, 'title'),
105 20
            'description' => Arr::get($attributes, 'description'),
106 20
            'keywords'    => Arr::get($attributes, 'keywords', []),
107 20
            'extras'      => Arr::get($attributes, 'extras', []),
108 20
            'noindex'     => Arr::get($attributes, 'noindex', false),
109
        ];
110
    }
111
112
    /**
113
     * Add an extra meta.
114
     *
115
     * @param  string  $key
116
     * @param  mixed   $value
117
     *
118
     * @return $this
119
     */
120 2
    public function addExtra($key, $value)
121
    {
122 2
        return $this->setExtras(
123 2
            $this->extras->put($key, $value)->all()
124
        );
125
    }
126
127
    /**
128
     * Add extra metas.
129
     *
130
     * @param  mixed  $extras
131
     *
132
     * @return $this
133
     */
134 2
    public function addExtras($extras)
135
    {
136 2
        return $this->setExtras(
137 2
            $this->extras->merge($extras)->all()
138
        );
139
    }
140
141
    /**
142
     * Set the extras.
143
     *
144
     * @param  mixed  $extras
145
     *
146
     * @return $this
147
     */
148 4
    public function setExtras($extras)
149
    {
150 4
        $this->extras = collect($extras);
151
152 4
        return $this;
153
    }
154
}
155