Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / migrate_plus / src / Plugin / migrate / destination / Table.php
1 <?php
2
3 namespace Drupal\migrate_plus\Plugin\migrate\destination;
4
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Database\Database;
7 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8 use Drupal\migrate\MigrateException;
9 use Drupal\migrate\MigrateSkipProcessException;
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  * Provides table destination plugin.
17  *
18  * Use this plugin for a table not registered with Drupal Schema API.
19  *
20  * @MigrateDestination(
21  *   id = "table"
22  * )
23  */
24 class Table extends DestinationBase implements ContainerFactoryPluginInterface {
25
26   /**
27    * The name of the destination table.
28    *
29    * @var string
30    */
31   protected $tableName;
32
33   /**
34    * IDMap compatible array of id fields.
35    *
36    * @var array
37    */
38   protected $idFields;
39
40   /**
41    * Array of fields present on the destination table.
42    *
43    * @var array
44    */
45   protected $fields;
46
47   /**
48    * The database connection.
49    *
50    * @var \Drupal\Core\Database\Connection
51    */
52   protected $dbConnection;
53
54   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, Connection $connection) {
55     parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
56     $this->dbConnection = $connection;
57     $this->tableName = $configuration['table_name'];
58     $this->idFields = $configuration['id_fields'];
59     $this->fields = isset($configuration['fields']) ? $configuration['fields'] : [];
60     $this->supportsRollback = TRUE;
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
67     $db_key = !empty($configuration['database_key']) ? $configuration['database_key'] : NULL;
68
69     return new static(
70       $configuration,
71       $plugin_id,
72       $plugin_definition,
73       $migration,
74       Database::getConnection('default', $db_key)
75     );
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function getIds() {
82     if (empty($this->idFields)) {
83       throw new MigrateException('Id fields are required for a table destination');
84     }
85     return $this->idFields;
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function fields(MigrationInterface $migration = NULL) {
92     return $this->fields;
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function import(Row $row, array $old_destination_id_values = []) {
99     $id = $row->getSourceIdValues();
100     if (count($id) != count($this->idFields)) {
101       throw new MigrateSkipProcessException('All the id fields are required for a table migration.');
102     }
103
104     $values = $row->getDestination();
105
106     if ($this->fields) {
107       $values = array_intersect_key($values, $this->fields);
108     }
109
110     $status = $this->dbConnection->merge($this->tableName)
111       ->key($id)
112       ->fields($values)
113       ->execute();
114
115     return $status ? $id : NULL;
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public function rollback(array $destination_identifier) {
122     $delete = $this->dbConnection->delete($this->tableName);
123     foreach ($destination_identifier as $field => $value) {
124       $delete->condition($field, $value);
125     }
126     $delete->execute();
127   }
128 }