You are here

final class RefreshInstagramTokenAction in Instagram Feeds 8

Provides an action that can refresh Instagram Token for an entity.

Plugin annotation


@Action(
  id = "entity:refresh_instagram_token",
  action_label = @Translation("Refresh Token"),
  deriver = "Drupal\instagram_feeds\Plugin\Action\Derivative\RefreshInstagramTokenActionDeriver",
)

Hierarchy

Expanded class hierarchy of RefreshInstagramTokenAction

File

src/Plugin/Action/RefreshInstagramTokenAction.php, line 22

Namespace

Drupal\instagram_feeds\Plugin\Action
View source
final class RefreshInstagramTokenAction extends EntityActionBase {

  /**
   * The time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * GuzzleHttp\ClientInterface definition.
   *
   * @var \GuzzleHttp\ClientInterface
   */
  protected $httpClient;

  /**
   * Constructs a RefreshTokenAction object.
   *
   * @param mixed[] $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin ID for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, TimeInterface $time, ClientInterface $http_client) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager);
    $this->time = $time;
    $this->httpClient = $http_client;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'), $container
      ->get('datetime.time'), $container
      ->get('http_client'));
  }

  /**
   * {@inheritdoc}
   */
  public function execute($entity = NULL) {

    /** @var \Drupal\instagram_feeds\Entity\InstagramAccountInterface $entity */
    $entity
      ->refreshToken($this->httpClient, TRUE);
  }

  /**
   * {@inheritdoc}
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {

    /** @var \Drupal\instagram_feeds\Entity\InstagramAccountInterface $object */
    if (!$account) {
      $account = \Drupal::currentUser();
    }

    // Token is invalid or entity is unpublished.
    if (!$object
      ->isPublished() || !$object
      ->tokenIsValid()) {
      $result = AccessResult::forbidden('Unpublished or Instagram token is invalid.')
        ->addCacheableDependency($object);
      return $return_as_object ? $result : $result
        ->isAllowed();
    }
    $primary_access = $account
      ->hasPermission('administer instagram_feeds');
    $role_access = $primary_access || $account
      ->hasPermission('update instagram_account');
    $user_access = $role_access || $account
      ->id() == $object
      ->getOwnerId() && $account
      ->hasPermission('update own instagram_account');

    // User doesn't have access.
    if (!$user_access) {
      $result = AccessResult::forbidden()
        ->cachePerPermissions();
      return $return_as_object ? $result : $result
        ->isAllowed();
    }

    // Token can be refreshed only after 24h. It lives 60 days. Expired token
    // cannot be refreshed anymore. 60 days - 1 day = 59 days = 5097600 sec.
    $age = $object
      ->getTokenExpirationTime() - $this->time
      ->getRequestTime();
    if ($age <= 5097600) {
      $result = AccessResult::allowed()
        ->setCacheMaxAge($age)
        ->cachePerPermissions()
        ->addCacheableDependency($object);
      return $return_as_object ? $result : $result
        ->isAllowed();
    }
    $age -= 5097600;
    $result = AccessResult::forbidden('You should wait at least 24 hours prior previous token refesh')
      ->setCacheMaxAge($age)
      ->cachePerPermissions();
    return $return_as_object ? $result : $result
      ->isAllowed();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ActionBase::executeMultiple public function Executes the plugin for an array of objects. Overrides ActionInterface::executeMultiple 3
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityActionBase::$entityTypeManager protected property The entity type manager.
EntityActionBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
RefreshInstagramTokenAction::$httpClient protected property GuzzleHttp\ClientInterface definition.
RefreshInstagramTokenAction::$time protected property The time service.
RefreshInstagramTokenAction::access public function Checks object access. Overrides ActionInterface::access
RefreshInstagramTokenAction::create public static function Creates an instance of the plugin. Overrides EntityActionBase::create
RefreshInstagramTokenAction::execute public function Executes the plugin. Overrides ExecutableInterface::execute
RefreshInstagramTokenAction::__construct public function Constructs a RefreshTokenAction object. Overrides EntityActionBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.