Completed
Push — master ( e6ae53...3ea501 )
by Sam
10:01 queued 45s
created

SwiftMailer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A sendSwift() 0 4 1
A getSwiftMailer() 0 4 1
A setSwiftMailer() 0 10 2
A send() 0 9 1
1
<?php
2
3
namespace SilverStripe\Control\Email;
4
5
use SilverStripe\Core\Config\Configurable;
6
use SilverStripe\Core\Injector\Injectable;
7
use SilverStripe\Core\Injector\Injector;
8
use Swift_Mailer;
9
use Swift_Message;
10
11
/**
12
 * Mailer objects are responsible for actually sending emails.
13
 * The default Mailer class will use PHP's mail() function.
14
 */
15
class SwiftMailer implements Mailer
16
{
17
18
    use Configurable;
19
    use Injectable;
20
21
    /**
22
     * @var array
23
     * @config
24
     */
25
    private static $swift_plugins = array(
26
        SwiftPlugin::class,
27
    );
28
29
    /**
30
     * @var Swift_Mailer
31
     */
32
    private $swift;
33
34
    /**
35
     * @param Email $message
36
     * @return bool Whether the sending was "successful" or not
37
     */
38
    public function send($message)
39
    {
40
        $swiftMessage = $message->getSwiftMessage();
41
        $failedRecipients = array();
42
        $result = $this->sendSwift($swiftMessage, $failedRecipients);
43
        $message->setFailedRecipients($failedRecipients);
0 ignored issues
show
Bug introduced by
It seems like $failedRecipients can also be of type null; however, SilverStripe\Control\Ema...::setFailedRecipients() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
44
45
        return $result != 0;
46
    }
47
48
    /**
49
     * @param Swift_Message $message
50
     * @param array $failedRecipients
51
     * @return int
52
     */
53
    protected function sendSwift($message, &$failedRecipients = null)
54
    {
55
        return $this->getSwiftMailer()->send($message, $failedRecipients);
56
    }
57
58
    /**
59
     * @return Swift_Mailer
60
     */
61
    public function getSwiftMailer()
62
    {
63
        return $this->swift;
64
    }
65
66
    /**
67
     * @param Swift_Mailer $swift
68
     * @return $this
69
     */
70
    public function setSwiftMailer($swift)
71
    {
72
        // register any required plugins
73
        foreach ($this->config()->get('swift_plugins') as $plugin) {
74
            $swift->registerPlugin(Injector::inst()->create($plugin));
75
        }
76
        $this->swift = $swift;
77
78
        return $this;
79
    }
80
}
81