<?php

/**
 * @file
 * Contains \Drupal\migrate_pets\Plugin\migrate\destination\PetDestination
 */

namespace Drupal\migrate_pets\Plugin\migrate\destination;

use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
use Drupal\migrate\Entity\MigrationInterface;
use Drupal\migrate\Row;

/**
 * Pet destination.
 *
 * @MigrateDestination(
 *   id = "pet_dest"
 * )
 */
class PetDestination extends DestinationBase {

  /**
   * {@inheritdoc}
   */
  public function fields(MigrationInterface $migration = NULL) {
    return [
      'pid' => $this->t('Pet id'),
      'type' => $this->t('Pet type'),
      'name' => $this->t('Pet name'),
      'photo' => $this->t('Pet photo'),
    ];
  }

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

  /**
   * {@inheritdoc}
   */
  public function import(Row $row, array $old_destination_id_values = []) {
    $pet = $this->save($row);
    if (!$pet) {
      throw new MigrateException('Could not save pet');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function rollback(array $destination_identifier) {
    $pet = $this->load(reset($destination_identifier));
    if ($pet) {
      $pet->delete();
    }
  }

  /**
   * Load a pet object.
   */
  protected function load($pid) {
    // Load and return pet object.
  }

  /**
   * Save a pet object.
   */
  protected function save(Row $row) {
    // Save and return pet object.
  }

}
