mercredi 13 décembre 2017

Better implementation or use design pattern

This is my service class :

<?php

namespace Service\Email;

use JMS\DiExtraBundle\Annotation as DI;

/**
* @DI\Service("manager.email")
*/
class Email
{
const USER_SUCCESS  = 1;
const ADMIN_SUCCESS = 2;

/**
 * @var
 */
private $oTemplating;

/**
 * @var
 */
private $oMailer;

/**
 * @var
 */
private $sMailerFrom;

/**
 * @var
 */
private $sMailerTo;


/**
 * @DI\InjectParams({
 *     "oTemplating" = @DI\Inject("templating"),
 *     "oMailer"     = @DI\Inject("mailer"),
 *     "sMailerFrom" = @DI\Inject("%mailer_from%"),
 *     "sMailerTo"   = @DI\Inject("%mailer_to%")
 * })
 */
public function __construct($oTemplating, \Swift_Mailer $oMailer, $sMailerFrom, $sMailerTo){
    $this->oTemplating   = $oTemplating;
    $this->oMailer       = $oMailer;
    $this->sMailerFrom   = $sMailerFrom;
    $this->sMailerTo     = $sMailerTo;
}

public function sendEmail($nType, $aParams){
    switch ($nType){
        case self::USER_SUCCESS:
            $sSubject   = '';
            $sEmailTo   = '';
            $oResponse  = $this->oTemplating->render('Test::email/client.html.twig', array('aParams'  => $aParams));
            break;
        case self::ADMIN_SUCCESS:
            $sSubject   = '';
            $sEmailTo   = '';
            $oResponse  = $this->oTemplating->render('Test::email/admin.html.twig', array('aParams'  => $aParams));
            break;
    }
    $message = \Swift_Message::newInstance()
        ->setSubject($sSubject)
        ->setFrom($this->sMailerFrom)
        ->setTo($sEmailTo)
        ->setBody($oResponse);

    $this->oMailer->send($message);
}

} And the implementation is :

$this->get('manager.email')->sendEmail(1, $aParams);
$this->get('manager.email')->sendEmail(2, $aParams);

This works perfect, but I want to use a design pattern or a clean implementation. Can you help me please ? Thank you, and sorry for my english. I have no idea how to change that. The code works just fine now, but I want to use a design pattern.

Aucun commentaire:

Enregistrer un commentaire