Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ManagementMailSendLogic
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 4
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 startup
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 validateImpl
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 executeImpl
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\App\Models\Domain\Page\Management;
6
7use PeServer\App\Models\AppConfiguration;
8use PeServer\App\Models\AppMailer;
9use PeServer\App\Models\AuditLog;
10use PeServer\App\Models\Domain\Page\PageLogicBase;
11use PeServer\Core\IO\File;
12use PeServer\Core\Mail\Attachment;
13use PeServer\Core\Mail\EmailAddress;
14use PeServer\Core\Mail\EmailMessage;
15use PeServer\Core\Mvc\LogicCallMode;
16use PeServer\Core\Mvc\LogicParameter;
17use PeServer\Core\Text;
18
19class ManagementMailSendLogic extends PageLogicBase
20{
21    public function __construct(LogicParameter $parameter, private AppMailer $mailer)
22    {
23        parent::__construct($parameter);
24    }
25
26    protected function startup(LogicCallMode $callMode): void
27    {
28        $this->registerParameterKeys([
29            'mail_subject',
30            'mail_to',
31            'mail_body',
32        ], true);
33    }
34
35    protected function validateImpl(LogicCallMode $callMode): void
36    {
37        if ($callMode === LogicCallMode::Initialize) {
38            return;
39        }
40
41        $this->validation('mail_subject', function (string $key, string $value) {
42            $this->validator->isNotEmpty($key, $value);
43        });
44
45        $this->validation('mail_to', function (string $key, string $value) {
46            $this->validator->isEmail($key, $value);
47        });
48
49        $this->validation('mail_body', function (string $key, string $value) {
50            $this->validator->isNotEmpty($key, $value);
51        });
52    }
53
54    protected function executeImpl(LogicCallMode $callMode): void
55    {
56        if ($callMode === LogicCallMode::Initialize) {
57            return;
58        }
59
60        $mailSubject = $this->getRequest('mail_subject');
61        $mailTo = $this->getRequest('mail_to');
62        $mailBody = $this->getRequest('mail_body');
63
64        $this->mailer->subject = $mailSubject;
65
66        $this->mailer->toAddresses = [
67            new EmailAddress($mailTo),
68        ];
69
70        $this->mailer->setMessage(new EmailMessage(
71            text: $mailBody
72        ));
73
74        $file = $this->getFile('mail_attachment');
75        if ($file->isEnabled()) {
76            $content = File::readContent($file->uploadedFilePath);
77            $this->mailer->attachments = [
78                new Attachment(
79                    Text::isNullOrWhiteSpace($file->originalFileName) ? 'mail_attachment' : $file->originalFileName,
80                    $content,
81                    $file->mime
82                )
83            ];
84        }
85
86        $this->writeAuditLogCurrentUser(AuditLog::ADMINISTRATOR_SEND_EMAIL, [
87            'subject' => $mailSubject,
88            'mailTo' => $mailTo,
89            'body' => $mailBody,
90        ]);
91
92        $this->mailer->send();
93    }
94}