Move findLicenseSeatToCheckout back to controller. (#5970)

* Move findLicenseSeatToCheckout back to controller.

After discussion, move findLicenseSeatToCheckout method back to
controller from form request.  Also cleanup one tiny bit more with null
coalesce operator (Yay php 7).

* Revert Earlier change.

$target only exists in the checkoutTo* methods.  Need to log the
checkout individually in each of those.
This commit is contained in:
Daniel Meltzer 2018-07-27 15:03:04 -04:00 committed by snipe
parent b58c77c8b8
commit 248fcfa869
2 changed files with 23 additions and 28 deletions

View file

@ -56,24 +56,41 @@ class LicenseCheckoutController extends Controller
public function store(LicenseCheckoutRequest $request, $licenseId, $seatId = null)
{
$license = License::find($licenseId);
if (!$license) {
if (!$license = License::find($licenseId)) {
return redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.not_found'));
}
$this->authorize('checkout', $license);
$licenseSeat = $request->findLicenseSeatToCheckout($license, $seatId);
$licenseSeat = $this->findLicenseSeatToCheckout($license, $seatId);
$licenseSeat->user_id = Auth::id();
$checkoutMethod = 'checkoutTo'.ucwords(request('checkout_to_type'));
$checkoutMethod = 'checkoutTo'.ucwords(request('checkout_to_type'));
if ($this->$checkoutMethod($licenseSeat)) {
return redirect()->route("licenses.index")->with('success', trans('admin/licenses/message.checkout.success'));
}
return redirect()->route("licenses.index")->with('error', trans('Something went wrong handling this checkout.'));
}
protected function findLicenseSeatToCheckout($license, $seatId)
{
$licenseSeat = LicenseSeat::find($seatId) ?? $license->freeSeat();
if (!$licenseSeat) {
if ($seatId) {
return redirect()->route('licenses.index')->with('error', 'This Seat is not available for checkout.');
}
return redirect()->route('licenses.index')->with('error', 'There are no available seats for this license');
}
if(!$licenseSeat->license->is($license)) {
return redirect()->route('licenses.index')->with('error', 'The license seat provided does not match the license.');
}
return $licenseSeat;
}
protected function checkoutToAsset($licenseSeat)
{
if (is_null($target = Asset::find(request('asset_id')))) {
@ -85,8 +102,7 @@ class LicenseCheckoutController extends Controller
if ($target->checkedOutToUser()) {
$licenseSeat->assigned_to = $target->assigned_to;
}
if ($licenseSeat->save()) {
if ($licenseSeat->save()) {
$licenseSeat->logCheckout(request('note'), $target);
return true;
}

View file

@ -29,25 +29,4 @@ class LicenseCheckoutRequest extends FormRequest
'asset_id' => 'required_without:assigned_to',
];
}
public function findLicenseSeatToCheckout($license, $seatId)
{
// This returns null if seatId is null
if (!$licenseSeat = LicenseSeat::find($seatId)) {
$licenseSeat = $license->freeSeat();
}
if (!$licenseSeat) {
if ($seatId) {
return redirect()->route('licenses.index')->with('error', 'This Seat is not available for checkout.');
}
return redirect()->route('licenses.index')->with('error', 'There are no available seats for this license');
}
if(!$licenseSeat->license->is($license)) {
return redirect()->route('licenses.index')->with('error', 'The license seat provided does not match the license.');
}
return $licenseSeat;
}
}