You are here

class DrupalClient in Facebook Instant Articles 3.x

Same name and namespace in other branches
  1. 8.2 src/DrupalClient.php \Drupal\fb_instant_articles\DrupalClient

Encapsulate Drupal-specific logic for FBIA Client.

Hierarchy

Expanded class hierarchy of DrupalClient

1 file declares its use of DrupalClient
DrupalClientTest.php in tests/src/Unit/DrupalClientTest.php
1 string reference to 'DrupalClient'
fb_instant_articles.services.yml in ./fb_instant_articles.services.yml
fb_instant_articles.services.yml
1 service uses DrupalClient
fb_instant_articles.drupal_client in ./fb_instant_articles.services.yml
Drupal\fb_instant_articles\DrupalClient

File

src/DrupalClient.php, line 18

Namespace

Drupal\fb_instant_articles
View source
class DrupalClient extends FbiaClient {
  use StringTranslationTrait;

  /**
   * Facebook Graph API Permision Error Code.
   */
  const FB_INSTANT_ARTICLES_ERROR_CODE_PERMISSION = 200;

  /**
   * Facebook Graph API Page Not Approved Error Code.
   */
  const FB_INSTANT_ARTICLES_ERROR_CODE_PAGE_NOT_APPROVED = 1888205;

  /**
   * Serializer service.
   *
   * @var \Symfony\Component\Serializer\Normalizer\NormalizerInterface
   */
  protected $serializer;

  /**
   * Logger channel.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $logger;

  /**
   * Instant article content entity normalizer.
   *
   * @var \Drupal\fb_instant_articles\Normalizer\InstantArticleContentEntityNormalizer
   */
  protected $iaNormalizer;

  /**
   * Set the serializer.
   *
   * @param \Symfony\Component\Serializer\Normalizer\NormalizerInterface $serializer
   *   Serializer service. Note that we are type hiting to the
   *   NormalizerInterface, b/c that is the functionality we actually want to
   *   use from the Serializer.
   */
  public function setSerializer(NormalizerInterface $serializer) {
    $this->serializer = $serializer;
  }

  /**
   * Set the logger.
   *
   * @param \Drupal\Core\Logger\LoggerChannelInterface $logger
   *   Logger channel.
   */
  public function setLogger(LoggerChannelInterface $logger) {
    $this->logger = $logger;
  }

  /**
   * Set the instant article content entity normalizer.
   *
   * @param \Drupal\fb_instant_articles\Normalizer\InstantArticleContentEntityNormalizer $ia_normalizer
   *   Instant article normalizer object.
   */
  public function setIaNormalizer(InstantArticleContentEntityNormalizer $ia_normalizer) {
    $this->iaNormalizer = $ia_normalizer;
  }

  /**
   * {@inheritdoc}
   *
   * Additionally try to catch an attempted import call that failed from an
   * authorization exception. In such a case, try again as unpublished if the
   * situation allows.
   */
  public function importArticle($article, $published = FALSE, $forceRescrape = FALSE, $formatOutput = FALSE) {
    try {
      parent::importArticle($article, $published, $forceRescrape, $formatOutput);
    } catch (FacebookResponseException $e) {

      // If this was an authorization exception and the error code indicates
      // that the page has not yet passed review, try posting the article
      // unpublished. Only try again if the article was intended to be
      // published, so we don't try to post unpublished twice.
      if ($e
        ->getCode() === self::FB_INSTANT_ARTICLES_ERROR_CODE_PERMISSION && $e
        ->getSubErrorCode() === self::FB_INSTANT_ARTICLES_ERROR_CODE_PAGE_NOT_APPROVED && $published) {
        parent::importArticle($article, FALSE);
      }
    }
  }

  /**
   * Import a content entity into Instant Articles.
   *
   * Runs the given entity through the serialization process and finally calls
   * importArticle() to send it to Instant Articles.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   Entity to import into Instant Articles.
   */
  public function importEntity(ContentEntityInterface $entity) {

    // Ensure that we have the services we need.
    if (!isset($this->serializer)) {
      throw new \LogicException($this
        ->t('Error importing entity to Facebook Instant Articles. Serializer is not defined.'));
    }

    /** @var \Facebook\InstantArticles\Elements\InstantArticle $article */
    $article = $this->serializer
      ->normalize($entity, 'fbia');

    // Default published status to TRUE for entities that don't implement
    // EntityPublishedInterface. For those that do implement
    // EntityPublishedInterface, go by the published status of the entity. Note
    // that the Development Mode setting under API settings will override this,
    // so ensure that it is turned off in production.
    $published = TRUE;
    if ($entity instanceof EntityPublishedInterface) {
      $published = $entity
        ->isPublished();
    }
    $this
      ->importArticle($article, $published);
    $this->logger
      ->info($this
      ->t('Imported %label (@entity_id) into Facebook Instant Articles.', [
      '%label' => $entity
        ->label(),
      '@entity_id' => $entity
        ->id(),
    ]));
  }

  /**
   * Remove a content entity from Instant Articles.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   Entity to import into Instant Articles.
   *
   * @return \Facebook\InstantArticles\Client\InstantArticleStatus
   *   Status of the response.
   */
  public function removeEntity(ContentEntityInterface $entity) {
    $status = $this
      ->removeArticle($this->iaNormalizer
      ->entityCanonicalUrl($entity));
    if ($status
      ->getStatus() === InstantArticleStatus::SUCCESS) {
      $this->logger
        ->info($this
        ->t('Removed %label (@entity_id) from Facebook Instant Articles.', [
        '%label' => $entity
          ->label(),
        '@entity_id' => $entity
          ->id(),
      ]));
    }
    return $status;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalClient::$iaNormalizer protected property Instant article content entity normalizer.
DrupalClient::$logger protected property Logger channel.
DrupalClient::$serializer protected property Serializer service.
DrupalClient::FB_INSTANT_ARTICLES_ERROR_CODE_PAGE_NOT_APPROVED constant Facebook Graph API Page Not Approved Error Code.
DrupalClient::FB_INSTANT_ARTICLES_ERROR_CODE_PERMISSION constant Facebook Graph API Permision Error Code.
DrupalClient::importArticle public function Additionally try to catch an attempted import call that failed from an authorization exception. In such a case, try again as unpublished if the situation allows.
DrupalClient::importEntity public function Import a content entity into Instant Articles.
DrupalClient::removeEntity public function Remove a content entity from Instant Articles.
DrupalClient::setIaNormalizer public function Set the instant article content entity normalizer.
DrupalClient::setLogger public function Set the logger.
DrupalClient::setSerializer public function Set the serializer.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.