f04d9f66d75f9bcb5933445e8b25fdea3d1d28ac
[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   /**
55    * Constructs a new Table.
56    *
57    * @param array $configuration
58    *   A configuration array containing information about the plugin instance.
59    * @param string $plugin_id
60    *   The plugin_id for the plugin instance.
61    * @param mixed $plugin_definition
62    *   The plugin implementation definition.
63    * @param \Drupal\migrate\Plugin\MigrationInterface $migration
64    *   The migration.
65    * @param \Drupal\Core\Database\Connection $connection
66    *   The database connection.
67    */
68   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, Connection $connection) {
69     parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
70     $this->dbConnection = $connection;
71     $this->tableName = $configuration['table_name'];
72     $this->idFields = $configuration['id_fields'];
73     $this->fields = isset($configuration['fields']) ? $configuration['fields'] : [];
74     $this->supportsRollback = TRUE;
75   }
76
77   /**
78    * {@inheritdoc}
79    */
80   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
81     $db_key = !empty($configuration['database_key']) ? $configuration['database_key'] : NULL;
82
83     return new static(
84       $configuration,
85       $plugin_id,
86       $plugin_definition,
87       $migration,
88       Database::getConnection('default', $db_key)
89     );
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public function getIds() {
96     if (empty($this->idFields)) {
97       throw new MigrateException('Id fields are required for a table destination');
98     }
99     return $this->idFields;
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   public function fields(MigrationInterface $migration = NULL) {
106     return $this->fields;
107   }
108
109   /**
110    * {@inheritdoc}
111    */
112   public function import(Row $row, array $old_destination_id_values = []) {
113     $id = $row->getSourceIdValues();
114     if (count($id) != count($this->idFields)) {
115       throw new MigrateSkipProcessException('All the id fields are required for a table migration.');
116     }
117
118     $values = $row->getDestination();
119
120     if ($this->fields) {
121       $values = array_intersect_key($values, $this->fields);
122     }
123
124     $status = $this->dbConnection->merge($this->tableName)
125       ->key($id)
126       ->fields($values)
127       ->execute();
128
129     return $status ? $id : NULL;
130   }
131
132   /**
133    * {@inheritdoc}
134    */
135   public function rollback(array $destination_identifier) {
136     $delete = $this->dbConnection->delete($this->tableName);
137     foreach ($destination_identifier as $field => $value) {
138       $delete->condition($field, $value);
139     }
140     $delete->execute();
141   }
142
143 }