code review with if, switch, foreach

Posted 7 months ago by michaelnguyen547

I have this code, it works but it is messy. Any advices to improve it?

foreach ($request->platform as $platform) {
    switch ($platform) {
        case \App\Enums\Platform::SMS:

            // customized drivers
            if ($request->recipients) {
                $recipients = \App\Provider::find(explode(',', $request->recipients));
                foreach ($recipients as $recipient) {
                    ProcessSMS::dispatch($recipient, $message);
                }
            } else {
                foreach ($request->driverStatus as $driverStatus) {
                    \App\Provider::where('type', $driverStatus)->chunk(100, function ($recipients) use ($message) {
                        foreach ($recipients as $recipient) {
                            ProcessSMS::dispatch($recipient, $message);
                        }
                    });
                }
            }

            break;

        case \App\Enums\Platform::PUSH:

            if ($request->recipients) {
                $recipients = \App\Provider::find(explode(',', $request->recipients));
                foreach ($recipients as $recipient) {
                    ProcessPushNotification::dispatch($recipient, $message);
                }
            } else {
                foreach ($request->driverStatus as $driverStatus) {
                    \App\Provider::where('type', $driverStatus)->chunk(100, function ($recipients) use ($message) {
                        foreach ($recipients as $recipient) {
                            ProcessPushNotification::dispatch($recipient, $message);
                        }
                    });
                }
            }
            break;
    }
}

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.