Passed
Push — master ( 06a78d...347ff2 )
by William
03:04
created

MailerComponent   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 95.45%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 21
c 3
b 0
f 1
dl 0
loc 40
ccs 21
cts 22
cp 0.9545
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A sendReportMail() 0 31 4
1
<?php
2
3
/**
4
 * Mailer component handling sending of report notification mails
5
 *
6
 * phpMyAdmin Error reporting server
7
 * Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
8
 *
9
 * Licensed under The MIT License
10
 * For full copyright and license information, please see the LICENSE.txt
11
 * Redistributions of files must retain the above copyright notice.
12
 *
13
 * @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
14
 * @license   https://opensource.org/licenses/mit-license.php MIT License
15
 *
16
 * @see      https://www.phpmyadmin.net/
17
 */
18
19
namespace App\Controller\Component;
20
21
use Cake\Controller\Component;
22
use Cake\Core\Configure;
23
use Cake\Mailer\Mailer;
24
use function sprintf;
25
26
/**
27
 * Mailer component handling report notification emails.
28
 */
29
class MailerComponent extends Component
30
{
31
    /**
32
     * Send an email about the report to configured Email
33
     *
34
     * @param array $viewVars Array of View Variables
35
     *
36
     * @return bool if Email was sent
37
     */
38 7
    public function sendReportMail(array $viewVars): bool
39
    {
40 7
        $email = new Mailer('default');
41 7
        $emailTo = Configure::read('NotificationEmailsTo');
42 7
        $emailFrom = Configure::read('NotificationEmailsFrom');
43 7
        $emailTransport = Configure::read('NotificationEmailsTransport');
44
45 7
        if ($emailTransport === null) {
46
            return false;
47
        }
48
49 7
        if (empty($emailFrom) || empty($emailTo)) {
50 7
            return false;
51
        }
52 4
        $email->viewBuilder()->setLayout('default');
53 4
        $email->viewBuilder()->setTemplate('report');
54
55 4
        $email->setTransport($emailTransport)
56 4
            ->setViewVars($viewVars)
57 4
            ->setSubject(
58 4
                sprintf(
59 2
                    'A new report has been submitted on the Error Reporting Server: %s',
60 4
                    $viewVars['report']['id']
61
                )
62
            )
63 4
            ->setEmailFormat('html')
64 4
            ->setTo($emailTo)
65 4
            ->setFrom($emailFrom)
66 4
            ->send();
67
68 4
        return true;
69
    }
70
}
71