<commit_before><?php
namespace Fortifi\Sdk\OAuth;

use Fortifi\FortifiApi\Auth\Responses\AuthUserDetailsResponse;
use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Token\AccessToken;

class FortifiProvider extends AbstractProvider
{
  protected $_url;
  protected $_userDetails;
  /**
   * @var Client
   */
  protected $_client;

  /**
   * @param $url
   *
   * @return $this
   */
  public function setFortifiUrl($url)
  {
    $this->_url = $url;
    return $this;
  }

  /**
   * Retrieve the Fortifi URL
   *
   * @return string
   */
  public function getFortifiUrl()
  {
    return $this->_url;
  }

  /**
   * @return string
   */
  public function urlAuthorize()
  {
    return build_path_unix($this->_url, 'oauth', 'authorize');
  }

  /**
   * @return string
   */
  public function urlAccessToken()
  {
    return build_path_unix($this->_url, 'oauth', 'access-token');
  }

  /**
   * @param AccessToken $token
   *
   * @return string
   */
  public function urlUserDetails(AccessToken $token)
  {
    return build_path_unix($this->_url, 'auth', 'details')
    . '?access_token=' . $token->accessToken;
  }

  /**
   * @param             $response
   * @param AccessToken $token
   *
   * @return OAuthUser
   */
  public function userDetails($response, AccessToken $token)
  {
    $user = new OAuthUser();
    $result = $response->result;
    /**
     * @var $result AuthUserDetailsResponse
     */
    if(!($result instanceof AuthUserDetailsResponse))
    {
      $result = AuthUserDetailsResponse::make($result);
    }
    $user->setAuthUserDetails($result);
    $user->exchangeArray(
      [
        'uid'         => $result->userFid,
        'nickname'    => nonempty(
          $result->displayName,
          $result->firstName,
          $result->lastName,
          $result->username,
          $result->userFid
        ),
        'locale'      => $result->language,
        'location'    => $result->timezone,
        'firstName'   => $result->firstName,
        'lastName'    => $result->lastName,
        'description' => $result->description,
        'email'       => $result->username
      ]
    );
    return $user;
  }

  public function userUid($response, AccessToken $token)
  {
    return isset($response->id) && $response->id ? $response->id : null;
  }

  public function userEmail($response, AccessToken $token)
  {
    return isset($response->email) && $response->email ? $response->email : null;
  }

  public function userScreenName($response, AccessToken $token)
  {
    return isset($response->name) && $response->name ? $response->name : null;
  }

  protected function fetchUserDetails(AccessToken $token)
  {
    if($this->_userDetails === null)
    {
      $this->setUserDetailsCache(parent::fetchUserDetails($token));
    }
    return $this->_userDetails;
  }

  public function setUserDetailsCache($response)<commit_msg>Support logout<commit_after><?php
namespace Fortifi\Sdk\OAuth;

use Fortifi\FortifiApi\Auth\Responses\AuthUserDetailsResponse;
use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Token\AccessToken;

class FortifiProvider extends AbstractProvider
{
  protected $_url;
  protected $_userDetails;
  /**
   * @var Client
   */
  protected $_client;

  /**
   * @param $url
   *
   * @return $this
   */
  public function setFortifiUrl($url)
  {
    $this->_url = $url;
    return $this;
  }

  /**
   * Retrieve the Fortifi URL
   *
   * @return string
   */
  public function getFortifiUrl()
  {
    return $this->_url;
  }

  /**
   * @return string
   */
  public function urlAuthorize()
  {
    return build_path_unix($this->_url, 'oauth', 'authorize');
  }

  /**
   * @return string
   */
  public function urlAccessToken()
  {
    return build_path_unix($this->_url, 'oauth', 'access-token');
  }

  /**
   * @param AccessToken $token
   *
   * @return string
   */
  public function urlUserDetails(AccessToken $token)
  {
    return build_path_unix($this->_url, 'auth', 'details')
    . '?access_token=' . $token->accessToken;
  }

  /**
   * @param AccessToken $token
   *
   * @return string
   */
  public function urlLogout(AccessToken $token)
  {
    return build_path_unix($this->_url, 'auth', 'logout')
    . '?access_token=' . $token->accessToken;
  }

  /**
   * @param             $response
   * @param AccessToken $token
   *
   * @return OAuthUser
   */
  public function userDetails($response, AccessToken $token)
  {
    $user = new OAuthUser();
    $result = $response->result;
    /**
     * @var $result AuthUserDetailsResponse
     */
    if(!($result instanceof AuthUserDetailsResponse))
    {
      $result = AuthUserDetailsResponse::make($result);
    }
    $user->setAuthUserDetails($result);
    $user->exchangeArray(
      [
        'uid'         => $result->userFid,
        'nickname'    => nonempty(
          $result->displayName,
          $result->firstName,
          $result->lastName,
          $result->username,
          $result->userFid
        ),
        'locale'      => $result->language,
        'location'    => $result->timezone,
        'firstName'   => $result->firstName,
        'lastName'    => $result->lastName,
        'description' => $result->description,
        'email'       => $result->username
      ]
    );
    return $user;
  }

  public function userUid($response, AccessToken $token)
  {
    return isset($response->id) && $response->id ? $response->id : null;
  }

  public function userEmail($response, AccessToken $token)
  {
    return isset($response->email) && $response->email ? $response->email : null;
  }

  public function userScreenName($response, AccessToken $token)
  {
    return isset($response->name) && $response->name ? $response->name : null;
  }

  public function logout(AccessToken $token)
  {
    $this->fetchProviderData($this->urlLogout($token));
    return true;
  }

  protected function fetchUserDetails(AccessToken $token)
  {
    if($this->_userDetails === null)
    {
      $this->setUserDetailsCache(parent::fetchUserDetails($token));
    }
    return $this->_userDetails;
  }

  public function setUserDetailsCache($response)
{'fraction_non_alphanumeric': 0.1453213995117982, 'fraction_numerical': 0.0006509357200976403, 'mean_word_length': 2.745277269957343, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1757542', 'n_tokens_mistral': 1943, 'n_tokens_neox': 1919, 'n_words': 358}