Coverage index

src/Plugin/migrate/destination/VisitorsCounter.php

Lines: 83.72%

LineHitsSource
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\visitors\Plugin\migrate\destination;
6
7
use Drupal\Core\Database\Connection;
8
use Drupal\Core\Entity\EntityTypeInterface;
9
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10
use Drupal\migrate\Plugin\MigrationInterface;
11
use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
12
use Drupal\migrate\Row;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
15
/**
16
 * Writes Statistics node counts into visitors_counter.
17
 *
18
 * A later import replaces the counts for that entity. Run this migration
19
 * before the new site records its own views.
20
 *
21
 * @MigrateDestination(
22
 *   id = "visitors_counter"
23
 * )
24
 */
25
class VisitorsCounter extends DestinationBase implements ContainerFactoryPluginInterface {
26
27
  /**
28
   * Constructs a VisitorsCounter object.
29
   *
30
   * @param array $configuration
31
   *   A configuration array containing information about the plugin instance.
32
   * @param string $plugin_id
33
   *   The plugin ID for the plugin instance.
34
   * @param mixed $plugin_definition
35
   *   The plugin implementation definition.
36
   * @param \Drupal\migrate\Plugin\MigrationInterface $migration
37
   *   The migration.
38
   * @param \Drupal\Core\Database\Connection $database
39
   *   The database connection.
40
   */
41
  public function __construct(
42
    array $configuration,
43
    $plugin_id,
44
    $plugin_definition,
45
    MigrationInterface $migration,
46
    protected Connection $database,
47
  ) {
482
    parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
492
    $this->supportsRollback = TRUE;
50
  }
51
52
  /**
53
   * {@inheritdoc}
54
   */
55
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, ?MigrationInterface $migration = NULL) {
562
    return new static(
572
      $configuration,
582
      $plugin_id,
592
      $plugin_definition,
602
      $migration,
612
      $container->get('database'),
622
    );
63
  }
64
65
  /**
66
   * {@inheritdoc}
67
   */
68
  public function import(Row $row, array $old_destination_id_values = []) {
692
    $entity_type = (string) $row->getDestinationProperty('entity_type');
702
    $entity_id = (int) $row->getDestinationProperty('entity_id');
712
    $this->database->merge('visitors_counter')
722
      ->keys([
732
        'entity_type' => $entity_type,
742
        'entity_id' => $entity_id,
752
      ])
762
      ->fields([
772
        'total' => (int) $row->getDestinationProperty('total'),
782
        'today' => (int) $row->getDestinationProperty('today'),
792
        'timestamp' => (int) $row->getDestinationProperty('timestamp'),
802
      ])
812
      ->execute();
82
832
    return [$entity_type, $entity_id];
84
  }
85
86
  /**
87
   * {@inheritdoc}
88
   */
89
  public function rollback(array $destination_identifier) {
901
    $this->database->delete('visitors_counter')
911
      ->condition('entity_type', $destination_identifier['entity_type'])
921
      ->condition('entity_id', $destination_identifier['entity_id'])
931
      ->execute();
94
  }
95
96
  /**
97
   * {@inheritdoc}
98
   */
99
  public function getIds() {
1002
    return [
1012
      'entity_type' => [
1022
        'type' => 'string',
1032
        'max_length' => EntityTypeInterface::ID_MAX_LENGTH,
1042
      ],
1052
      'entity_id' => [
1062
        'type' => 'integer',
1072
      ],
1082
    ];
109
  }
110
111
  /**
112
   * {@inheritdoc}
113
   */
114
  public function fields() {
1150
    return [
1160
      'entity_type' => $this->t('The entity type.'),
1170
      'entity_id' => $this->t('The entity ID.'),
1180
      'total' => $this->t('The total number of views.'),
1190
      'today' => $this->t('The number of views today.'),
1200
      'timestamp' => $this->t('The time of the most recent view.'),
1210
    ];
122
  }
123
124
}