<?php

namespace LakeDrops\DorgFlow;

use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\ScriptEvents;

/**
 * Composer plugin for preparing Drupal project for development with dorgflow.
 */
class Plugin implements PluginInterface, EventSubscriberInterface {

  /**
   * @var \LakeDrops\DorgFlow\Handler
   */
  protected $handler;

  /**
   * {@inheritdoc}
   */
  public function activate(Composer $composer, IOInterface $io) {
    $this->handler = new Handler($composer, $io);
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return array(
      ScriptEvents::POST_INSTALL_CMD => 'prepareDevProjects',
      ScriptEvents::POST_UPDATE_CMD => 'prepareDevProjects',
    );
  }

  /**
   * Post install/update event callback.
   *
   * @param \Composer\Script\Event $event
   */
  public function prepareDevProjects($event) {
    $this->handler->prepareDevProjects($event);
  }

  /**
   * Script callback for putting in composer scripts to prepare the project.
   *
   * @param \Composer\Script\Event $event
   */
  public static function prepare($event) {
    $handler = new Handler($event->getComposer(), $event->getIO());
    $handler->prepareDevProjects($event);
  }

}