1
\$\begingroup\$

I use the Laravel API and want make messy code cleaner.

I have a form for registering business and businessOwner. And stylist who is working under this business registering from same function.

And which case best to use there? if statements or Switch case statements?

public function businessRegister (Request $request) { 

        if ($request['stylist'] === "stylist") {
            $validator = Validator::make($request->all(), [
                'firstName' => 'required|string',
                'lastName' => 'required|string',
                'gender' => 'required|in:Male,Female',
                'email' => 'required|string|email|max:255|unique:stylists',
                'password' => 'required|string|min:6|confirmed',
                'mobileNumber' => 'required|integer',
                'address' => 'required|string',
                'name' => 'required|string',
                'registrationNumber' => 'required|unique:businesses',
                'bMobileNumber' => 'required|integer',
                'bAddress' => 'required',

            ]);
        } elseif ($request['business'] === "business") {
            $validator = Validator::make($request->all(), [
                'firstName' => 'required|string',
                'lastName' => 'required|string',
                'gender' => 'required|in:Male,Female',
                'email' => 'required|string|email|max:255|unique:stylists',
                'password' => 'required|string|min:6|confirmed',
                'mobileNumber' => 'required|integer',
                'address' => 'required|string',
                'name' => 'required|string',
                'registrationNumber' => 'required|unique:businesses',
                'bMobileNumber' => 'required|integer',
                'bAddress' => 'required',

            ]);
        }

        if ($validator->fails()) {
            return response(['errors' => $validator->errors()->all()], 422);
        }

        if ($request['stylist'] === "stylist") {
            $findBusiness = Business::where([['name',$request->name], ['inviteCode',$request->inviteCode]])->first();
        } elseif ($request['business'] === "business") {
            $inviteCode = $this->inviteCodeGen();
            //print_r($inviteCode);
            $findBusiness = $this->business($request->name, $request->registrationNumber, $request->bImage, $request->bAddress, $request->bLongitude, $request->bLatitude, $request->bMobileNumber, 0, $inviteCode);
        }

        if (count($findBusiness) === 1 || $findBusiness['success'] === 1) {
            $request['device'] = "deviceToken|Or|DeviceTypeLike|IOS|OR|Android|IDK";
            $request['longitude'] = "getStylistLongitude";
            $request['latitude'] = "getStylistLatitude";

            if ($request['stylist'] === "stylist") {
                $id = $findBusiness->id;
            } elseif ($request['business'] === "business") {
                $id = $findBusiness['id'];
            }

            $stylist = Stylist::create([
                'firstName' => $request['firstName'],
                'lastName' => $request['lastName'],
                'image' => $request['image'],
                'stylistStatus' => "Stylist", // Freelancer - Stylist
                'businessId' => $id, // Last inserted Business ID 
                'gender' => $request['gender'],
                'email' => $request['email'],
                'password' => Hash::make($request['password']),
                'mobileNumber' => $request['mobileNumber'],
                'address' => $request['address'],
                'longitude' => $request['longitude'],
                'latitude' => $request['latitude'],
                'device' => $request['device'],
            ]);

            BookingTimes::insert(array(
                array('stylistId' => $stylist->id, 'day' => 'Monday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
                array('stylistId' => $stylist->id, 'day' => 'Tuesday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
                array('stylistId' => $stylist->id, 'day' => 'Wednesday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
                array('stylistId' => $stylist->id, 'day' => 'Thursday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
                array('stylistId' => $stylist->id, 'day' => 'Friday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
                array('stylistId' => $stylist->id, 'day' => 'Saturday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
                array('stylistId' => $stylist->id, 'day' => 'Sunday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
            ));

            if ($request['business']) {
                Business::where('id', $findBusiness['id'])->update(['ownerId' => $stylist->id]);
            }

            $token = $stylist->createToken('Stylist registered')->accessToken;
            $response = ['token' => $token];

            return response($response, 200);
        }

        $response = ['error' => 'Registration error'];
        return response($response, 422);
    }

    private function inviteCodeGen($length = 3) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }

        $randomString2 = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString2 .= $characters[rand(0, $charactersLength - 1)];
        }

        return strtoupper($randomString . "-" . $randomString2);
    }

    protected function business ($name, $registrationNumber, $image, $address, $longitude, $latitude, $mobileNumber, $ownerId, $inviteCode) {
        $business = Business::create([
            'name' => $name,
            'inviteCode' => $inviteCode,
            'registrationNumber' => $registrationNumber,
            'image' => $image,
            'address' => $address,
            'longitude' => $longitude,
            'latitude' => $latitude,
            'mobileNumber' => $mobileNumber,
        ]);

        BusinessBookingTimes::insert(array(
            array('businessId' => $business->id, 'day' => 'Monday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
            array('businessId' => $business->id, 'day' => 'Tuesday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
            array('businessId' => $business->id, 'day' => 'Wednesday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
            array('businessId' => $business->id, 'day' => 'Thursday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
            array('businessId' => $business->id, 'day' => 'Friday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
            array('businessId' => $business->id, 'day' => 'Saturday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
            array('businessId' => $business->id, 'day' => 'Sunday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
        ));

        return ['id' => $business->id, 'success' => 1];
    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.