Link::user()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace App;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class Link extends Model
8
{
9
    /**
10
     * The attributes that are mass assignable.
11
     *
12
     * @var array
13
     */
14
    protected $fillable = [
15
        'url',
16
        'hash',
17
        'user_id',
18
    ];
19
20
    /**
21
     * Generate a link with a unique hash.
22
     *
23
     * @see      generateUniqueHash
24
     *
25
     * @param string    $url
26
     * @param User|null $user
27
     *
28
     * @return mixed
29
     *
30
     * @internal param int $user_id
31
     */
32
    public function generate($url = '', User $user = null)
33
    {
34
        $hash = $this->generateUniqueHash();
35
        $data = [
36
            'url'  => $url,
37
            'hash' => $hash,
38
        ];
39
40
        /*
41
         * If the user_id has been offered
42
         * add the user_id so we can build
43
         * a relationship between link and
44
         * user.
45
         */
46
        if ($user) {
47
            $data['user_id'] = $user->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\User>. 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...
48
        }
49
50
        /**
51
         * Finally insert the link into
52
         * the database.
53
         */
54
        $link = self::create($data);
0 ignored issues
show
Bug introduced by
The method create() does not exist on App\Link. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
55
56
        return $link;
57
    }
58
59
    /**
60
     * Create a hash for this link to use.
61
     *
62
     * @return int
63
     */
64
    private function generateUniqueHash()
65
    {
66
        $number = str_random(config('myio.general.hash_length')); // better than rand()
67
68
        if (self::whereHash($number)->exists()) {
69
            return $this->generateUniqueHash();
70
        }
71
72
        return $number;
73
    }
74
75
    /**
76
     * Return the user for this link.
77
     *
78
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
79
     */
80
    public function user()
81
    {
82
        return $this->belongsTo(User::class);
83
    }
84
85
    /**
86
     * Return the hits for this link.
87
     *
88
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
89
     */
90
    public function hits()
91
    {
92
        return $this->hasMany(Hit::class);
93
    }
94
}
95