You are here

abstract class ReportTypeBase in Commerce Reporting 8

Provides the base order report type class.

Hierarchy

Expanded class hierarchy of ReportTypeBase

File

src/Plugin/Commerce/ReportType/ReportTypeBase.php, line 14

Namespace

Drupal\commerce_reports\Plugin\Commerce\ReportType
View source
abstract class ReportTypeBase extends PluginBase implements ReportTypeInterface, ContainerFactoryPluginInterface {

  /**
   * The order report storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $orderReportStorage;

  /**
   * Constructs a new ReportTypeBase object.
   *
   * @param array $configuration
   *   The plugin configuration, i.e. an array with configuration values keyed
   *   by configuration option name.
   * @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.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->orderReportStorage = $entity_type_manager
      ->getStorage('commerce_order_report');
  }

  /**
   * {@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'));
  }

  /**
   * {@inheritdoc}
   */
  public function getLabel() {
    return $this->pluginDefinition['label'];
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this->pluginDefinition['description'];
  }

  /**
   * {@inheritdoc}
   */
  public function buildReportTable(array $results) {
    $build = [
      '#type' => 'table',
      '#header' => $this
        ->doBuildReportTableHeaders(),
      '#rows' => [],
      '#empty' => t('No reports yet'),
    ];
    foreach ($results as $result) {
      $row = $this
        ->doBuildReportTableRow($result);
      $build['#rows'][] = $row;
    }
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function createFromOrder(OrderInterface $order, array $values = []) {
    $values += [
      'type' => $this
        ->getPluginId(),
      'order_id' => $order
        ->id(),
      'created' => $order
        ->getPlacedTime(),
    ];
    $order_report = $this->orderReportStorage
      ->create($values);
    $order_report
      ->save();
  }

  /**
   * Builds the report table headers.
   *
   * @return array
   *   The report table headers.
   */
  protected abstract function doBuildReportTableHeaders();

  /**
   * Build the report table row.
   *
   * @param array $result
   *   The result row.
   *
   * @return array
   *   The table row data.
   */
  protected abstract function doBuildReportTableRow(array $result);

}

Members

Namesort descending Modifiers Type Description Overrides
BundlePluginInterface::buildFieldDefinitions public function Builds the field definitions for entities of this bundle. 2
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.
ReportTypeBase::$orderReportStorage protected property The order report storage.
ReportTypeBase::buildReportTable public function Build a report table from query results. Overrides ReportTypeInterface::buildReportTable
ReportTypeBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 2
ReportTypeBase::createFromOrder public function Creates a new order report using the given order and values. Overrides ReportTypeInterface::createFromOrder
ReportTypeBase::doBuildReportTableHeaders abstract protected function Builds the report table headers. 4
ReportTypeBase::doBuildReportTableRow abstract protected function Build the report table row. 4
ReportTypeBase::getDescription public function Gets the order report type description. Overrides ReportTypeInterface::getDescription
ReportTypeBase::getLabel public function Gets the order report type label. Overrides ReportTypeInterface::getLabel
ReportTypeBase::__construct public function Constructs a new ReportTypeBase object. Overrides PluginBase::__construct 2
ReportTypeInterface::buildQuery public function Builds the aggregate query. 4
ReportTypeInterface::generateReports public function Generates order reports for an order. 4