Ipa
Ipa

Reputation: 13

PHP: Error handling without throwing an exception

I'm sending multiple emails from a PHP application, and I want to inform the user of the emails that failed to be sent.

What is the most elegant way to do the error handling when

What I want is to get the $notificationSucceeded back from Suggestion::notifyDeletionToAll() to SuggestionController somehow nicely from all notifications.

The depth of the call stack made me doubt if returning it through all the methods is the most elegant way, especially when I already have a return value from Suggestion::cancel().

Is there a better way?

Controller:

class SuggestionController {
    function cancelSuggestion($suggestionId)
    {
        $suggestion = new Suggestion();
        $suggestion->fetch($suggestionId);

        $suggestionDeleted = $suggestion->cancel();

        print json_encode(array(
            'status' => 'ok',
            'suggestionDeleted' => $suggestionDeleted,
        ));
    }
}

Suggestion class:

class Suggestion {

    /**
     * Cancels membership of current user in the suggestion
     */
    public function cancel()
    {
        $this->cancelMembership();

        if (!$this->hasAcceptedMembers()) {
            $this->deleteAndNotify();
            return true;
        }

        return false;
    }

    /**
     * Deletes the suggestion and notifies all the users in it
     */
    private function deleteAndNotify()
    {
        $this->notifyDeletionToAll();
        DB::inst()->query("DELETE FROM suggestions WHERE id = {$this->id}");
    }

    /**
     * Notifies about the deletion of the suggestion to all members (users in the suggestion)
     */
    private function notifyDeletionToAll()
    {
        $result = DB::inst()->query("SELECT user_id FROM suggestions_users
            WHERE suggestion_id = {$this->id}");
        while ($member_id = DB::inst()->fetchFirstField($result)) {
            $member = new User();
            $member->fetch($member_id);
            $notificationSucceeded = $member->notifySuggestionDeleted($this);
        }
    }
}

Upvotes: 0

Views: 62

Answers (1)

Dinesh Babu
Dinesh Babu

Reputation: 456

I can't understand your question clearly. but i hope this will help you

$successfully_sent_arr = array();
$failure_notsent_arr   = array();
if($mail->Send())
{
    $successfully_sent_arr[] = $to_email;
    //Once the last loop occurs, update this array in database
}
else
{
    $failure_notsent_arr[] = $to_email;
    //Once the last loop occurs, update this array in database
}

Upvotes: 1

Related Questions