Passed
Push — master ( af710d...2c0b17 )
by Dan Michael O.
08:29
created

CheckinRequest::rules()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 59
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 27
nc 7
nop 0
dl 0
loc 59
rs 8.4444
c 0
b 0
f 0

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;
0 ignored issues
show
Bug introduced by
The type Illuminate\Foundation\Http\FormRequest 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...
11
use Scriptotek\Alma\Client as AlmaClient;
0 ignored issues
show
Bug introduced by
The type Scriptotek\Alma\Client 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...
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();
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...
37
        $alma = app(AlmaClient::class);
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

37
        $alma = /** @scrutinizer ignore-call */ app(AlmaClient::class);
Loading history...
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) && !empty($library->library_code)) {
67
            // Item doesn't exist locally, but perhaps in Alma?
68
            // If the library doesn't have a library code set, it means we should not check Alma.
69
            $almaItem = $alma->items->fromBarcode($barcode);
70
            if (is_null($almaItem)) {
71
                // Bail out, this thing really does not exist!
72
                return [
73
                    'barcode' => [new ThingExists()],
74
                ];
75
            }
76
            // Success, we have located an item in Alma that does not exist locally.
77
            $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...
78
            return [];
79
        }
80
81
        // 3. At this point we have a local item (deleted or not). Let's see if it's on loan, or have been.
82
83
        $lastLoan = $item->loans()->withTrashed()->first();
84
85
        if (!is_null($lastLoan)) {
86
            $this->loan = $lastLoan; // This will be the newest one
87
            return [];
88
        }
89
90
        // 4. The item exists, but has never been loaned out.
91
        return [
92
            'barcode' => [new NeverLoanedOut($item)],
93
        ];
94
    }
95
}
96