You are here

MollieProfile.php in Mollie Payment 8.2

File

src/Entity/MollieProfile.php
View source
<?php

namespace Drupal\mollie_payment\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Entity\EntityManagerInterface;

/**
 * Defines a Mollie profile entity.
 *
 * @ConfigEntityType(
 *   admin_permission = "administer mollie payment",
 *   handlers = {
 *     "access" = "\Drupal\Core\Entity\EntityAccessControlHandler",
 *     "form" = {
 *       "default" = "Drupal\mollie_payment\Entity\MollieProfile\MollieProfileForm",
 *       "delete" = "Drupal\mollie_payment\Entity\MollieProfile\MollieProfileDeleteForm"
 *     },
 *     "list_builder" = "Drupal\mollie_payment\Entity\MollieProfile\MollieProfileListBuilder",
 *     "storage" = "\Drupal\Core\Config\Entity\ConfigEntityStorage"
 *   },
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
 *     "apiKey" = "apiKey"
 *   },
 *   config_export = {
 *     "id",
 *     "label",
 *     "apiKey",
 *     "uuid"
 *   },
 *   id = "mollie_profile",
 *   label = @Translation("Mollie Profile"),
 *   links = {
 *     "canonical" = "/admin/config/services/payment/mollie/profiles/edit/{mollie_profile}",
 *     "collection" = "/admin/config/services/payment/mollie/profiles",
 *     "edit-form" = "/admin/config/services/payment/mollie/profiles/edit/{mollie_profile}",
 *     "delete-form" = "/admin/config/services/payment/mollie/profiles/edit/{mollie_profile}/delete"
 *   }
 * )
 */
class MollieProfile extends ConfigEntityBase implements MollieProfileInterface {

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * The entity's unique machine name.
   *
   * @var string
   */
  public $id;

  /**
   * The human-readable name.
   *
   * @var string
   */
  protected $label;

  /**
   * The Mollie API key for this profile.
   *
   * @var string
   */
  protected $apiKey;

  /**
   * The typed config manager.
   *
   * @var \Drupal\Core\Config\TypedConfigManagerInterface
   */
  protected $typedConfigManager;

  /**
   * The entity's UUID.
   *
   * @var string
   */
  public $uuid;

  /**
   * The Mollie API Client
   *
   * @var \Mollie_API_Client
   */
  protected $mollieClient;

  /**
   * {@inheritdoc}
   */
  public function setId($id) {
    $this->id = $id;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setLabel($label) {
    $this->label = $label;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setApiKey($apiKey) {
    $this->apiKey = $apiKey;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getApiKey() {
    return $this->apiKey;
  }

  /**
   * Sets the entity manager.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *
   * @return $this
   */
  public function setEntityManager(EntityManagerInterface $entity_manager) {
    $this->entityManager = $entity_manager;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  protected function entityManager() {
    if (!$this->entityManager) {
      $this->entityManager = parent::entityManager();
    }
    return $this->entityManager;
  }

  /**
   * Sets the typed config.
   *
   * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config_manager
   *
   * @return $this
   */
  public function setTypedConfig(TypedConfigManagerInterface $typed_config_manager) {
    $this->typedConfigManager = $typed_config_manager;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  protected function getTypedConfig() {
    if (!$this->typedConfigManager) {
      $this->typedConfigManager = parent::getTypedConfig();
    }
    return $this->typedConfigManager;
  }

  /**
   * Set the Mollie API Client.
   */
  protected function setMollieClient() {
    $mollieClient = new \Mollie_API_Client();
    try {
      $mollieClient
        ->setApiKey($this
        ->getApiKey());

      // Register major version of Drupal.
      $mollieClient
        ->addVersionString('Drupal/8.x');

      // Register minor version of Drupal.
      $mollieClient
        ->addVersionString('Drupal/' . VERSION);
      $this->mollieClient = $mollieClient;
    } catch (Mollie_API_Exception $e) {
      drupal_set_message($e
        ->getMessage(), 'error');
    }
    return $this;
  }

  /**
   * Get the Mollie API Client.
   */
  public function getMollieClient() {
    if (!$this->mollieClient) {
      $this
        ->setMollieClient();
    }
    return $this->mollieClient;
  }

}

Classes

Namesort descending Description
MollieProfile Defines a Mollie profile entity.