Passed
Pull Request — master (#50)
by Ronan
09:06
created

Helper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 54
c 1
b 0
f 0
dl 0
loc 119
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B send() 0 80 7
A __construct() 0 8 1
1
<?php
2
3
namespace App\Mail;
4
5
use App\Facades\Log;
6
use Exception;
7
use ReflectionClass;
8
use Swift_Mailer;
9
use Swift_Message;
10
use Twig\Environment;
11
use Twig\Error\LoaderError;
12
13
/**
14
 * Mail helper
15
 *
16
 * The mail helper provides a simple interface for sending mail via swiftmailer
17
 *
18
 * @author Ronan Chilvers <[email protected]>
19
 */
20
class Helper
21
{
22
    /**
23
     * @var array
24
     */
25
    protected $config;
26
27
    /**
28
     * @var Swift_Mailer
29
     */
30
    protected $swiftMailer;
31
32
    /**
33
     * @var Twig_Environment
0 ignored issues
show
Bug introduced by
The type App\Mail\Twig_Environment was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
     */
35
    protected $twig;
36
37
    /**
38
     * Class constructor
39
     *
40
     * @author Ronan Chilvers <[email protected]>
41
     */
42
    public function __construct(
43
        array $config,
44
        Swift_Mailer $swiftMailer,
45
        Environment $twig
46
    ) {
47
        $this->config      = $config;
48
        $this->swiftMailer = $swiftMailer;
49
        $this->twig        = $twig;
0 ignored issues
show
Documentation Bug introduced by
It seems like $twig of type Twig\Environment is incompatible with the declared type App\Mail\Twig_Environment of property $twig.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50
    }
51
52
    /**
53
     * Send an email
54
     *
55
     * @param App\Mail\Email $email
0 ignored issues
show
Bug introduced by
The type App\Mail\App\Mail\Email was not found. Did you mean App\Mail\Email? If so, make sure to prefix the type with \.
Loading history...
56
     * @return bool
57
     * @author Ronan Chilvers <[email protected]>
58
     */
59
    public function send(Email $email)
60
    {
61
        Log::debug('Beginning mail send', [
62
            'email' => $email,
63
        ]);
64
        try {
65
            $message = new Swift_Message(
66
                $email->getSubject()
67
            );
68
69
            // Sender
70
            if ($email->getFrom()) {
71
                $message->setFrom(
72
                    $email->getFrom(),
73
                    $email->getFromName()
74
                );
75
            } else {
76
                $message->setFrom(
77
                    $this->config['from'],
78
                    $this->config['from_name']
79
                );
80
            }
81
82
            // Recipients
83
            $destinations = [
84
                'addTo'  => $email->getTo(),
85
                'addCc'  => $email->getCc(),
86
                'addBcc' => $email->getBcc(),
87
            ];
88
            foreach ($destinations as $setter => $addresses) {
89
                foreach ($addresses as $address => $name) {
90
                    $message->$setter($address, $name);
91
                }
92
            }
93
94
            // Default template contexts
95
            // foreach ($this->config['site'] as $key => $value) {
96
            //     $email->addTemplateContext(
97
            //         'site_' . $key,
98
            //         $value
99
            //     );
100
            // }
101
102
            // Content
103
            $context = $email->getTemplateContext();
104
            $html = $this->twig->render(
105
                $email->getTemplateHtml(),
106
                $context
107
            );
108
109
            try {
110
                $text = $this->twig->render(
111
                    $email->getTemplateText(),
112
                    $context
113
                );
114
            } catch (LoaderError $ex) {
115
                $text = strip_tags($html);
116
            }
117
            $text = trim($text);
118
            $html = trim($html);
119
            $message->setBody($text);
120
            $message->addPart($html, 'text/html');
121
122
            $failed = [];
123
            $result = $this->swiftMailer->send($message, $failed);
124
            Log::debug('Sent email', [
125
                'recipient_count' => $result,
126
                'message' => $message,
127
                'failed' => $failed,
128
            ]);
129
            if (0 == $result) {
130
                return false;
131
            }
132
133
            return true;
134
        } catch (Exception $ex) {
135
            Log::error($ex->getMessage(), [
136
                'exception' => $ex,
137
            ]);
138
            return false;
139
        }
140
    }
141
}
142