0
\$\begingroup\$

My code-editor "show hints" says that the method length exceeds 20 lines. How can I refactor the setEmailTemplateBcc function?

private function setBcc($bcc)
    {
        if ($bcc) {
            $this->bcc = explode(',', $bcc);
        }
    }


    public function setType($type)
    {
        $this->setEmailTemplateBcc($type);
        return $this;
    }

    private function setEmailTemplateBcc($type)
    {
        $template = '';
        $bcc = '';

        switch ($type) {
            case self::EMAIL_NEW_SUBSCRIPTION:
                $template = DataHelper::XML_PATH_SUBSCRIPTION_EMAIL;
                $bcc = DataHelper::XML_PATH_REMINDER_EMAIL_BCC;
                break;
            case self::EMAIL_CARD_ADD:
                $template = DataHelper::XML_PATH_NEW_CARD_ADD_EMAIL;
                $bcc = DataHelper::XML_PATH_NEW_CARD_ADD_EMAIL_BCC;
                break;
            case self::EMAIL_PAYMENT_FAILED:
                $template = DataHelper::XML_PATH_PAYMENT_FAILED_EMAIL;
                $bcc = DataHelper::XML_PATH_PAYMENT_FAILED_EMAIL_BCC;
                break;
            case self::EMAIL_PROFILE_UPADATE:
                $template = DataHelper::XML_PATH_PROFILE_UPDATE_EMAIL;
                $bcc = DataHelper::XML_PATH_PROFILE_UPDATE_EMAIL_BCC;
                break;
            case self::EMAIL_REMINDER:
                $template = DataHelper::XML_PATH_REMINDER_EMAIL;
                $bcc = DataHelper::XML_PATH_REMINDER_EMAIL_BCC;
                break;
            case self::EMAIL_TOPUP_REMINDER:
                $template = DataHelper::XML_PATH_EWALLET_TOPUP_REMINDER_EMAIL;
                $bcc = DataHelper::XML_PATH_EWALLET_TOPUP_REMINDER_EMAIL;
                break;
        }

        $this->setTemplate($template);
        $this->setBcc($bcc);
    }
\$\endgroup\$
3
\$\begingroup\$

I would create two new static variables in your class, arrays that contain all options, like

public static $template = [
    self::EMAIL_NEW_SUBSCRIPTION => DataHelper::XML_PATH_SUBSCRIPTION_EMAIL,
    self::EMAIL_CARD_ADD         => DataHelper::XML_PATH_NEW_CARD_ADD_EMAIL_BCC,
    // and so on
];

then setting the email template will be as simple as just one line:

$template = self::$template[self::EMAIL_CARD_ADD] ?? '';

and the same goes for BCC

\$\endgroup\$
0
\$\begingroup\$

Or you can try this approach.

interface EmailTemplateInterface
{
    public function create($type);

    /**
     * @param string $name
     * @return bool
     */
    public function supports($type);
}

class EmailNewSubscription implements EmailTemplateInterface
{
    /**
     * @inheritdoc
     */
    public function create($type)
    {
        echo 'XML_PATH_SUBSCRIPTION_EMAIL';
    }

    /**
     * @inheritdoc
     */
    public function supports($type)
    {
        return $type === 'XML_PATH_SUBSCRIPTION_EMAIL';
    }
}

class EmailCardAdd implements EmailTemplateInterface
{
    /**
     * @inheritdoc
     */
    public function create($type)
    {
        echo 'EMAIL_CARD_ADD';
    }

    /**
     * @inheritdoc
     */
    public function supports($type)
    {
        return $type === 'EMAIL_CARD_ADD';
    }
}

class BuildEmailTemplate implements EmailTemplateInterface
{
    /**
     * @var EmailTemplateInterface[]
     */
    private $emailTemplateInterface;

    /**
     * @param EmailTemplateInterface[] $emailCreators
     * @throws \InvalidArgumentException
     */
    public function __construct(array $emailCreators)
    {
        foreach ($emailCreators as $builder) {
            if (!$builder instanceof EmailTemplateInterface) {
                throw new \InvalidArgumentException('emailCreator is not valid');
            }
        }
        $this->emailTemplateInterface = $emailCreators;
    }

    /**
     * @inheritdoc
     */
    public function create($type)
    {
        foreach ($this->emailTemplateInterface as $builder) {
            if ($builder->supports($type)) {
                return $builder->create($type);
            }
        }

        throw new \UnexpectedValueException('email creator for supporting this type is not found');
    }

    /**
     * @inheritdoc
     */
    public function supports($type)
    {
        foreach ($this->emailTemplateInterface as $builder) {
            if ($builder->supports($type)) {
                return true;
            }
        }

        return false;
    }

}
// usage
$emailCreators = [
    new EmailNewSubscription(),
    new EmailCardAdd(),
];

$delegate = new BuildEmailTemplate($emailCreators);

$delegate->create('XML_PATH_SUBSCRIPTION_EMAIL');
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.