BlockedIp.php 2.11 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Drupal\ban\Plugin\migrate\destination;

use Drupal\ban\BanIpManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7
use Drupal\migrate\Plugin\MigrationInterface;
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Destination for blocked IP addresses.
 *
 * @MigrateDestination(
 *   id = "blocked_ip"
 * )
 */
class BlockedIP extends DestinationBase implements ContainerFactoryPluginInterface {

  /**
   * The IP ban manager.
   *
   * @var \Drupal\ban\BanIpManagerInterface
   */
  protected $banManager;

  /**
   * Constructs a BlockedIP object.
   *
   * @param array $configuration
32
   *   Plugin configuration.
33
   * @param string $plugin_id
34
   *   The plugin ID.
35
   * @param mixed $plugin_definition
36
   *   The plugin definiiton.
37
   * @param \Drupal\migrate\Plugin\MigrationInterface $migration
38
   *   The current migration.
39
   * @param \Drupal\ban\BanIpManagerInterface $ban_manager
40
   *   The IP manager service.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, BanIpManagerInterface $ban_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
    $this->banManager = $ban_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $migration,
      $container->get('ban.ip_manager')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getIds() {
    return ['ip' => ['type' => 'string']];
  }

  /**
   * {@inheritdoc}
   */
  public function fields(MigrationInterface $migration = NULL) {
    return [
      'ip' => $this->t('The blocked IP address.'),
    ];
  }

  /**
   * {@inheritdoc}
   */
79
  public function import(Row $row, array $old_destination_id_values = []) {
80 81 82 83
    $this->banManager->banIp($row->getDestinationProperty('ip'));
  }

}