Url::redirectsTo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 2
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Luminark\Url\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Carbon\Carbon;
7
use Exception;
8
9
class Url extends Model
10
{
11
    public $timestamps = false;
12
13
    public $incrementing = false;
14
15
    protected $fillable = ['uri', 'redirects_to'];
16
17
    protected $primaryKey = 'uri';
18
19
    protected $with = ['resource', 'redirectsTo'];
20
21
    protected static function boot()
22
    {
23
        parent::boot();
24
        static::creating(function (Url $url) {
25
            $url->created_at = Carbon::now();
0 ignored issues
show
Documentation introduced by
The property created_at does not exist on object<Luminark\Url\Models\Url>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
26
        });
27
        static::deleting(function (Url $url) {
28
            if ($url->redirectedToBy) {
0 ignored issues
show
Documentation introduced by
The property redirectedToBy does not exist on object<Luminark\Url\Models\Url>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
29
                $url->redirectedToBy->each(function (Url $url) {
0 ignored issues
show
Documentation introduced by
The property redirectedToBy does not exist on object<Luminark\Url\Models\Url>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
30
                    $url->delete();
31
                });
32
            }
33
        });
34
    }
35
36
    public function resource()
37
    {
38
        return $this->morphTo();
39
    }
40
41
    public function redirectsTo()
42
    {
43
        return $this->belongsTo(static::class, 'redirects_to', 'uri');
44
    }
45
46
    public function redirectedToBy()
47
    {
48
        return $this->hasMany(static::class, 'redirects_to', 'uri');
49
    }
50
    
51
    public function setUriAttribute($uri)
52
    {
53
        if ($this->exists) {
54
            throw new Exception(
55
                "An existing Url object's uri parameter cannot be updated, update the resource's uri attribute instead."
56
            );
57
        }
58
        
59
        $this->attributes['uri'] = $uri;
60
    }
61
    
62
    public function getUrlAttribute()
63
    {
64
        return '/' . $this->uri;
0 ignored issues
show
Documentation introduced by
The property uri does not exist on object<Luminark\Url\Models\Url>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
65
    }
66
67
    public function __toString()
68
    {
69
        return $this->url;
0 ignored issues
show
Documentation introduced by
The property url does not exist on object<Luminark\Url\Models\Url>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
70
    }
71
}
72