GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( ae4985...7677b1 )
by Drakakis
02:51
created

InteractsWithSwiftEmailer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 60
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A sendMail() 0 15 3
A buildMailMessage() 0 19 2
A setDefaults() 0 19 3
1
<?php
2
3
4
namespace Drakakisgeo\Mailtester;
5
6
use RuntimeException;
7
use Swift_Message;
8
use Swift_Mailer;
9
use Swift_Mime_MimePart;
10
use Swift_SmtpTransport;
11
12
trait InteractsWithSwiftEmailer
13
{
14
    private $emailMessage = null;
15
16
    public function sendMail()
17
    {
18
        if (is_null($this->emailMessage)) {
19
            throw new RuntimeException('You need to create the message first and chain it.');
20
        }
21
22
        $transport = Swift_SmtpTransport::newInstance(getenv('MAIL_HOST'), getenv('MAIL_PORT'));
23
        $mailer = Swift_Mailer::newInstance($transport);
24
25
        if (!$mailer->send($this->emailMessage)) {
26
            throw new RuntimeException('Can\'t send the Email message');
27
        }
28
29
        $this->emailMessage = null;
30
    }
31
32
    public function buildMailMessage(array $options)
33
    {
34
        $options = $this->setDefaults($options);
35
36
        // Make sure Body exists
37
        if (!array_key_exists('body', $options)) {
38
            throw new RuntimeException('You really need to set the body');
39
        }
40
41
        $this->emailMessage = Swift_Message::newInstance()
42
            ->setSubject($options['subject'])
43
            ->setFrom($options['from'])
44
            ->setCc($options['cc'])
45
            ->setBcc($options['bcc'])
46
            ->setTo($options['to'])
47
            ->setBody($options['body'], $options['contentType']);
48
49
        return $this;
50
    }
51
52
    private function setDefaults($options)
53
    {
54
        $defaults = [
55
            'from' => ['[email protected]' => 'FromTester'],
56
            'to' => ['[email protected]' => 'ToTester'],
57
            'subject' => 'Testing Email',
58
            'contentType' => 'text/html',
59
            'cc' => [],
60
            'bcc' => []
61
        ];
62
63
        foreach ($defaults as $key=>$value) {
64
            if (!array_key_exists($key, $options)) {
65
                $options[$key] = $value;
66
            }
67
        }
68
69
        return $options;
70
    }
71
}
72