CheckinRequest::rules()   B
last analyzed

Complexity

Conditions 9
Paths 8

Size

Total Lines 65
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 9
eloc 30
c 2
b 0
f 0
nc 8
nop 0
dl 0
loc 65
rs 8.0555

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Requests;
4
5
use App\Item;
6
use App\Loan;
7
use App\Rules\LoanExists;
8
use App\Rules\NeverLoanedOut;
9
use App\Rules\ThingExists;
10
use Illuminate\Foundation\Http\FormRequest;
11
use Scriptotek\Alma\Client as AlmaClient;
12
13
class CheckinRequest extends FormRequest
14
{
15
    public $item;
16
    public $user;
17
    public $localUser;
18
19
    /**
20
     * Determine if the user is authorized to make this request.
21
     *
22
     * @return bool
23
     */
24
    public function authorize()
25
    {
26
        return true;
27
    }
28
29
    /**
30
     * Get the validation rules that apply to the request.
31
     *
32
     * @return array
33
     */
34
    public function rules()
35
    {
36
        $library = \Auth::user();
37
        $alma = app(AlmaClient::class);
38
39
        // 1. If we're given a loan id, look for that.
40
41
        if ($this->input('loan')) {
42
            $loan = Loan::with(['item', 'item.thing', 'user'])
43
                ->find($this->input('loan'));
44
            if (is_null($loan)) {
45
                // Bail out
46
                return [
47
                    'loan' => [new LoanExists()],
48
                ];
49
            }
50
            // Success, we have located an active local loan!
51
            $this->loan = $loan;
0 ignored issues
show
Bug Best Practice introduced by
The property loan does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
52
            return [];
53
        }
54
55
        // 2. Otherwise, start by looking up the item by barcode, locally or in Alma.
56
57
        $barcode = $this->input('barcode');
58
59
        if (is_null($barcode)) {
60
            // Bail out, but no need for error, controller will take care of this case.
61
            return [];
62
        }
63
64
        $item = Item::withTrashed()->where('barcode', '=', $barcode)->first();
65
66
        if (is_null($item)) {
67
            // Item doesn't exist locally, but perhaps in Alma?
68
            if (empty($library->library_code) || is_null($alma->key)) {
0 ignored issues
show
Bug introduced by
The property library_code does not exist on App\User. Did you mean library_id?
Loading history...
69
                // Alma integration is not configured. Bail out.
70
                return [
71
                    'barcode' => [new ThingExists()],
72
                ];
73
            }
74
75
            $almaItem = $alma->items->fromBarcode($barcode);
76
            if (is_null($almaItem)) {
77
                // Nope, not in Alma either. Bail out.
78
                return [
79
                    'barcode' => [new ThingExists()],
80
                ];
81
            }
82
            // Success, we have located an item in Alma that does not exist locally.
83
            $this->almaItem = $almaItem;
0 ignored issues
show
Bug Best Practice introduced by
The property almaItem does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
84
            return [];
85
        }
86
87
        // 3. At this point we have a local item (deleted or not). Let's see if it's on loan, or have been.
88
89
        $lastLoan = $item->loans()->withTrashed()->first();
90
91
        if (!is_null($lastLoan)) {
92
            $this->loan = $lastLoan; // This will be the newest one
93
            return [];
94
        }
95
96
        // 4. The item exists, but has never been loaned out.
97
        return [
98
            'barcode' => [new NeverLoanedOut($item)],
99
        ];
100
    }
101
}
102