Lines: 83.72%
| Line | Hits | Source |
|---|---|---|
| 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 | ) { | |
| 48 | 2 | parent::__construct($configuration, $plugin_id, $plugin_definition, $migration); |
| 49 | 2 | $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) { | |
| 56 | 2 | return new static( |
| 57 | 2 | $configuration, |
| 58 | 2 | $plugin_id, |
| 59 | 2 | $plugin_definition, |
| 60 | 2 | $migration, |
| 61 | 2 | $container->get('database'), |
| 62 | 2 | ); |
| 63 | } | |
| 64 | ||
| 65 | /** | |
| 66 | * {@inheritdoc} | |
| 67 | */ | |
| 68 | public function import(Row $row, array $old_destination_id_values = []) { | |
| 69 | 2 | $entity_type = (string) $row->getDestinationProperty('entity_type'); |
| 70 | 2 | $entity_id = (int) $row->getDestinationProperty('entity_id'); |
| 71 | 2 | $this->database->merge('visitors_counter') |
| 72 | 2 | ->keys([ |
| 73 | 2 | 'entity_type' => $entity_type, |
| 74 | 2 | 'entity_id' => $entity_id, |
| 75 | 2 | ]) |
| 76 | 2 | ->fields([ |
| 77 | 2 | 'total' => (int) $row->getDestinationProperty('total'), |
| 78 | 2 | 'today' => (int) $row->getDestinationProperty('today'), |
| 79 | 2 | 'timestamp' => (int) $row->getDestinationProperty('timestamp'), |
| 80 | 2 | ]) |
| 81 | 2 | ->execute(); |
| 82 | ||
| 83 | 2 | return [$entity_type, $entity_id]; |
| 84 | } | |
| 85 | ||
| 86 | /** | |
| 87 | * {@inheritdoc} | |
| 88 | */ | |
| 89 | public function rollback(array $destination_identifier) { | |
| 90 | 1 | $this->database->delete('visitors_counter') |
| 91 | 1 | ->condition('entity_type', $destination_identifier['entity_type']) |
| 92 | 1 | ->condition('entity_id', $destination_identifier['entity_id']) |
| 93 | 1 | ->execute(); |
| 94 | } | |
| 95 | ||
| 96 | /** | |
| 97 | * {@inheritdoc} | |
| 98 | */ | |
| 99 | public function getIds() { | |
| 100 | 2 | return [ |
| 101 | 2 | 'entity_type' => [ |
| 102 | 2 | 'type' => 'string', |
| 103 | 2 | 'max_length' => EntityTypeInterface::ID_MAX_LENGTH, |
| 104 | 2 | ], |
| 105 | 2 | 'entity_id' => [ |
| 106 | 2 | 'type' => 'integer', |
| 107 | 2 | ], |
| 108 | 2 | ]; |
| 109 | } | |
| 110 | ||
| 111 | /** | |
| 112 | * {@inheritdoc} | |
| 113 | */ | |
| 114 | public function fields() { | |
| 115 | 0 | return [ |
| 116 | 0 | 'entity_type' => $this->t('The entity type.'), |
| 117 | 0 | 'entity_id' => $this->t('The entity ID.'), |
| 118 | 0 | 'total' => $this->t('The total number of views.'), |
| 119 | 0 | 'today' => $this->t('The number of views today.'), |
| 120 | 0 | 'timestamp' => $this->t('The time of the most recent view.'), |
| 121 | 0 | ]; |
| 122 | } | |
| 123 | ||
| 124 | } |