Your IP : 216.73.217.79


Current Path : /var/www/v3.cesa.co.za/src/UserBundle/Services/
Upload File :
Current File : /var/www/v3.cesa.co.za/src/UserBundle/Services/OauthManager.php

<?php

namespace App\UserBundle\Services;

use Symfony\Component\DependencyInjection\ContainerInterface;
//use Buzz\Browser;
//use Buzz\Client\FileGetContents;
//use Buzz\Message\Form\FormRequest;
//use Buzz\Message\Response;
use GuzzleHttp\Client;
use Monolog\Logger;

/**
 * Oauth manager
 *
 * @author Otto Saayman <otto@igotafrica.com>
 * @package UserBundle
 * @subpackage Oauth
 * @version 0.0.1
 */
final class OauthManager {

    /**
     * Service Container
     * @var object
     */
    private $container = null;

    /**
     * Class construct
     *
     * @param  ContainerInterface $container
     * @return void
     */
    public function __construct(
    ContainerInterface $container) {
        $this->setContainer($container);

        return;
    }

    /**
     * Get Container
     *
     * @return ContainerInterface $container
     */
    public function getContainer() {
        return $this->container;
    }

    /**
     * Set Container
     *
     * @param  ContainerInterface $container
     * @return void
     */
    public function setContainer($container) {
        $this->container = $container;
    }

    /**
     * Oauth2 login and response
     *
     * @param  array $config
     * @return array
     */
    public function getAccessToken(&$config) {

        if (!isset($config['host']) || !isset($config['client_id']) || !isset($config['client_secret'])) {
            return;
        }

        $browser = new Client();

        try {
            $response = $browser->get(
                    $config['host'] . '/oauth/v2/token?client_id=' . $config['client_id'] . '&grant_type=client_credentials&client_secret=' . $config['client_secret']
            );
        } catch (\GuzzleHttp\Exception\RequestException $ex) {
            echo $ex->getResponse()->getBody() . "\r\n Error in Token get \r\n";
            $config['access_token'] = '';
            return;
        }

        $responseArr = json_decode($response->getBody(), true);
        if (!is_null($responseArr) && isset($responseArr['access_token'])) {
            $config['access_token'] = $responseArr['access_token'];
        }
    }

    /**
     * Oauth2 login and response
     *
     * @param  array $config
     * @return array
     */
    public function loginResponse(&$config) {

        $response = array('success' => 1, 'message' => '');

        if (!isset($config['access_token'])) {
            $this->getAccessToken($config);
        }

        if (!isset($config['access_token']) || $config['uri']) {
            $response['success'] = 0;
            $response['message'] = 'Unable to get token';
        }

        $browser = new Client();

        try {

            if (isset($config['post_vars'])) {
//            echo "Posting to " . $config['uri'] . '?access_token=' . $config['access_token'] . "\r\n";
                $responseBrowser = $browser->request(
                        'POST', $config['host'] . $config['uri'] . '?access_token=' . $config['access_token'], array(
                    'form_params' => array('data' => json_encode($config['post_vars']))
                        )
                );
            } else {
                $responseBrowser = $browser->get(
                        $config['host'] . $config['uri'] . '?access_token=' . $config['access_token']
                );
            }

            $response = json_decode($responseBrowser->getBody(), true);

            if (is_null($response)) {
                echo $responseBrowser->getBody() . "\r\n Error in JSON \r\n";
            }
        } catch (\GuzzleHttp\Exception\RequestException $ex) {
            echo $ex->getResponse()->getBody() . "\r\n Error in Response \r\n";
        }
        return $response;
    }

}