You are here

class YandexServicesAuthController in Yandex Services Authorization API 8

Controller providing oauth callback for the site authorization on Yandex.

Hierarchy

Expanded class hierarchy of YandexServicesAuthController

File

lib/Drupal/yandex_services_auth/Controller/YandexServicesAuthController.php, line 22
Contains \Drupal\action\Controller\YandexServicesAuthController

Namespace

Drupal\yandex_services_auth\Controller
View source
class YandexServicesAuthController implements ContainerInjectionInterface {

  /**
   * The HTTP client to fetch the feed data with.
   *
   * @var \Guzzle\Http\Client
   */
  protected $httpClient;

  /**
   * Class constructor.
   *
   * @param \Guzzle\Http\Client
   *   The Guzzle HTTP client.
   */
  public function __construct(Client $http_client) {
    $this->httpClient = $http_client;
  }

  /**
   * Implements \Drupal\Core\ControllerInterface::create().
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('http_default_client'));
  }

  /**
   * Registers authorization result.
   */
  public function oauthCallback() {
    $request = Request::createFromGlobals();
    $code = $request
      ->get('code', '');
    if (empty($code)) {
      watchdog('yandex_services_auth', 'The "code" parameter is empty.', array(), WATCHDOG_WARNING);
      drupal_set_message(t('An error has occurred. Please try again.'), 'error');
      return new RedirectResponse(url('admin/config/system/yandex_services_auth'));
    }
    $client_id = \Drupal::state()
      ->get('yandex_services_auth_client_id') ?: '';
    $client_secret = \Drupal::state()
      ->get('yandex_services_auth_client_secret') ?: '';
    $data = 'grant_type=authorization_code&client_id=' . $client_id . '&code=' . $_GET['code'];
    if (!empty($client_secret)) {
      $data .= '&client_secret=' . $client_secret;
    }
    try {
      $response = $this->httpClient
        ->post('https://oauth.yandex.ru/token', NULL, $data)
        ->send();
      $data = $response
        ->getBody(TRUE);
    } catch (BadResponseException $e) {
      $response = $e
        ->getResponse();
      watchdog('yandex_services_auth', 'Failed to retrieve token data due to "%error".', array(
        '%error' => $response
          ->getStatusCode() . ' ' . $response
          ->getReasonPhrase(),
      ), WATCHDOG_WARNING);

      //      watchdog_exception('yandex_services_auth', $e, 'Failed to retrieve token data');
      drupal_set_message(t('An error has occurred. Please try again.'), 'error');
      return new RedirectResponse(url('admin/config/system/yandex_services_auth'));
    } catch (RequestException $e) {
      watchdog('yandex_services_auth', 'Failed to retrieve token data due to "%error".', array(
        '%error' => $e
          ->getMessage(),
      ), WATCHDOG_WARNING);

      //      watchdog_exception('yandex_services_auth', $e, 'Failed to retrieve token data');
      drupal_set_message(t('An error has occurred. Please try again.'), 'error');
      return new RedirectResponse(url('admin/config/system/yandex_services_auth'));
    } catch (\Exception $e) {
      watchdog('yandex_services_auth', 'Failed to retrieve token data due to "%error".', array(
        '%error' => $e
          ->getMessage(),
      ), WATCHDOG_WARNING);

      //      watchdog_exception('yandex_services_auth', $e, 'Failed to retrieve token data');
      drupal_set_message(t('An error has occurred. Please try again.'), 'error');
      return new RedirectResponse(url('admin/config/system/yandex_services_auth'));
    }
    $response = json_decode($data);
    \Drupal::state()
      ->set('yandex_services_auth_token', $response->access_token);
    watchdog('yandex_services_auth', 'Token request is successful.');
    drupal_set_message(t('Congratulations! Your application has been authorized by Yandex.'));
    return new RedirectResponse(url('admin/config/system/yandex_services_auth'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
YandexServicesAuthController::$httpClient protected property The HTTP client to fetch the feed data with.
YandexServicesAuthController::create public static function Implements \Drupal\Core\ControllerInterface::create(). Overrides ContainerInjectionInterface::create
YandexServicesAuthController::oauthCallback public function Registers authorization result.
YandexServicesAuthController::__construct public function Class constructor.