| Total Complexity | 6 |
| Total Lines | 58 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
| 1 | <?php |
||
| 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) |
||
| 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.'; |
||
| 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() |
||
| 68 | } |
||
| 69 | } |
||
| 70 |