| Conditions | 9 |
| Paths | 8 |
| Total Lines | 65 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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; |
||
|
|
|||
| 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)) { |
||
| 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; |
||
| 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 | ]; |
||
| 102 |