<?php

namespace LakeDrops\Docker4Drupal;

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

/**
 * Composer plugin for handling docker4drupal setup.
 */
class Plugin implements PluginInterface, EventSubscriberInterface {

  /**
   * @var \LakeDrops\Docker4Drupal\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_CREATE_PROJECT_CMD => 'configureProject',
      ScriptEvents::POST_INSTALL_CMD => 'configureProject',
      ScriptEvents::POST_UPDATE_CMD => 'configureProject',
    );
  }

  /**
   * Configure project event callback.
   *
   * @param \Composer\Script\Event $event
   */
  public function configureProject($event) {
    $this->handler->configureProject($event);
  }

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

}