Passed
Push — master ( 22ad80...fa0027 )
by Gabor
08:44
created

ServiceAdapter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 133
ccs 0
cts 53
cp 0
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 18 5
A setFrom() 0 6 1
A addTo() 0 6 1
A addCc() 0 6 1
A addBcc() 0 6 1
A setSubject() 0 6 1
A setBody() 0 6 1
A addAttachment() 0 5 1
A send() 0 4 1
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Mailer\ServiceAdapter\TxMailer;
15
16
use Tx\Mailer;
17
use WebHemi\Configuration\ServiceInterface as ConfigurationInterface;
18
use WebHemi\Mailer\ServiceInterface;
19
20
/**
21
 * Class ServiceAdapter
22
 * @SuppressWarnings(PHPMD.ShortMethodName)
23
 */
24
class ServiceAdapter implements ServiceInterface
25
{
26
    /** @var Mailer */
27
    private $mailer;
28
29
    /**
30
     * ServiceAdapter constructor.
31
     * @param ConfigurationInterface $configuration
32
     */
33
    public function __construct(ConfigurationInterface $configuration)
34
    {
35
        $configuration = $configuration->getData('email');
36
37
        $secure = isset($configuration['secure']) ? $configuration['secure'] : null;
38
        $auth = isset($configuration['auth']) ? $configuration['auth'] : null;
39
40
        $this->mailer = new \Tx\Mailer(null);
41
        $this->mailer->setServer($configuration['host'], $configuration['port'], $secure);
42
43
        if ($auth == 'login') {
44
            $this->mailer->setAuth($configuration['username'], $configuration['password']);
45
        } elseif ($auth == 'oatuh') {
46
            $this->mailer->setOAuth($configuration['oauthToken']);
47
        }
48
49
        $this->mailer->setFrom($configuration['from']['name'], $configuration['from']['email']);
50
    }
51
52
    /**
53
     * Sets the sender.
54
     *
55
     * @param string $name
56
     * @param string $email
57
     * @return ServiceInterface
58
     */
59
    public function setFrom(string $name, string $email) : ServiceInterface
60
    {
61
        $this->mailer->setFrom($name, $email);
62
63
        return $this;
64
    }
65
66
    /**
67
     * Sets a recipient.
68
     *
69
     * @param string $name
70
     * @param string $email
71
     * @return ServiceInterface
72
     */
73
    public function addTo(string $name, string $email) : ServiceInterface
74
    {
75
        $this->mailer->addTo($name, $email);
76
77
        return $this;
78
    }
79
80
    /**
81
     * Sets a Carbon Copy recipient.
82
     *
83
     * @param string $name
84
     * @param string $email
85
     * @return ServiceInterface
86
     */
87
    public function addCc(string $name, string $email) : ServiceInterface
88
    {
89
        $this->mailer->addCc($name, $email);
90
91
        return $this;
92
    }
93
94
    /**
95
     * Sets a Blind Carbon Copy recipient.
96
     *
97
     * @param string $name
98
     * @param string $email
99
     * @return ServiceInterface
100
     */
101
    public function addBcc(string $name, string $email) : ServiceInterface
102
    {
103
        $this->mailer->addBcc($name, $email);
104
105
        return $this;
106
    }
107
108
    /**
109
     * Sets the subject.
110
     *
111
     * @param string $subject
112
     * @return ServiceInterface
113
     */
114
    public function setSubject(string $subject) : ServiceInterface
115
    {
116
        $this->mailer->setSubject($subject);
117
118
        return $this;
119
    }
120
121
    /**
122
     * Sets the body.
123
     *
124
     * @param string $body
125
     * @return ServiceInterface
126
     */
127
    public function setBody(string $body) : ServiceInterface
128
    {
129
        $this->mailer->setBody($body);
130
131
        return $this;
132
    }
133
134
    /**
135
     * Adds an attachment to the mail.
136
     *
137
     * @param string $name
138
     * @param string $path
139
     * @return ServiceInterface
140
     */
141
    public function addAttachment(string $name, string $path) : ServiceInterface
142
    {
143
        $this->mailer->addAttachment($name, $path);
144
        return $this;
145
    }
146
147
    /**
148
     * Sends the email.
149
     *
150
     * @return bool
151
     */
152
    public function send() : bool
153
    {
154
        return $this->mailer->send();
155
    }
156
}
157