Google People PHP best practise to get OOP I need to log in users to web app in Javascript and PHP. So I have Javascript interface which send me accessToken to PHP which stores users data to DB and log user in. But the result code does not look like OOP but like a peace of garbage. May be I dont know how to write that code, so the question is if this peace of PHP code is ok or if it is possible to rewrite it to real OOP. Cause OOP exists because of people who use the code. It should make sense for humans. It seems this make sense only for authors of the code.
$code = $_POST['code'];


try
{
    $client = new Google_Client();
    $client-&gt;setAuthConfig(__DIR__ . '/../model/JsonData/GoogleCredentials.json');
    $client-&gt;setRedirectUri( self::REDIRECT_URI );

    $accessToken = $client-&gt;fetchAccessTokenWithAuthCode( $code );

    $client-&gt;setAccessToken( $accessToken );

    $peopleService = new \Google_Service_PeopleService( $client );
    $me = $peopleService-&gt;people-&gt;get('people/me', ['personFields' =&gt; 'emailAddresses,names,metadata,coverPhotos']);  // This is extra feature which does not accept spaces....

}
catch ( \Exception $e )
{
    Debugger::log( $e );
    $this-&gt;sendJson( array( 'errCode' =&gt; 1, 'error' =&gt; 'Log in fails.' ) );
}

/** @var \Google_Service_PeopleService_EmailAddress $email */
foreach ( $me-&gt;getEmailAddresses() as $email )
{
    if ( $email-&gt;getMetadata()-&gt;getPrimary() ) $user_email = $email-&gt;value;  // Oh really? I want to log in user....
}
/** @var \Google_Service_PeopleService_Name $name */
foreach ( $me-&gt;getNames() as $name )
{
    if ( $email-&gt;getMetadata()-&gt;getPrimary() ) $user_name = $name-&gt;displayName;
}

if ( ! isset( $user_email ) ) $this-&gt;sendJson( array( 'errCode' =&gt; 0, 'error' =&gt; 'Email is required.' ) );
if ( ! isset( $user_name ) ) $this-&gt;sendJson( array( 'errCode' =&gt; 0, 'error' =&gt; 'Name is required' ) );

// Hello ID is missing. How to uniquely identify user?

May be I am wrong but is it possible to write it better way to get relevant data for log in? Where is ID which can uniquely identify user?
Look at this JS interface. This looks like OOP https://developers.google.com/identity/sign-in/web/people
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);
     }

   }

}