Am i refactoring the right way ?

Posted 1 year ago by francoboy7

Hey guys,

Just wanted to know if I'm doing my "refactoring" in a good way or I am under thinking this, or overthinking this.

What do you think of this, is it secure/safe ? Is there a way Laravel already check this logic and I'm just duplicating for nothing ?

Thanks a lot !

OLD CODE (in my [email protected])

public function destroy ($id)
{

        $quote = Quote::find($id);
        if ($quote->user_id == auth()->user()->id) 
        {

            $quote->delete();
            session()->flash('message-style', 'success');
            session()->flash('message', 'Citation supprimée !');

            return redirect ('/');
        }
        else {

            session()->flash('message-style', 'danger');
            session()->flash('message', 'Vous ne pouvez pas supprimer une citation qui ne vous appartient pas!');
            return redirect ('/');

        }
    }

NEW CODE (in [email protected])

public function destroy ($id)
{

        $quote = new Quote;

        if ($$quote->hisQuote($id))
        {
        
            return redirect ('/');

        } else {
        
            return redirect ('/');

        }
    }

QUOTE.PHP MODEL

public function hisQuote ($id)
{
    $quote = $this->find($id);
    // check if user_id of Quote matched Authenticated user id
    if ($quote->user_id === auth()->user()->id) 
    {
        $quote->delete();
        session()->flash('message-style', 'success');
        session()->flash('message', 'Citation supprimée !');
        return true;

    } 
    else 
    {
        session()->flash('message-style', 'danger');
        session()->flash('message', 'Vous ne pouvez pas supprimer une citation qui ne vous appartient pas!');
        return false;
    }

    
}

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