<?php

namespace Cyberpull\Plugin\Mailer;

require_once ABSPATH . WPINC . '/class-phpmailer.php';

use Cyberpull\Plugin\Exceptions\Exception;
use Cyberpull\Plugin\Option\Messaging\Settings;
use PHPMailer as WP_PHPMailer, phpmailerException;

class PHPMailer extends WP_PHPMailer {

  public function send() {
    try {
      $settings = Settings::instance();
      $provider = $settings->provider;

      if ($provider === 'php') {
        // Use Default Mail Provider
        return parent::send();
      } else {
        // Use Other Mail Providers
        $hook = "cyberpull_send_mail_{$provider}";
        do_action_ref_array($hook, array($this));

        if ($provider === 'smtp') {
          $this->isSMTP();
          return parent::send();
        } else {
          return true;
        }
      }

    } catch (Exception $e) {
      throw new phpmailerException($e->getMessage(), $e->getCode());
    }
  }

}
