View source
<?php
declare (strict_types=1);
namespace Drupal\sms\Plugin\migrate\destination;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\sms\Entity\PhoneNumberVerificationInterface as EntityPhoneNumberVerificationInterface;
use Drupal\sms\Provider\PhoneNumberVerificationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SmsVerification extends EntityContentBase implements ContainerFactoryPluginInterface {
protected $phoneNumberVerificationService;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, PhoneNumberVerificationInterface $verification) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $entity_field_manager, $field_type_manager);
$this->phoneNumberVerificationService = $verification;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
$entity_type = static::getEntityTypeId($plugin_id);
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('entity_type.manager')
->getStorage($entity_type), array_keys($container
->get('entity_type.bundle.info')
->getBundleInfo($entity_type)), $container
->get('entity_field.manager'), $container
->get('plugin.manager.field.field_type'), $container
->get('sms.phone_number.verification'));
}
public function import(Row $row, array $old_destination_id_values = []) {
$return = parent::import($row, $old_destination_id_values);
if ($return) {
$verification = $this->storage
->load(reset($return));
$this
->setVerifiedValue($verification, $row
->getSourceProperty('delta'));
}
return $return;
}
public function rollback(array $destination_identifier) {
$verification = $this->storage
->load(reset($destination_identifier));
$this
->unsetVerifiedValue($verification);
parent::rollback($destination_identifier);
}
protected function setVerifiedValue(EntityPhoneNumberVerificationInterface $verification, $delta) {
if (!isset($delta)) {
$delta = 0;
}
$user_entity = $verification
->getEntity();
$phone_number_settings = $this->phoneNumberVerificationService
->getPhoneNumberSettingsForEntity($user_entity);
if ($user_entity && $phone_number_settings) {
$phone_field_name = $phone_number_settings
->getFieldName('phone_number');
$user_entity->{$phone_field_name}[$delta] = $verification
->getPhoneNumber();
$user_entity
->save();
}
}
protected function unsetVerifiedValue(EntityPhoneNumberVerificationInterface $verification) {
$user_entity = $verification
->getEntity();
$phone_number_settings = $this->phoneNumberVerificationService
->getPhoneNumberSettingsForEntity($user_entity);
if ($user_entity && $phone_number_settings) {
$phone_field_name = $phone_number_settings
->getFieldName('phone_number');
$user_entity->{$phone_field_name} = '';
$user_entity
->save();
}
}
}