Redirect::createUnique()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class Redirect extends Model
8
{
9
    /**
10
     * Allow these attributes to be filled.
11
     *
12
     * @var array
13
     */
14
    protected $fillable = [
15
        'hash',
16
        'redirect_to',
17
    ];
18
19
    /**
20
     * The url attribute of a particular.
21
     *
22
     * @var array
23
     */
24
    protected $appends = [
25
        'url'
26
    ];
27
28
    /**
29
     * Hide unnecessary or private fields.
30
     *
31
     * @return array
32
     */
33
    protected $hidden = [
34
        'created_at',
35
        'updated_at'
36
    ];
37
38
    /**
39
     * Get the url attribute.
40
     *
41
     * @return void
42
     */
43 1
    public function getUrlAttribute()
44
    {
45 1
        return config()['base_url'] . '/' . $this->hash;
0 ignored issues
show
Bug Best Practice introduced by
The expression return config()['base_url'] . '/' . $this->hash returns the type string which is incompatible with the documented return type void.
Loading history...
Bug introduced by
The property hash does not seem to exist on App\Models\Redirect. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
46
    }
47
48
    /**
49
     * Creates an absolutely unique hash for a given url.
50
     *
51
     * @return void
52
     */
53 1
    public static function createUnique($url)
54
    {
55
        do {
56 1
            $hash = bin2hex(openssl_random_pseudo_bytes(4));
57 1
        } while (static::where('hash', $hash)->first());
58 1
        return static::create([
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::create(ar...'redirect_to' => $url)) returns the type App\Models\Redirect which is incompatible with the documented return type void.
Loading history...
59 1
            'hash' => $hash,
60 1
            'redirect_to' => $url
61
        ]);
62
    }
63
}
64