Sending email notifications, each to a different set of subscribers Here is a code which sends email notification on new comments.

&lt;?php


namespace Rib\Src\Apps\ContentNotification\ContentNotificationControllers;


use Rib\Src\Apps\ContentNotification\Email\NewContentNotificationEmail;
use Rib\Src\Services\DbSql;
use Rib\Src\Services\Email;

class JobSendNotificationsCron
{
    public function sendEmails()
    {
        $db = ( new DbSql() )-&gt;db()-&gt;getConnection();

        # Collect jobs to process
        $jobsToProcess = $db-&gt;table( 'content_notifications_jobs' )
                            -&gt;where( 'status', '=', 'active' )
                            -&gt;orderBy( 'last_mailing_time', 'asc' )
                            -&gt;limit( 5 )
                            -&gt;get()-&gt;toArray() ?? null;

        # Treat each job indivicually....
        foreach ( $jobsToProcess as $job ) {

            # Get all users that subscribed to be notified about new content in a particular job
            $usersToNotify = $db-&gt;table( 'content_notifications_subscribers' )
                                -&gt;where( 'content_id', '=', $job-&gt;content_id )
                                -&gt;get()-&gt;toArray() ?? null;

            # ... and send notification to each user subscribed to the job
            foreach ( $usersToNotify as $user ) {


                # Send email confirmation email
                Email::getMailer()-&gt;send( NewContentNotificationEmail::setContent( $job, $user ) );
            }


            # Update job table. Set job to inactive so it is not treated if no new answer was posted.
            # And set last_mailing_time to also skip jobs that had a mailing before delay was expired.
            $db-&gt;table( 'content_notifications_jobs' )-&gt;update(
                [
                    'status'            =&gt; 'inactive',
                    'last_mailing_time' =&gt; time()
                ]
            );
        }
    }
}


I don't like this code because there are sql calls in loop.
I'm thinking of refactoring this code. What would be the best way to refactor it by moving the sql out of the loop ?
Is there any reason for this to be a concrete class/method?



You only seem to be considering happy path.

What happens in DB connection failure occurs?  Have you considered passing the DB connection/object as a dependency to this class or method such that this class/method is guaranteed it gets a connection in a proper state?



Have you considered using a join to get notification/subscriber information in a single query and not have to query in loops?




Email::getMailer()-&gt;send(...)



Do you really need to instantiate mailer here every time? Should mailer be a passed dependency?



Do you not need a unique key for performing your update? Right now it looks like you are updating every record on the table.



Do you really want to be updating unix timestamps into the table as opposed to using appropriate MySQL datetime or timestamp fields?