You are here

class FirebaseServiceBase in Firebase Push Notification (FCM) 8

Same name and namespace in other branches
  1. 3.0.x src/Service/FirebaseServiceBase.php \Drupal\firebase\Service\FirebaseServiceBase

Provides a base class for service, working with FCM.

Hierarchy

Expanded class hierarchy of FirebaseServiceBase

File

src/Service/FirebaseServiceBase.php, line 15

Namespace

Drupal\firebase\Service
View source
class FirebaseServiceBase implements FirebaseServiceInterface {
  use DependencySerializationTrait {
    __wakeup as wakeup;
    __sleep as sleep;
  }

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * HTTP client.
   *
   * @var \GuzzleHttp\Client
   */
  protected $client;

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

  /**
   * Firebase API Key.
   *
   * @var string
   */
  protected $key;

  /**
   * Firebase service endpoint.
   *
   * @var string
   */
  protected $endpoint;

  /**
   * Request body.
   *
   * @var array
   */
  protected $body;

  /**
   * Constructs a FirebaseServiceBase object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The factory for configuration objects.
   * @param \GuzzleHttp\ClientInterface $client
   *   An HTTP client.
   * @param \Drupal\Core\Logger\LoggerChannelInterface $loggerChannel
   *   The logger channel.
   */
  public function __construct(ConfigFactoryInterface $configFactory, ClientInterface $client, LoggerChannelInterface $loggerChannel) {
    $this->configFactory = $configFactory;
    $this->client = $client;
    $this->logger = $loggerChannel;
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    return [
      'Content-Type' => 'application/json',
      'Authorization' => 'key=' . $this->key,
    ];
  }

  /**
   * {@inheritdoc}
   *
   * @throws \Exception
   */
  public function send() {

    // Build the header of our request to Firebase.
    // The header is composed by Content-Type and Authorization.
    // The Authorization is our server key, which needs to be provided
    // by the admin interface.
    // @see \Drupal\firebase\Form\ConfigurationForm.
    $headers = $this
      ->buildHeader();

    // The body is composed by an array of data.
    if (!$this->body) {
      throw new \InvalidArgumentException("The body of request shouldn't be blank.");
    }
    try {
      $response = $this->client
        ->post($this->endpoint, [
        'headers' => $headers,
        'body' => Json::encode($this->body),
      ]);
    } catch (\Exception $e) {

      // Error connecting to Firebase API.
      $this->logger
        ->error($e
        ->getMessage());
      throw $e;
    }
    if (isset($response)) {
      $responseBody = Json::decode($response
        ->getBody());
      if ($response
        ->getStatusCode() === 200) {
        if (isset($responseBody['failure']) && $responseBody['failure'] != 0) {

          // Something went wrong. We didn't sent the push notification.
          // Common errors:
          // - Authentication Error
          //   The Server Key is invalid.
          // - Invalid Registration Token
          //   The token (generated by app) is not recognized by Firebase.
          // @see https://firebase.google.com/docs/cloud-messaging/http-server-ref#error-codes
          if (count($responseBody['results']) == 1) {
            $errorMessage = reset($responseBody['results'])['error'];
            $this->logger
              ->error('Failure message: @error', [
              '@error' => $errorMessage,
            ]);
          }
          else {
            $this->logger
              ->error('Some of the recipients will not be able to receive the notification, please check response.');
          }
        }
        return $responseBody;
      }
    }
    return FALSE;
  }

  /**
   * Reset body of service.
   */
  public function resetService() {
    unset($this->body);
  }

  /**
   * {@inheritdoc}
   */
  public function __sleep() {
    $vars = $this
      ->sleep();

    // Do not serialize static cache.
    unset($vars['key'], $vars['endpoint'], $vars['body']);
    return $vars;
  }

  /**
   * {@inheritdoc}
   */
  public function __wakeup() {
    $this
      ->wakeup();

    // Initialize static cache.
    $this->key = '';
    $this->endpoint = '';
    $this->body = [];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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 Aliased as: sleep 1
DependencySerializationTrait::__wakeup public function Aliased as: wakeup 2
FirebaseServiceBase::$body protected property Request body.
FirebaseServiceBase::$client protected property HTTP client.
FirebaseServiceBase::$configFactory protected property The config factory.
FirebaseServiceBase::$endpoint protected property Firebase service endpoint.
FirebaseServiceBase::$key protected property Firebase API Key.
FirebaseServiceBase::$logger protected property The logger channel.
FirebaseServiceBase::buildHeader public function Build the header. Overrides FirebaseServiceInterface::buildHeader 1
FirebaseServiceBase::resetService public function Reset body of service.
FirebaseServiceBase::send public function Overrides FirebaseServiceInterface::send 1
FirebaseServiceBase::__construct public function Constructs a FirebaseServiceBase object. 3
FirebaseServiceBase::__sleep public function
FirebaseServiceBase::__wakeup public function