Completed
Push — master ( e476df...2c506c )
by Iurii
01:39
created

Main::hookModuleEnableAfter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
    gplcart\core\Module;
15
use gplcart\core\exceptions\Dependency as DependencyException;
16
17
/**
18
 * Main class for Mail module
19
 */
20
class Main
21
{
22
23
    /**
24
     * Module class instance
25
     * @var \gplcart\core\Module $module
26
     */
27
    protected $module;
28
29
    /**
30
     * Library class instance
31
     * @var \gplcart\core\Library $library
32
     */
33
    protected $library;
34
35
    /**
36
     * @param Module $module
37
     * @param Library $library
38
     */
39
    public function __construct(Module $module, Library $library)
40
    {
41
        $this->module = $module;
42
        $this->library = $library;
43
    }
44
45
    /**
46
     * Implements hook "library.list"
47
     * @param array $libraries
48
     */
49
    public function hookLibraryList(array &$libraries)
50
    {
51
        $libraries['phpmailer'] = array(
52
            'name' => /* @text */'PHP Mail',
53
            'description' => /* @text */'The classic email sending library for PHP',
54
            'url' => 'https://github.com/PHPMailer/PHPMailer',
55
            'download' => 'https://github.com/PHPMailer/PHPMailer/archive/v5.2.23.zip',
56
            'type' => 'php',
57
            'version_source' => array(
58
                'lines' => 100,
59
                'pattern' => '/.*\\$Version.*(\\d+\\.+\\d+\\.+\\d+)/',
60
                'file' => 'vendor/phpmailer/phpmailer/class.phpmailer.php'
61
            ),
62
            'module' => 'mail',
63
            'files' => array(
64
                'vendor/autoload.php'
65
            )
66
        );
67
    }
68
69
    /**
70
     * Implements hook "route.list"
71
     * @param array $routes
72
     */
73
    public function hookRouteList(array &$routes)
74
    {
75
        $routes['admin/module/settings/mail'] = array(
76
            'access' => 'module_edit',
77
            'handlers' => array(
78
                'controller' => array('gplcart\\modules\\mail\\controllers\\Settings', 'editSettings')
79
            )
80
        );
81
    }
82
83
    /**
84
     * Implements hook "mail.send"
85
     * @param array $to
86
     * @param string $subject
87
     * @param string $message
88
     * @param array $options
89
     * @param mixed $result
90
     */
91
    public function hookMailSend($to, $subject, $message, $options, &$result)
92
    {
93
        $this->setMailer($to, $subject, $message, $options, $result);
94
    }
95
96
    /**
97
     * Implements hook "module.enable.after"
98
     */
99
    public function hookModuleEnableAfter()
100
    {
101
        $this->library->clearCache();
102
    }
103
104
    /**
105
     * Implements hook "module.disable.after"
106
     */
107
    public function hookModuleDisableAfter()
108
    {
109
        $this->library->clearCache();
110
    }
111
112
    /**
113
     * Implements hook "module.install.after"
114
     */
115
    public function hookModuleInstallAfter()
116
    {
117
        $this->library->clearCache();
118
    }
119
120
    /**
121
     * Implements hook "module.uninstall.after"
122
     */
123
    public function hookModuleUninstallAfter()
124
    {
125
        $this->library->clearCache();
126
    }
127
128
    /**
129
     * Send an E-mail
130
     * @param array $to
131
     * @param string $subject
132
     * @param string $message
133
     * @param array $options
134
     * @param array $settings
135
     * @return boolean|string
136
     */
137
    public function send($to, $subject, $message, $options, $settings)
138
    {
139
        try {
140
            $mailer = $this->getMailer();
141
        } catch (Exception $ex) {
142
            return $ex->getMessage();
143
        }
144
145
        $mailer->isSMTP();
146
        $mailer->isHTML(!empty($options['html']));
147
148
        $mailer->Body = $message;
149
        $mailer->Subject = $subject;
150
151
        if (!empty($options['html'])) {
152
            $mailer->AltBody = strip_tags($message);
153
        }
154
155
        $mailer->Port = $settings['port'];
156
        $mailer->setFrom($options['from']);
157
        $mailer->Username = $settings['user'];
158
        $mailer->Password = $settings['password'];
159
        $mailer->SMTPSecure = $settings['secure'];
160
        $mailer->SMTPAuth = !empty($settings['auth']);
161
        $mailer->Host = implode(';', $settings['host']);
162
163
        foreach ($to as $address) {
164
            settype($address, 'array');
165
            call_user_func_array(array($mailer, 'addAddress'), $address);
166
        }
167
168
        if (!empty($options['attachment'])) {
169
            foreach ($options['attachment'] as $attachment) {
170
                settype($attachment, 'array');
171
                call_user_func_array(array($mailer, 'addAttachment'), $attachment);
172
            }
173
        }
174
175
        if ($mailer->send()) {
176
            return true;
177
        }
178
179
        return (string) $mailer->ErrorInfo;
180
    }
181
182
    /**
183
     * Returns PHPMailer instance
184
     * @return \PHPMailer
185
     * @throws DependencyException
186
     */
187
    public function getMailer()
188
    {
189
        $this->library->load('phpmailer');
190
191
        if (class_exists('PHPMailer')) {
192
            return new \PHPMailer;
193
        }
194
195
        throw new DependencyException('Class \PHPMailer not found');
196
    }
197
198
    /**
199
     * @param array $to
200
     * @param string $subject
201
     * @param string $message
202
     * @param array $options
203
     * @param mixed $result
204
     */
205
    protected function setMailer($to, $subject, $message, $options, &$result)
206
    {
207
        $settings = $this->module->getSettings('mail');
208
209
        if (!empty($settings['status']) && $result === null) {
210
            $sent = $this->send($to, $subject, $message, $options, $settings);
211
            if ($sent === true) {
212
                $result = true;
213
            }
214
        }
215
    }
216
217
}
218