NotOnLoan::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
3
namespace App\Rules;
4
5
use App\Item;
6
use Illuminate\Contracts\Validation\Rule;
7
use Scriptotek\Alma\Bibs\Item as AlmaItem;
8
use Scriptotek\Alma\Client as AlmaClient;
9
10
class NotOnLoan implements Rule
11
{
12
    protected $alma;
13
    protected $item;
14
    protected $what;
15
16
    /**
17
     * Create a new rule instance.
18
     *
19
     * @param AlmaClient $alma
20
     * @param $item
21
     */
22
    public function __construct(AlmaClient $alma, $item = null)
23
    {
24
        $this->alma = $alma;
25
        $this->item = $item;
26
        $this->msg = 'Tingen er allerede utlånt i Bibrex.';
0 ignored issues
show
Bug Best Practice introduced by
The property msg does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
27
    }
28
29
    /**
30
     * Determine if the validation rule passes.
31
     *
32
     * @param  string  $attribute
33
     * @param  mixed  $value
34
     * @return bool
35
     */
36
    public function passes($attribute, $value)
37
    {
38
        if (is_null($this->item)) {
39
            return true;
40
        }
41
42
        if (is_a($this->item, AlmaItem::class)) {
43
            $almaLoan = $this->item->loan;
44
            $this->msg = 'Dokumentet er allerede utlånt i Alma.';
0 ignored issues
show
Bug Best Practice introduced by
The property msg does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
45
46
            return is_null($almaLoan);
47
        }
48
49
        // Always true if the item is a generic representation without barcode
50
        if (is_null($this->item->barcode)) {
51
            return true;
52
        }
53
54
        // Check if already on loan
55
        $loan = $this->item->loans()->first();
56
57
        return is_null($loan);
58
    }
59
60
    /**
61
     * Get the validation error message.
62
     *
63
     * @return string
64
     */
65
    public function message()
66
    {
67
        return $this->msg;
68
    }
69
}
70