Main   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 0
loc 160
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A hookLibraryList() 0 13 1
A hookRouteList() 0 9 1
A hookMailSend() 0 4 1
C send() 0 44 7
A getMailer() 0 10 2
A setMailer() 0 11 4
1
<?php
2
3
/**
4
 * @package Mail
5
 * @author Iurii Makukh
6
 * @copyright Copyright (c) 2017, Iurii Makukh
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\mail;
11
12
use Exception;
13
use gplcart\core\Library;
14
use gplcart\core\Module;
15
use LogicException;
16
use PHPMailer;
17
18
/**
19
 * Main class for Mail module
20
 */
21
class Main
22
{
23
24
    /**
25
     * Module class instance
26
     * @var \gplcart\core\Module $module
27
     */
28
    protected $module;
29
30
    /**
31
     * Library class instance
32
     * @var \gplcart\core\Library $library
33
     */
34
    protected $library;
35
36
    /**
37
     * @param Module $module
38
     * @param Library $library
39
     */
40
    public function __construct(Module $module, Library $library)
41
    {
42
        $this->module = $module;
43
        $this->library = $library;
44
    }
45
46
    /**
47
     * Implements hook "library.list"
48
     * @param array $libraries
49
     */
50
    public function hookLibraryList(array &$libraries)
51
    {
52
        $libraries['phpmailer'] = array(
53
            'name' => 'PHP Mail', // @text
54
            'description' => 'The classic email sending library for PHP', // @text
55
            'url' => 'https://github.com/PHPMailer/PHPMailer',
56
            'download' => 'https://github.com/PHPMailer/PHPMailer/archive/v5.2.23.zip',
57
            'type' => 'php',
58
            'vendor' => 'phpmailer/phpmailer',
59
            'version' => '5.2.23',
60
            'module' => 'mail'
61
        );
62
    }
63
64
    /**
65
     * Implements hook "route.list"
66
     * @param array $routes
67
     */
68
    public function hookRouteList(array &$routes)
69
    {
70
        $routes['admin/module/settings/mail'] = array(
71
            'access' => 'module_edit',
72
            'handlers' => array(
73
                'controller' => array('gplcart\\modules\\mail\\controllers\\Settings', 'editSettings')
74
            )
75
        );
76
    }
77
78
    /**
79
     * Implements hook "mail.send"
80
     * @param array $to
81
     * @param string $subject
82
     * @param string $message
83
     * @param array $options
84
     * @param mixed $result
85
     */
86
    public function hookMailSend($to, $subject, $message, $options, &$result)
87
    {
88
        $this->setMailer($to, $subject, $message, $options, $result);
89
    }
90
91
    /**
92
     * Send an E-mail
93
     * @param array $to
94
     * @param string $subject
95
     * @param string $message
96
     * @param array $options
97
     * @param array $settings
98
     * @return boolean|string
99
     */
100
    public function send($to, $subject, $message, $options, $settings)
101
    {
102
        try {
103
            $mailer = $this->getMailer();
104
        } catch (Exception $ex) {
105
            return $ex->getMessage();
106
        }
107
108
        $mailer->isSMTP();
109
        $mailer->isHTML(!empty($options['html']));
110
111
        $mailer->Body = $message;
112
        $mailer->Subject = $subject;
113
114
        if (!empty($options['html'])) {
115
            $mailer->AltBody = strip_tags($message);
116
        }
117
118
        $mailer->Port = $settings['port'];
119
        $mailer->setFrom($options['from']);
120
        $mailer->Username = $settings['user'];
121
        $mailer->Password = $settings['password'];
122
        $mailer->SMTPSecure = $settings['secure'];
123
        $mailer->SMTPAuth = !empty($settings['auth']);
124
        $mailer->Host = implode(';', $settings['host']);
125
126
        foreach ($to as $address) {
127
            settype($address, 'array');
128
            call_user_func_array(array($mailer, 'addAddress'), $address);
129
        }
130
131
        if (!empty($options['attachment'])) {
132
            foreach ($options['attachment'] as $attachment) {
133
                settype($attachment, 'array');
134
                call_user_func_array(array($mailer, 'addAttachment'), $attachment);
135
            }
136
        }
137
138
        if ($mailer->send()) {
139
            return true;
140
        }
141
142
        return (string) $mailer->ErrorInfo;
143
    }
144
145
    /**
146
     * Returns PHPMailer instance
147
     * @return PHPMailer
148
     * @throws LogicException
149
     */
150
    public function getMailer()
151
    {
152
        $this->library->load('phpmailer');
153
154
        if (class_exists('PHPMailer')) {
155
            return new PHPMailer;
156
        }
157
158
        throw new LogicException('Class \PHPMailer not found');
159
    }
160
161
    /**
162
     * @param array $to
163
     * @param string $subject
164
     * @param string $message
165
     * @param array $options
166
     * @param mixed $result
167
     */
168
    protected function setMailer($to, $subject, $message, $options, &$result)
169
    {
170
        $settings = $this->module->getSettings('mail');
171
172
        if (!empty($settings['status']) && $result === null) {
173
            $sent = $this->send($to, $subject, $message, $options, $settings);
174
            if ($sent === true) {
175
                $result = true;
176
            }
177
        }
178
    }
179
180
}
181