Passed
Push — master ( e1f169...671bd8 )
by Dan Michael O.
02:49
created

Item::library()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App;
4
5
use Illuminate\Database\Eloquent\Model;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Model was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Database\Eloquent\SoftDeletes;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\SoftDeletes was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use function Stringy\create as s;
0 ignored issues
show
introduced by
The function Stringy\create was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
8
9
class Item extends Model
10
{
11
12
    use SoftDeletes;
13
14
    /**
15
     * The attributes that should be mutated to dates.
16
     *
17
     * @var array
18
     */
19
    protected $dates = ['deleted_at'];
20
21
    /**
22
     * The attributes that are mass assignable.
23
     *
24
     * @var array
25
     */
26
    protected $fillable = ['barcode', 'library_id', 'note', 'thing_id'];
27
28
    /**
29
     * The accessors to append to the model's array form.
30
     *
31
     * @var array
32
     */
33
    protected $appends = ['last_loan', 'available'];
34
35
    /**
36
     * The attributes that should be cast to native types.
37
     *
38
     * @var array
39
     */
40
    protected $casts = [
41
        'properties' => 'array',
42
    ];
43
44
    public function thing()
45
    {
46
        return $this->belongsTo(Thing::class, 'thing_id');
47
    }
48
49
    public function library()
50
    {
51
        return $this->belongsTo(Library::class, 'library_id');
52
    }
53
54
    /**
55
     * Get the loans for this items. By default, only active loans are returned,
56
     * so the result should be zero or one loan. But all active and former loans can
57
     * be returned by adding `withTrashed()`.
58
     */
59
    public function loans()
60
    {
61
        return $this->hasMany(Loan::class)
62
            ->with('user')
63
            ->latest();
64
    }
65
66
    /**
67
     * Get the last loan, active or not.
68
     *
69
     * @return bool
70
     */
71
    public function getLastLoanAttribute()
72
    {
73
        return $this->attributes['last_loan'] = $this->loans()
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
74
                ->withTrashed()
75
                ->orderBy('created_at', 'desc')
76
                ->first();
77
    }
78
79
    /**
80
     * Whether item is available or not.
81
     *
82
     * @return bool
83
     */
84
    public function getAvailableAttribute()
85
    {
86
        return is_null($this->attributes['available'] = $this->loans()->first());
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
87
    }
88
89
    public function allLoans()
90
    {
91
        $library_id = \Auth::user()->id;
0 ignored issues
show
Bug introduced by
The type Auth was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
92
93
        return $this->hasMany(Loan::class)
94
            ->with('user')
95
            ->withTrashed()
96
            ->where('library_id', $library_id)
97
            ->orderBy('created_at', 'desc');
98
    }
99
100
    public function lost()
101
    {
102
        $this->is_lost = true;
0 ignored issues
show
Bug Best Practice introduced by
The property is_lost does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
103
        $this->save();
104
        $this->delete();
105
    }
106
107
    public function found()
108
    {
109
        $this->restore();
110
111
        if ($this->is_lost) {
112
            \Log::info(sprintf(
113
                'Registrerte %s som funnet.',
114
                $this->formattedLink(false, false)
115
            ), ['library' => \Auth::user()->name]);
116
            $this->is_lost = false;
0 ignored issues
show
Bug Best Practice introduced by
The property is_lost does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
117
            $this->save();
118
        }
119
    }
120
121
    public function formattedLink($ucfirst = false, $definite = true)
122
    {
123
        $name = s($this->thing->properties->get($definite ? 'name_definite.nob' : 'name_indefinite.nob'));
0 ignored issues
show
Bug introduced by
The function create was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

123
        $name = /** @scrutinizer ignore-call */ s($this->thing->properties->get($definite ? 'name_definite.nob' : 'name_indefinite.nob'));
Loading history...
124
        $name = $ucfirst ? $name->upperCaseFirst() : $name->lowerCaseFirst();
125
126
        return sprintf(
127
            '<a href="%s">%s</a>',
128
            action('ItemsController@show', $this->id),
0 ignored issues
show
Bug introduced by
The function action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

128
            /** @scrutinizer ignore-call */ 
129
            action('ItemsController@show', $this->id),
Loading history...
129
            $name
130
        );
131
    }
132
}
133