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 ( a9ac44...3461ec )
by Drakakis
02:02
created

InteractsWithSwiftEmailer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A sendMail() 0 15 3
D buildMailMessage() 0 38 8
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 $option)
33
    {
34
        // Set defaults
35
        if (!array_key_exists('from', $option)) {
36
            $option['from'] = ['[email protected]' => 'FromTester'];
37
        }
38
39
        if (!array_key_exists('to', $option)) {
40
            $option['to'] = ['[email protected]' => 'ToTester'];
41
        }
42
        if (!array_key_exists('subject', $option)) {
43
            $option['subject'] = 'Testing Email';
44
        }
45
        if (!array_key_exists('contentType', $option)) {
46
            $option['contentType'] = 'text/html';
47
        }
48
        if (!array_key_exists('cc', $option)) {
49
            $option['cc'] = [];
50
        }
51
        if (!array_key_exists('bcc', $option)) {
52
            $option['bcc'] = [];
53
        }
54
55
        // Make sure Body exists
56
        if (!array_key_exists('body', $option)) {
57
            throw new RuntimeException('You really need to set the body');
58
        }
59
60
        $this->emailMessage = Swift_Message::newInstance()
61
            ->setSubject($option['subject'])
62
            ->setFrom($option['from'])
63
            ->setCc($option['cc'])
64
            ->setBcc($option['bcc'])
65
            ->setTo($option['to'])
66
            ->setBody($option['body'], $option['contentType']);
67
68
        return $this;
69
    }
70
}
71