Passed
Push — master ( e373b2...0aca6e )
by Andreas
17:28
created

org_openpsa_mail_backend::prepare_mailer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package org.openpsa.mail
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
/**
10
 * Backend for mailer operations
11
 *
12
 * @package org.openpsa.mail
13
 */
14
abstract class org_openpsa_mail_backend
15
{
16
    public $error;
17
18
    /**
19
     * @var Swift_Mailer
20
     */
21
    protected $mailer;
22
23
    abstract public function __construct(array $params);
24
25
    /**
26
     * Send the actual email
27
     *
28
     * @param org_openpsa_mail_message $message
29
     */
30
    abstract public function mail(org_openpsa_mail_message $message);
31
32
    /**
33
     * Prepare the mail backend
34
     */
35 5
    public static function get(string $implementation, array $params) : self
36
    {
37 5
        if (defined('OPENPSA2_UNITTEST_RUN')) {
38 5
            return self::_load_backend('unittest', $params);
39
        }
40
        if ($implementation == 'try_default') {
41
            $try_backends = midcom_baseclasses_components_configuration::get('org.openpsa.mail', 'config')->get('default_try_backends');
42
            //Use first available backend in list
43
            foreach ($try_backends as $backend) {
44
                try {
45
                    $object = self::_load_backend($backend, $params);
46
                    debug_add('Using backend ' . $backend);
47
                    return $object;
48
                } catch (midcom_error $e) {
49
                    debug_add('Failed to load backend ' . $backend . ', message:' . $e->getMessage());
50
                }
51
            }
52
            throw new midcom_error('All configured backends failed to load');
53
        }
54
        return self::_load_backend($implementation, $params);
55
    }
56
57
    protected function prepare_mailer(Swift_Transport $transport, array $params)
58
    {
59
        $this->mailer = new Swift_Mailer($transport);
60
        if (!empty($params['swift_plugins'])) {
61
            foreach ($params['swift_plugins'] as $plugin) {
62
                $this->mailer->registerPlugin($plugin);
63
            }
64
        }
65
    }
66
67 5
    private static function _load_backend(string $backend, array $params) : self
68
    {
69 5
        $default_params = midcom_baseclasses_components_configuration::get('org.openpsa.mail', 'config')->get($backend . '_params');
70 5
        if (is_array($default_params)) {
71
            $params = array_merge($default_params, $params);
72
        }
73 5
        $classname = 'org_openpsa_mail_backend_' . $backend;
74 5
        return new $classname($params);
75
    }
76
77 4
    final public function send(org_openpsa_mail_message $message)
78
    {
79
        try {
80 4
            $ret = $this->mail($message);
81 4
            $this->error = null;
82 4
            return $ret;
83
        } catch (Exception $e) {
84
            $this->error = $e->getMessage();
85
            return false;
86
        }
87
    }
88
89
    public function get_error_message() : string
90
    {
91
        if ($this->error === null) {
92
            return '';
93
        }
94
95
        return $this->error ?: 'Unknown error';
96
    }
97
}
98