Completed
Push — master ( 49cca7...006e45 )
by Raffael
13:02 queued 09:25
created

Mail::setOptions()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 17
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 12
nc 5
nop 1
crap 30
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\Async;
13
14
use Psr\Log\LoggerInterface;
15
use TaskScheduler\AbstractJob;
16
use Zend\Mail\Message;
17
use Zend\Mail\Transport\TransportInterface;
18
19
class Mail extends AbstractJob
20
{
21
    /**
22
     * Transport.
23
     *
24
     * @var TransportInterface
25
     */
26
    protected $transport;
27
28
    /**
29
     * Logger.
30
     *
31
     * @var LoggerInterface
32
     */
33
    protected $logger;
34
35
    /**
36
     * Sender address.
37
     *
38
     * @var string
39
     */
40
    protected $sender_address = 'balloon@local';
41
42
    /**
43
     * Sender name.
44
     *
45
     * @var string
46
     */
47
    protected $sender_name = 'balloon';
48
49
    /**
50
     * Constructor.
51
     */
52
    public function __construct(TransportInterface $transport, LoggerInterface $logger, ?Iterable $config = null)
53
    {
54
        $this->transport = $transport;
55
        $this->logger = $logger;
56
        $this->setOptions($config);
57
    }
58
59
    /**
60
     * Set options.
61
     */
62
    public function setOptions(?Iterable $config = []): self
63
    {
64
        if (null === $config) {
65
            return $this;
66
        }
67
68
        foreach ($config as $option => $value) {
69
            switch ($option) {
70
                case 'sender_address':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
71
                case 'sender_name':
72
                    $this->{$option} = $value;
73
74
                break;
75
                default:
76
                    throw new InvalidArgumentException('invalid option '.$option.' given');
77
            }
78
        }
79
80
        return $this;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function start(): bool
87
    {
88
        $mail = Message::fromString($this->data);
89
        $mail->setEncoding('UTF-8');
90
        $mail->setFrom($this->sender_address, $this->sender_name);
91
        $mail->getHeaders()->addHeaderLine('X-Mailer', 'balloon');
92
93
        $this->logger->debug('send mail ['.$mail->getSubject().']', [
94
            'category' => get_class($this),
95
        ]);
96
97
        $this->transport->send($mail);
98
        $connection = $this->transport->getConnection();
99
        $connection->rset();
100
        $connection->quit();
101
        $connection->disconnect();
102
103
        return true;
104
    }
105
}
106