Google People PHP best practise to get OOP
If I had  to make this OOP, I'd begin by hedging concerns :

a class for user data handling (retrieving the POST and using it)
a service (that you'd dependency inject) to handle the configuration of the Google Service
a GoogleUser class for encapsulating the result of the service and abstracting all those foreach

Then, orchestrate those in some kind of controller (I guess you have already since you use a $this-&gt;sendJson method)
I like custom Exception cascade but that's just me :
try{
  //everything working well
}catch(GoogleLoginException $e){
}catch(RequiredEmailException $e){
}catch(RequiredNameException $e){
}

GoogleSDK is notoriously verbose and looks a lot like enterprise java and is always a pain to make elegant.
In the end you'd have something like :

class LoginController{
   private $google;

   public function __construct(GoogleService $google){
     $this-&gt;google = $google;
   }

   public function oauthCallbackAction($postRequest){
     $code = $postRequest-&gt;get(&quot;code&quot;);
     try{
         $authentifiedClient = $google-&gt;authentifiedClientWithCode($code);
         $googleUser = $authentifiedClient-&gt;getMe();
         return $this-&gt;json([
                              &quot;user_name&quot; =&gt; $googleUser-&gt;getUserName(),
                              &quot;email&quot; =&gt; $googleUser-&gt;getEmail(),
                            ], 200);
     }catch(GoogleAuthException $e){   //thrown by GoogleService
             return $this-&gt;json(&quot;blah&quot;, 500);
     }catch(RequiredEmailException $e){ //thrown by GoogleUser::getEmail
             return $this-&gt;json(&quot;gimme email !&quot;, 400);
     }catch(RequiredNameException $e){  //thrown by GoogleUser::getUserName
             return $this-&gt;json(&quot;gimme name !&quot;, 400);
     }

   }

}