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

Thing::activeLoans()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 9
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
8
class Thing extends Model
9
{
10
11
    use SoftDeletes;
12
13
    protected $guarded = array();
14
15
    public function items()
16
    {
17
        return $this->hasMany(Item::class);
18
    }
19
20
21
22
    public function settings()
23
    {
24
        return $this->hasMany(ThingSettings::class);
25
    }
26
27
    /**
28
     * The attributes that are mass assignable.
29
     *
30
     * @var array
31
     */
32
    protected $fillable = ['properties', 'note'];
33
34
    /**
35
     * The attributes that should be mutated to dates.
36
     *
37
     * @var array
38
     */
39
    protected $dates = ['deleted_at'];
40
41
    /**
42
     * The accessors to append to the model's array form.
43
     *
44
     * @var array
45
     */
46
    protected $appends = ['library_settings'];
47
48
    public function activeLoans()
49
    {
50
        $loans = array();
51
        foreach ($this->items as $item) {
52
            foreach ($item->loans as $loan) {
53
                $loans[] = $loan;
54
            }
55
        }
56
        return $loans;
57
    }
58
59
    /**
60
     * The settings for this thing at my library.
61
     *
62
     * @return ThingSettings
63
     */
64
    public function getLibrarySettingsAttribute(Library $library = null)
65
    {
66
        $library = $library ?? \Auth::user();
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...
67
68
        return $this->settings()
69
            ->where('library_id', $library->id)
70
            ->first() ?? ThingSettings::make([
71
                'library_id' => $library->id,
72
                'thing_id' => $this->id,
73
            ]);
74
    }
75
76
    public function availableItems()
77
    {
78
        return $this->items()->count() - count($this->activeLoans());
79
    }
80
81
    public function allLoans()
82
    {
83
        $loans = array();
84
        foreach ($this->items as $item) {
85
            foreach ($item->allLoans as $loan) {
86
                $loans[] = $loan;
87
            }
88
        }
89
        return $loans;
90
    }
91
92
    public function name($lang = 'nob')
93
    {
94
        return $this->properties->get("name.$lang");
95
    }
96
97
    public function getPropertiesAttribute()
98
    {
99
        return new ThingProperties(json_decode($this->attributes['properties'], true));
100
    }
101
102
    public function setPropertiesAttribute($value)
103
    {
104
        $this->attributes['properties'] = json_encode($value);
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...
105
    }
106
}
107