Passed
Push — develop ( a56058...ddfce4 )
by Nikolay
04:39
created

SendMailAction::main()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 23
rs 8.8333
cc 7
nc 5
nop 1
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2024 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace MikoPBX\PBXCoreREST\Lib\System;
21
22
use MikoPBX\Core\System\Notifications;
23
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
24
25
/**
26
 *
27
 * @package MikoPBX\PBXCoreREST\Lib\System
28
 */
29
class SendMailAction extends \Phalcon\Di\Injectable
30
{
31
    /**
32
     * Sends an email notification.
33
     *
34
     * @param array $data The data containing email, subject, and body.
35
     * @return PBXApiResult An object containing the result of the API call.
36
     */
37
    public static function main(array $data): PBXApiResult
38
    {
39
        $res            = new PBXApiResult();
40
        $res->processor = __METHOD__;
41
        if (isset($data['email']) && isset($data['subject']) && isset($data['body'])) {
42
            if (isset($data['encode']) && $data['encode'] === 'base64') {
43
                $data['subject'] = base64_decode($data['subject']);
44
                $data['body']    = base64_decode($data['body']);
45
            }
46
            $notifier = new Notifications();
47
            $result   = $notifier->sendMail($data['email'], $data['subject'], $data['body']);
48
            if ($result === true) {
49
                $res->success = true;
50
            } else {
51
                $res->success    = false;
52
                $res->messages[] = 'Notifications::sendMail method returned false';
53
            }
54
        } else {
55
            $res->success    = false;
56
            $res->messages[] = 'Not all query parameters were set';
57
        }
58
59
        return $res;
60
    }
61
}