GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 3256f8...9fb5eb )
by Andrei
02:11
created

Redirect::syncOldRedirects()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.9666
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Neurony\Redirects\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Builder;
7
use Neurony\Redirects\Exceptions\RedirectException;
8
use Neurony\Redirects\Contracts\RedirectModelContract;
9
10
class Redirect extends Model implements RedirectModelContract
11
{
12
    /**
13
     * The database table.
14
     *
15
     * @var string
16
     */
17
    protected $table = 'redirects';
18
19
    /**
20
     * The attributes that are mass assignable.
21
     *
22
     * @var array
23
     */
24
    protected $fillable = [
25
        'old_url',
26
        'new_url',
27
        'status',
28
    ];
29
30
    /**
31
     * Boot the model.
32
     *
33
     * @return void
34
     */
35
    public static function boot()
36
    {
37
        parent::boot();
38
39
        static::saving(function (Redirect $model) {
40
            if (trim(strtolower($model->old_url), '/') == trim(strtolower($model->new_url), '/')) {
41
                throw RedirectException::sameUrls();
42
            }
43
44
            static::whereOldUrl($model->new_url)->whereNewUrl($model->old_url)->delete();
45
46
            $model->syncOldRedirects($model, $model->new_url);
47
        });
48
    }
49
50
    /**
51
     * The mutator to set the "old_url" attribute.
52
     *
53
     * @param string $value
54
     */
55
    public function setOldUrlAttribute($value)
56
    {
57
        $this->attributes['old_url'] = trim(parse_url($value)['path'], '/');
58
    }
59
60
    /**
61
     * The mutator to set the "new_url" attribute.
62
     *
63
     * @param string $value
64
     */
65
    public function setNewUrlAttribute($value)
66
    {
67
        $this->attributes['new_url'] = trim(parse_url($value)['path'], '/');
68
    }
69
70
    /**
71
     * Filter the query by an old url.
72
     *
73
     * @param Builder $query
74
     * @param string $url
75
     *
76
     * @return mixed
77
     */
78
    public function scopeWhereOldUrl($query, string $url)
79
    {
80
        return $query->where('old_url', $url);
81
    }
82
83
    /**
84
     * Filter the query by a new url.
85
     *
86
     * @param Builder $query
87
     * @param string $url
88
     *
89
     * @return mixed
90
     */
91
    public function scopeWhereNewUrl($query, string $url)
92
    {
93
        return $query->where('new_url', $url);
94
    }
95
96
    /**
97
     * Get all redirect statuses defined inside the "config/redirects.php" file.
98
     *
99
     * @return array
100
     */
101
    public static function getStatuses(): array
102
    {
103
        return (array) config('redirects.statuses', []);
104
    }
105
106
    /**
107
     * Sync old redirects to point to the new (final) url.
108
     *
109
     * @param RedirectModelContract $model
110
     * @param string $finalUrl
111
     * @return void
112
     */
113
    public function syncOldRedirects(RedirectModelContract $model, string $finalUrl): void
114
    {
115
        $items = static::whereNewUrl($model->old_url)->get();
0 ignored issues
show
Bug introduced by
Accessing old_url on the interface Neurony\Redirects\Contracts\RedirectModelContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
116
117
        foreach ($items as $item) {
118
            $item->update(['new_url' => $finalUrl]);
119
            $item->syncOldRedirects($model, $finalUrl);
120
        }
121
    }
122
123
    /**
124
     * Return a valid redirect entity for a given path (old url).
125
     * A redirect is valid if:
126
     * - it has an url to redirect to (new url)
127
     * - it's status code is one of the statuses defined on this model.
128
     *
129
     * @param string $path
130
     * @return Redirect|null
131
     */
132
    public static function findValidOrNull($path): ?RedirectModelContract
133
    {
134
        return static::where('old_url', $path === '/' ? $path : trim($path, '/'))
135
            ->whereNotNull('new_url')
136
            ->whereIn('status', array_keys(self::getStatuses()))
137
            ->latest()->first();
138
    }
139
}
140