Loan::daysLeft()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 15
rs 9.9666
1
<?php
2
3
namespace App;
4
5
use App\Notifications\ExtendedDatabaseNotification;
6
use DateTimeInterface;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\SoftDeletes;
9
use Carbon\Carbon;
10
use Illuminate\Notifications\Notification;
11
use Illuminate\Support\MessageBag;
12
13
class Loan extends Model
14
{
15
16
    use SoftDeletes;
17
18
    protected $guarded = array();
19
20
    public $errors;
21
22
    /**
23
     * The attributes that are mass assignable.
24
     *
25
     * @var array
26
     */
27
    protected $fillable = ['user_id', 'item_id', 'due_at', 'as_guest'];
28
29
    /**
30
     * The attributes that should be mutated to dates.
31
     *
32
     * @var array
33
     */
34
    protected $dates = ['due_at', 'deleted_at'];
35
36
    /**
37
     * The accessors to append to the model's array form.
38
     *
39
     * @var array
40
     */
41
    protected $appends = ['url', 'created_at_relative', 'days_left'];
42
43
    /**
44
     * Prepare a date for array / JSON serialization.
45
     *
46
     * @param  \DateTimeInterface  $date
47
     * @return string
48
     */
49
    protected function serializeDate(DateTimeInterface $date)
50
    {
51
        return $date->format('Y-m-d H:i:s');
52
    }
53
54
    public function user()
55
    {
56
        return $this->belongsTo(User::class);
57
    }
58
59
    public function item()
60
    {
61
        return $this->belongsTo(Item::class)
62
            ->withTrashed();
63
    }
64
65
    public function notifications()
66
    {
67
        return $this->hasMany(ExtendedDatabaseNotification::class)
68
            ->orderBy('created_at', 'desc');
69
    }
70
71
    public function library()
72
    {
73
        return $this->belongsTo(Library::class);
74
    }
75
76
    /**
77
     * Get library settings for the loaned thing.
78
     *
79
     * @return ThingSettings
80
     */
81
    public function getLibrarySettings()
82
    {
83
        return $this->item->thing->getLibrarySettingsAttribute($this->library);
84
    }
85
86
    public function representation($plaintext = false)
0 ignored issues
show
Unused Code introduced by
The parameter $plaintext is not used and could be removed. ( Ignorable by Annotation )

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

86
    public function representation(/** @scrutinizer ignore-unused */ $plaintext = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
    {
88
        if ($this->item->barcode) {
89
            return "{$this->item->thing->name()} <samp>(#{$this->item->barcode})</samp>";
90
        } else {
91
            return $this->item->thing->name();
92
        }
93
    }
94
95
    public function daysLeft()
96
    {
97
        if (is_null($this->due_at)) {
98
            return 999999;
99
        }
100
        $d1 = $this->due_at;
101
        $d2 = Carbon::now();
102
        $days = $d2->diffInDays($d1, false);
103
        $hours = $d2->diffInHours($d1, false);
104
105
        if ($hours > 0) {
106
            $days++;
107
        }
108
109
        return $days;
110
    }
111
112
    public function getDaysLeftAttribute()
113
    {
114
        return $this->daysLeft();
115
    }
116
117
    public function getUrlAttribute()
118
    {
119
        return action('LoansController@getShow', ['loan' => $this->id]);
120
    }
121
122
    public function relativeCreationTimeHours()
123
    {
124
        Carbon::setLocale('no');
125
        $hours = $this->created_at->diffInHours(Carbon::now());
126
        return $hours;
127
    }
128
129
    public function relativeCreationTime($lang = null)
130
    {
131
        $lang = $lang ?? $this->user->lang;
132
        if ($lang == 'eng') {
133
            Carbon::setLocale('en');
134
            $msgs = [
135
                'justnow' => 'just now',
136
                'today' => '{hours} hour(s) ago',
137
                'yesterday' => 'yesterday',
138
                '2days' => 'two days ago',
139
                'generic' => '{diff} ago',
140
            ];
141
        } else {
142
            Carbon::setLocale('no');
143
            $msgs = [
144
                'justnow' => 'nå nettopp',
145
                'today' => 'for {hours} time(r) siden',
146
                'yesterday' => 'i går',
147
                '2days' => 'i forgårs',
148
                'generic' => 'for {diff} siden',
149
            ];
150
        }
151
152
        $now = Carbon::now();
153
        $diffHours = $this->created_at->diffInHours($now);
154
        if ($diffHours < 1) {
155
            return $msgs['justnow'];
156
        }
157
        if ($now->dayOfYear - $this->created_at->dayOfYear == 1) {
0 ignored issues
show
Bug introduced by
The property dayOfYear does not exist on string.
Loading history...
158
            return $msgs['yesterday'];
159
        }
160
        if ($now->dayOfYear - $this->created_at->dayOfYear == 2) {
161
            return $msgs['2days'];
162
        }
163
        if ($diffHours < 48) {
164
            return str_replace('{hours}', $diffHours, $msgs['today']);
165
        }
166
        return str_replace(
167
            '{diff}',
168
            $this->created_at
169
                ->setTime(0, 0, 0)
170
                ->diffForHumans(Carbon::now(), true),
171
            $msgs['generic']
172
        );
173
    }
174
175
    public function getCreatedAtRelativeAttribute()
176
    {
177
        return $this->relativeCreationTime('nob');
178
    }
179
180
    /**
181
     * Save the model to the database.
182
     *
183
     * @param  array  $options
184
     * @return bool
185
     */
186
    public function save(array $options = array())
187
    {
188
        $this->errors = new MessageBag();
189
        if (is_null($this->library_id)) {
0 ignored issues
show
introduced by
The condition is_null($this->library_id) is always false.
Loading history...
190
            // Set library id
191
            $this->library_id = \Auth::user()->id;
192
        }
193
194
        parent::save($options);
195
        return true;
196
    }
197
198
    public function lost()
199
    {
200
        if ($this->item->barcode) {
201
            $this->item->lost();
202
        }
203
204
        $this->is_lost = true;
205
        $this->save();
206
        $this->delete();
207
    }
208
209
    public function found()
210
    {
211
        $this->restore();
212
        $this->is_lost = false;
213
        $this->save();
214
215
        if ($this->item->is_lost) {
216
            $this->item->found();
217
        }
218
    }
219
220
    /**
221
     * Check in the item.
222
     *
223
     * @return null
224
     */
225
    public function checkIn()
226
    {
227
        $this->delete();
228
229
        if ($this->item->thing_id == 1) {
230
            $this->item->delete();
231
            \Log::info(sprintf(
232
                'Slettet midlertidig eksemplar for Alma-utlån (<a href="%s">Detaljer</a>)',
233
                action('ItemsController@show', $this->item_id)
234
            ), ['library' => $this->library->name]);
235
        }
236
    }
237
}
238