public function rentLocker(User $user, Locker $locker) { if ($user->getIsPenalized()) { throw new PenalizedUserException(); } if ($user->getFaculty()->getIsEnabled() === false) { throw new PenalizedFacultyException(); } if ($locker->getStatus() != Locker::AVAILABLE) { throw new BusyLockerException(); } if ($user->getLocker()) { throw new TooManyLockersRentedException(); } /** @var Rental $rental */ $rental = $this->rentalRepository->createNew(); $rental->setUser($user); $rental->setLocker($locker); $rental->setEndAt(new \DateTime($this->days_length_rental . ' days')); $locker->setStatus(Locker::RENTED); $locker->setOwner($user); $this->manager->persist($locker); $this->manager->persist($rental); $this->manager->flush(); $event = new RentalEvent($rental); $this->dispatcher->dispatch(RentalEvents::LOCKER_RENTED, $event); return $rental; }
public function it_cannot_rent_busy_locker(User $user, Locker $locker) { $locker->getStatus()->willReturn(Locker::RENTED); $this->shouldThrow(BusyLockerException::class)->duringRentLocker($user, $locker); }