MailerAction::execute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 2
Metric Value
c 5
b 2
f 2
dl 0
loc 14
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the WPSymfonyForm plugin.
5
 *
6
 * Copyright (c) 2015-2016 LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\WPSymfonyForm\Action;
13
14
use Symfony\Component\Form\FormInterface;
15
16
/**
17
 * Class MailerAction.
18
 *
19
 * @author Gorka Laucirica <[email protected]>
20
 */
21
class MailerAction implements Action
22
{
23
    /**
24
     * The email subject.
25
     *
26
     * @var string
27
     */
28
    private $subject;
29
30
    /**
31
     * The email HTML template.
32
     *
33
     * @var string
34
     */
35
    private $template;
36
37
    /**
38
     * The receiver email.
39
     *
40
     * @var string
41
     */
42
    private $to;
43
44
    /**
45
     * Constructor.
46
     *
47
     * @param string $to       The receiver email
48
     * @param string $template The email HTML template
49
     * @param string $subject  The email subject
50
     */
51
    public function __construct($to, $template, $subject)
52
    {
53
        $this->to = $to;
54
        $this->template = $template;
55
        $this->subject = $subject;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function execute(FormInterface $form)
62
    {
63
        if (class_exists('\Timber\Timber')) {
64
            $message = \Timber\Timber::compile($this->template, ['request' => $form->getData()]);
65
        } elseif (class_exists('\Timber')) {
66
            $message = \Timber::compile($this->template, ['request' => $form->getData()]);
67
        } else {
68
            throw new \InvalidArgumentException('Timber is required to use MailerAction');
69
        }
70
71
        wp_mail($this->to, $this->subject, $message, [
72
            'Content-Type: text/html; charset=UTF-8',
73
        ]);
74
    }
75
}
76