You are here

function commerce_square_requirements in Commerce Square Connect 8

Same name and namespace in other branches
  1. 7 commerce_square.install \commerce_square_requirements()

Implements hook_requirements().

File

./commerce_square.install, line 20
Contains install and update functions for Commerce Square.

Code

function commerce_square_requirements($phase) {
  $requirements = [];
  if ($phase == 'install') {
    if (!class_exists('\\SquareConnect\\ApiClient')) {
      $requirements['commerce_square_library'] = [
        'description' => t('Commerce Square requires the square/connect library.'),
        'severity' => REQUIREMENT_ERROR,
      ];
    }
  }
  elseif ($phase === 'update') {
    $state = \Drupal::state();
    if ($state
      ->get('commerce_square.production_refresh_token') === NULL) {
      $request = \Drupal::request();
      $try_again_url = Url::fromUri($request
        ->getUriForPath(''))
        ->setOptions([
        'query' => [
          'continue' => 1,
        ],
      ])
        ->toString(TRUE)
        ->getGeneratedUrl();
      $requirements['commerce_square_authentication'] = [
        'title' => t('Commerce Square Connect'),
        'description' => t('<p>In the <a href="https://connect.squareup.com/apps">Square Developer Portal</a> make sure your application is on Connect API version 2019-03-13 or later.</p><p>Once you have updated your application, you may <a href=":url">click here to continue.</a></p>', [
          ':url' => $try_again_url,
        ]),
        'severity' => REQUIREMENT_WARNING,
      ];
    }
  }
  elseif ($phase === 'runtime') {
    $connect = \Drupal::getContainer()
      ->get('commerce_square.connect');
    $client = $connect
      ->getClient('production');
    $locations_api = new LocationsApi($client);
    try {
      $locations_api
        ->listLocations();
      $requirements['commerce_square_authentication'] = [
        'title' => t('Commerce Square Connect'),
        'description' => t('Square Connect OAuth is connected.'),
        'severity' => REQUIREMENT_OK,
      ];
    } catch (ApiException $e) {
      $requirements['commerce_square_authentication'] = [
        'title' => t('Commerce Square Connect'),
        'description' => t('Square Connect OAuth access token is not valid. Please reconnect by submitting the <a href=":url">Square settings form</a>.', [
          ':url' => Url::fromRoute('commerce_square.settings'),
        ]),
        'severity' => REQUIREMENT_ERROR,
      ];
    }
  }
  return $requirements;
}