Completed
Push — master ( d43f4a...846842 )
by ARCANEDEV
05:01
created

Redirect::getStatusNameAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Arcanedev\LaravelSeo\Models;
2
3
use Arcanedev\LaravelSeo\Entities\RedirectStatuses;
4
use Arcanedev\LaravelSeo\Seo;
5
use Symfony\Component\HttpFoundation\Response;
6
7
/**
8
 * Class     Redirect
9
 *
10
 * @package  Arcanedev\LaravelSeo\Models
11
 * @author   ARCANEDEV <[email protected]>
12
 *
13
 * @property  int             id
14
 * @property  string          old_url
15
 * @property  string          new_url
16
 * @property  int             status
17
 * @property  string          status_name
18
 * @property  \Carbon\Carbon  created_at
19
 * @property  \Carbon\Carbon  updated_at
20
 */
21
class Redirect extends AbstractModel
22
{
23
    /* -----------------------------------------------------------------
24
     |  Properties
25
     | -----------------------------------------------------------------
26
     */
27
    /**
28
     * The attributes that are mass assignable.
29
     *
30
     * @var array
31
     */
32
    protected $fillable = ['old_url', 'new_url', 'status'];
33
34
    /**
35
     * The attributes that should be cast to native types.
36
     *
37
     * @var array
38
     */
39
    protected $casts = [
40
        'status' => 'integer',
41
    ];
42
43
    /* -----------------------------------------------------------------
44
     |  Constructor
45
     | -----------------------------------------------------------------
46
     */
47
    /**
48
     * Meta constructor.
49
     *
50
     * @param  array  $attributes
51
     */
52 6
    public function __construct(array $attributes = [])
53
    {
54 6
        parent::__construct($attributes);
55
56 6
        $this->setTable(Seo::getConfig('redirects.table', 'redirects'));
57 6
    }
58
59
    /* -----------------------------------------------------------------
60
     |  Getters & Setters
61
     | -----------------------------------------------------------------
62
     */
63
    /**
64
     * Get the `status_name` attribute.
65
     *
66
     * @return string
67
     */
68 3
    public function getStatusNameAttribute()
69
    {
70 3
        return RedirectStatuses::get($this->status);
71
    }
72
73
    /* -----------------------------------------------------------------
74
     |  Main Methods
75
     | -----------------------------------------------------------------
76
     */
77
    /**
78
     * Create a redirect url.
79
     *
80
     * @param  string  $oldUrl
81
     * @param  string  $newUrl
82
     * @param  int     $status
83
     *
84
     * @return \Arcanedev\LaravelSeo\Models\Redirect
85
     */
86 6
    public static function createOne($oldUrl, $newUrl, $status = Response::HTTP_MOVED_PERMANENTLY)
87
    {
88 6
        $redirect = new self([
89 6
            'old_url' => $oldUrl,
90 6
            'new_url' => $newUrl,
91 6
            'status'  => $status,
92 2
        ]);
93
94 6
        $redirect->save();
95
96 6
        return $redirect;
97
    }
98
}
99