Thing::availableItems()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace App;
4
5
use DateTimeInterface;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\SoftDeletes;
8
9
class Thing extends Model
10
{
11
12
    use SoftDeletes;
13
14
    protected $guarded = array();
15
16
    /**
17
     * The attributes that should be cast to native types.
18
     *
19
     * @var array
20
     */
21
    protected $casts = [
22
        'image' => 'object',
23
    ];
24
25
    public function items()
26
    {
27
        return $this->hasMany(Item::class);
28
    }
29
30
    public function settings()
31
    {
32
        return $this->hasMany(ThingSettings::class);
33
    }
34
35
    /**
36
     * Prepare a date for array / JSON serialization.
37
     *
38
     * @param  \DateTimeInterface  $date
39
     * @return string
40
     */
41
    protected function serializeDate(DateTimeInterface $date)
42
    {
43
        return $date->format('Y-m-d H:i:s');
44
    }
45
46
    /**
47
     * The attributes that are mass assignable.
48
     *
49
     * @var array
50
     */
51
    protected $fillable = ['properties', 'note', 'image'];
52
53
    /**
54
     * The attributes that should be mutated to dates.
55
     *
56
     * @var array
57
     */
58
    protected $dates = ['deleted_at'];
59
60
    /**
61
     * The accessors to append to the model's array form.
62
     *
63
     * @var array
64
     */
65
    protected $appends = ['library_settings'];
66
67
    public function activeLoans()
68
    {
69
        $loans = array();
70
        foreach ($this->items as $item) {
71
            foreach ($item->loans as $loan) {
72
                $loans[] = $loan;
73
            }
74
        }
75
        return $loans;
76
    }
77
78
    /**
79
     * The settings for this thing at my library.
80
     *
81
     * @return ThingSettings
82
     */
83
    public function getLibrarySettingsAttribute(Library $library = null)
84
    {
85
        $library = $library ?? \Auth::user();
86
87
        return $this->settings()
88
            ->where('library_id', $library->id)
89
            ->first() ?? ThingSettings::make([
90
                'library_id' => $library->id,
91
                'thing_id' => $this->id,
92
            ]);
93
    }
94
95
    public function availableItems()
96
    {
97
        return $this->items()->count() - count($this->activeLoans());
98
    }
99
100
    public function allLoans()
101
    {
102
        $loans = array();
103
        foreach ($this->items as $item) {
104
            foreach ($item->allLoans as $loan) {
105
                $loans[] = $loan;
106
            }
107
        }
108
        return $loans;
109
    }
110
111
    public function name($lang = 'nob')
112
    {
113
        return $this->properties->get("name.$lang");
114
    }
115
116
    public function getPropertiesAttribute()
117
    {
118
        return new ThingProperties(json_decode($this->attributes['properties'], true));
119
    }
120
121
    public function setPropertiesAttribute($value)
122
    {
123
        $this->attributes['properties'] = json_encode($value);
124
    }
125
}
126