UrlRewrite   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 1
dl 0
loc 81
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A isForward() 0 4 1
A isRedirect() 0 4 1
A getRedirectType() 0 4 2
A getByTypeAndAttributes() 0 10 2
A getRedirectTypeOptionsArray() 0 8 1
A getPossibleTypesArray() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RuthgerIdema\UrlRewrite\Entities;
6
7
use Illuminate\Database\Eloquent\Model;
8
9
class UrlRewrite extends Model
10
{
11
    /** @var int */
12
    public const FORWARD = 0;
13
14
    /** @var int */
15
    public const PERMANENT = 1;
16
17
    /** @var int */
18
    public const TEMPORARY = 2;
19
20
    /** @var array */
21
    protected $fillable = [
22
        'type',
23
        'type_attributes',
24
        'request_path',
25
        'target_path',
26
        'redirect_type',
27
        'description',
28
    ];
29
30
    /** @var array */
31
    protected $casts = [
32
        'type_attributes' => 'array',
33
    ];
34
35
    public function __construct(?array $attributes = [])
36
    {
37
        if (! isset($this->table)) {
38
            $this->setTable(config('url-rewrite.table-name'));
39
        }
40
41
        parent::__construct($attributes);
42
    }
43
44
    public function isForward(): bool
45
    {
46
        return $this->redirect_type === static::FORWARD;
47
    }
48
49
    public function isRedirect(): bool
50
    {
51
        return $this->redirect_type !== static::FORWARD;
52
    }
53
54
    public function getRedirectType(): int
55
    {
56
        return $this->redirect_type === static::PERMANENT ? 301 : 302;
57
    }
58
59
    public function getByTypeAndAttributes(string $type, array $attributes)
60
    {
61
        $query = $this->where('type', $type);
62
63
        foreach ($attributes as $key => $attribute) {
64
            $query = $query->where("type_attributes->$key", (string) $attribute);
65
        }
66
67
        return $query;
68
    }
69
70
    public static function getRedirectTypeOptionsArray(): array
71
    {
72
        return [
73
            static::FORWARD => trans('urlrewrites::translations.forward'),
74
            static::PERMANENT => trans('urlrewrites::translations.permanent'),
75
            static::TEMPORARY => trans('urlrewrites::translations.temporary'),
76
        ];
77
    }
78
79
    public static function getPossibleTypesArray(): array
80
    {
81
        $array = [];
82
83
        foreach (array_keys(config('url-rewrite.types')) as $type) {
84
            $array[$type] = $type;
85
        }
86
87
        return $array;
88
    }
89
}
90