<commit_before><?php
/**
 * Created by PhpStorm.
 *
 * combo-outfit (by <NAME>)
 *
 * User: ducanh-ki
 * Date: 3/6/17
 * Time: 5:17 PM
 */

namespace CreativeDelta\User\Service;


use Zend\Http\Client;

class UserFacebookService
{
    const FACEBOOK_RESPONSE  = "code";
    const FACEBOOK_OAUTH_URL = "https://www.facebook.com/v2.8/dialog/oauth";
    const FACEBOOK_TOKEN_URL = "https://graph.facebook.com/v2.8/oauth/access_token";
    const FACEBOOK_GRAPH_URL = "https://graph.facebook.com/me";
    const FACEBOOK_SCOPE     = "public_profile, email";

    protected $appId;
    protected $appSecret;

    protected $token;
    protected $scope;
    protected $state;
    protected $redirectUri;

    /**
     * UserFacebookService constructor.
     * @param $appId
     * @param $appSecret
     * @param $scope
     * @param $redirectUri
     */
    public function __construct($appId, $appSecret, $scope, $redirectUri)
    {
        $this->appId       = $appId;
        $this->appSecret   = $appSecret;
        $this->scope       = $scope;
        $this->redirectUri = $redirectUri;
    }

    public function generateOAuthUrl($state)
    {
        $config = [
            'response_type' => self::FACEBOOK_RESPONSE,
            'scope'         => $this->scope,
            'client_id'     => $this->appId,
            'redirect_uri'  => $this->redirectUri,
            'state'         => $state
        ];

        $query = http_build_query($config);
        $url   = self::FACEBOOK_OAUTH_URL . '?' . $query;

        return $url;
    }

    public function initializeToken($data)
    {
        $code = $data['code'];

        $config = [
            'client_id'     => $this->appId,
            'client_secret' => $this->appSecret,
            'redirect_uri'  => $this->redirectUri,
            'code'          => $code
        ];

        $httpToken = new Client(self::FACEBOOK_TOKEN_URL);
        $httpToken->setParameterGet($config);

        $response  = $httpToken->send();
        $dataArray = json_decode($response->getBody(), true);

        $this->token = $dataArray['access_token'];
    }

    public function profile($fields)
    {
        $httpGraph = new Client(self::FACEBOOK_GRAPH_URL);
        $httpGraph->setParameterGet([
            'access_token' => $this->token,
            'fields'       => $fields
        ]);

        $response     = $httpGraph->send();
        $profileJson  = $response->getBody();
        $profileArray = json_decode($profileJson, true);

        return $profileArray;
    }
}<commit_msg>Add function comments<commit_after><?php
/**
 * Created by PhpStorm.
 *
 * combo-outfit (by <NAME>)
 *
 * User: ducanh-ki
 * Date: 3/6/17
 * Time: 5:17 PM
 */

namespace CreativeDelta\User\Service;


use Zend\Http\Client;

class UserFacebookService
{
    const FACEBOOK_RESPONSE  = "code";
    const FACEBOOK_OAUTH_URL = "https://www.facebook.com/v2.8/dialog/oauth";
    const FACEBOOK_TOKEN_URL = "https://graph.facebook.com/v2.8/oauth/access_token";
    const FACEBOOK_GRAPH_URL = "https://graph.facebook.com/me";
    const FACEBOOK_SCOPE     = "public_profile, email";

    protected $appId;
    protected $appSecret;

    protected $token;
    protected $scope;
    protected $state;
    protected $redirectUri;

    /**
     * UserFacebookService constructor.
     * @param $appId
     * @param $appSecret
     * @param $scope
     * @param $redirectUri
     */
    public function __construct($appId, $appSecret, $scope, $redirectUri)
    {
        $this->appId       = $appId;
        $this->appSecret   = $appSecret;
        $this->scope       = $scope;
        $this->redirectUri = $redirectUri;
    }

    /**
     * Use this method to receive URL for Facebook authentication, then redirect your page to the URL.
     * Once user has logged-in, the user will then be redirected back to the URL set by $redirectUri.
     * You should have an controller::action for this redirection url. Then in this action, you can
     * retrieve authentication data {code, state} for next uses.
     *
     * @param $state
     * @return string
     */
    public function generateOAuthUrl($state)
    {
        $config = [
            'response_type' => self::FACEBOOK_RESPONSE,
            'scope'         => $this->scope,
            'client_id'     => $this->appId,
            'redirect_uri'  => $this->redirectUri,
            'state'         => $state
        ];

        $query = http_build_query($config);
        $url   = self::FACEBOOK_OAUTH_URL . '?' . $query;

        return $url;
    }

    /**
     * Since service is only set to received [code] in the authentication response,
     * an extra step must be made in order to receive an access_token for your application.
     *
     * Use this method and provide it with the code (received from authentication) to get a token.
     *
     * @param $code
     */
    public function initializeToken($code)
    {
        $config = [
            'client_id'     => $this->appId,
            'client_secret' => $this->appSecret,
            'redirect_uri'  => $this->redirectUri,
            'code'          => $code
        ];

        $httpToken = new Client(self::FACEBOOK_TOKEN_URL);
        $httpToken->setParameterGet($config);

        $response  = $httpToken->send();
        $dataArray = json_decode($response->getBody(), true);

        $this->token = $dataArray['access_token'];
    }

    /**
     * @param string $fields // a comma separated string of profile fields to be retrieved
     * @return mixed
     */
    public function profile($fields)
    {
        $httpGraph = new Client(self::FACEBOOK_GRAPH_URL);
        $httpGraph->setParameterGet([
            'access_token' => $this->token,
            'fields'       => $fields
        ]);

        $response     = $httpGraph->send();
        $profileJson  = $response->getBody();
        $profileArray = json_decode($profileJson, true);

        return $profileArray;
    }
}
{'fraction_non_alphanumeric': 0.13087417887822134, 'fraction_numerical': 0.003705575206333165, 'mean_word_length': 2.199353448275862, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 63, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1348177', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1810, 'n_words': 396}