<commit_before><?php
/**
 * @author <NAME> (http://stefanotorresi.it)
 * @license See the file LICENSE.txt for copying permission.
 * ************************************************
 */

namespace Thorr\OAuth\GrantType\ThirdParty\Provider;

use Thorr\OAuth\Entity\User;
use Zend\Http\Client;
use Zend\Http\Response;
use Zend\Json\Json;

class FacebookProvider implements
    ProviderInterface
{
    use ProviderTrait;

    /**
     * @var string
     */
    protected $uri;
    /**
     * @var string
     */
    protected $appId;

    /**
     * @var array
     */
    protected $clientOptions;

    /**
     * @param $options
     * @throws Exception\InvalidArgumentException
     */
    public function __construct($options)
    {
        if (! isset($options['app_id'])) {
            throw new Exception\InvalidArgumentException('Missing "app_id" option');
        }

        $this->setAppId($options['app_id']);

        if (! isset($options['uri'])) {
            throw new Exception\InvalidArgumentException('Missing "uri" option');
        }

        $this->setUri($options['uri']);

        if (! isset($options['user_factory'])) {
            $options['user_factory'] = function ($data) {
                return new User($data->id);
            };
        }

        $this->setUserFactory($options['user_factory']);

        if (isset($options['client_options'])) {
            $this->clientOptions = $options['client_options'];
        }
    }

    /**
     * @return string
     */
    public function getUri()
    {
        return $this->uri;
    }

    /**
     * @param string $uri
     */
    public function setUri($uri)
    {
        $this->uri = rtrim($uri, '/');
    }

    /**
     * @return string
     */
    public function getAppId()
    {
        return $this->appId;
    }

    /**
     * @param string $appId
     */
    public function setAppId($appId)
    {
        $this->appId = $appId;
    }

    /**
     * @return string
     */
    public function getIdentifier()
    {
        return 'facebook';
    }

    /**
     * @param $userId
     * @param $accessToken
     * @param null $errorMessage
     * @throws Exception\ClientException
     * @return bool
     */
    public function validate($userId, $accessToken, &$errorMessage = null)
    {
        $client = new Client($this->uri . '/me', $this->clientOptions);
        $client->setMethod('POST');
        $client->setParameterPost(['access_token' => $accessToken]);

        $userData = $this->decodeBody($client->send());

        if (isset($userData->error)) {
            throw new Exception\ClientException($userData->error->message, 400);
        }

        if (! isset($userData->id)) {
            throw new Exception\ClientException("Invalid data returned by provider", 400);
        }

        if ($userData->id !== $userId) {
            $errorMessage = 'user_id mismatch';
            return false;
        }

        $this->userData = $userData;<commit_msg>moar fix facebook provider api call<commit_after><?php
/**
 * @author <NAME> (http://stefanotorresi.it)
 * @license See the file LICENSE.txt for copying permission.
 * ************************************************
 */

namespace Thorr\OAuth\GrantType\ThirdParty\Provider;

use Thorr\OAuth\Entity\User;
use Zend\Http\Client;
use Zend\Http\Response;
use Zend\Json\Json;

class FacebookProvider implements
    ProviderInterface
{
    use ProviderTrait;

    /**
     * @var string
     */
    protected $uri;
    /**
     * @var string
     */
    protected $appId;

    /**
     * @var array
     */
    protected $clientOptions;

    /**
     * @param $options
     * @throws Exception\InvalidArgumentException
     */
    public function __construct($options)
    {
        if (! isset($options['app_id'])) {
            throw new Exception\InvalidArgumentException('Missing "app_id" option');
        }

        $this->setAppId($options['app_id']);

        if (! isset($options['uri'])) {
            throw new Exception\InvalidArgumentException('Missing "uri" option');
        }

        $this->setUri($options['uri']);

        if (! isset($options['user_factory'])) {
            $options['user_factory'] = function ($data) {
                return new User($data->id);
            };
        }

        $this->setUserFactory($options['user_factory']);

        if (isset($options['client_options'])) {
            $this->clientOptions = $options['client_options'];
        }
    }

    /**
     * @return string
     */
    public function getUri()
    {
        return $this->uri;
    }

    /**
     * @param string $uri
     */
    public function setUri($uri)
    {
        $this->uri = rtrim($uri, '/');
    }

    /**
     * @return string
     */
    public function getAppId()
    {
        return $this->appId;
    }

    /**
     * @param string $appId
     */
    public function setAppId($appId)
    {
        $this->appId = $appId;
    }

    /**
     * @return string
     */
    public function getIdentifier()
    {
        return 'facebook';
    }

    /**
     * @param $userId
     * @param $accessToken
     * @param null $errorMessage
     * @throws Exception\ClientException
     * @return bool
     */
    public function validate($userId, $accessToken, &$errorMessage = null)
    {
        $client = new Client($this->uri . '/me', $this->clientOptions);
        $client->setMethod('GET');
        $client->setParameterGet(['access_token' => $accessToken]);

        $userData = $this->decodeBody($client->send());

        if (isset($userData->error)) {
            throw new Exception\ClientException($userData->error->message, 400);
        }

        if (! isset($userData->id)) {
            throw new Exception\ClientException("Invalid data returned by provider", 400);
        }

        if ($userData->id !== $userId) {
            $errorMessage = 'user_id mismatch';
            return false;
        }

        $this->userData = $userData;
{'fraction_non_alphanumeric': 0.16352624495289367, 'fraction_numerical': 0.002018842530282638, 'mean_word_length': 2.1028183716075155, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1082823', 'n_tokens_mistral': 1740, 'n_tokens_neox': 1667, 'n_words': 336}