/** * get existing registered business. * * @param User $user User who attempts to register the business * @param string $slug Desired slug for business * * @throws BusinessAlreadyExists When Business exists and is not owned by User * * @return Business|false Business if found or false otherwise */ public function getExisting(User $user, $slug) { $business = Business::withTrashed()->where(['slug' => $slug])->first(); if ($business === null) { return false; } logger()->info("Found existing businessId:{$business->id}"); if (!$user->isOwner($business->id)) { logger()->info("Already taken businessId:{$business->id}"); throw new BusinessAlreadyExists(); } logger()->info("Restoring owned businessId:{$business->id}"); $business->restore(); return $business; }
/** * store Business * * @param BusinessFormRequest $request Business form Request * @return Response Redirect */ public function store(BusinessFormRequest $request) { $this->log->info(__METHOD__); ////////////////// // FOR REFACTOR // ////////////////// // Search Existing $existingBusiness = Business::withTrashed()->where(['slug' => $request->input('slug')])->first(); // If found if ($existingBusiness !== null) { $this->log->info(" Found existing businessId:{$existingBusiness->id}"); // If owned, restore if (auth()->user()->isOwner($existingBusiness)) { $this->log->info(" Restoring owned businessId:{$existingBusiness->id}"); $existingBusiness->restore(); Flash::success(trans('manager.businesses.msg.store.restored_trashed')); return redirect()->route('manager.business.service.create', $existingBusiness); } // If not owned, return message $this->log->info(" Already taken businessId:{$existingBusiness->id}"); Flash::error(trans('manager.businesses.msg.store.business_already_exists')); return redirect()->back()->withInput(request()->all()); } // Register new Business $business = new Business($request->all()); $category = Category::find($request->get('category')); $business->strategy = $category->strategy; $business->category()->associate($category); $business->save(); auth()->user()->businesses()->attach($business); auth()->user()->save(); // Generate local notification $business_name = $business->name; Notifynder::category('user.registeredBusiness')->from('App\\Models\\User', auth()->user()->id)->to('App\\Models\\Business', $business->id)->url('http://localhost')->extra(compact('business_name'))->send(); // Redirect success Flash::success(trans('manager.businesses.msg.store.success')); return redirect()->route('manager.business.service.create', $business); }