Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 61
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApplicationApiFeedbackLogic
0.00% covered (danger)
0.00%
0 / 61
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 validateImpl
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
6
 executeImpl
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\App\Models\Domain\Api\ApplicationApi;
6
7use Exception;
8use PeServer\App\Models\AppConfiguration;
9use PeServer\App\Models\AppMailer;
10use PeServer\App\Models\AppTemplate;
11use PeServer\App\Models\AuditLog;
12use PeServer\App\Models\Dao\Entities\FeedbacksEntityDao;
13use PeServer\App\Models\Dao\Entities\SequenceEntityDao;
14use PeServer\App\Models\Domain\Api\ApiLogicBase;
15use PeServer\App\Models\ResponseJson;
16use PeServer\Core\Database\IDatabaseContext;
17use PeServer\Core\Mail\Attachment;
18use PeServer\Core\Mail\EmailAddress;
19use PeServer\Core\Mail\EmailMessage;
20use PeServer\Core\Mime;
21use PeServer\Core\Mvc\LogicCallMode;
22use PeServer\Core\Mvc\LogicParameter;
23use PeServer\Core\Throws\NotImplementedException;
24
25class ApplicationApiFeedbackLogic extends ApiLogicBase
26{
27    #region variable
28
29    /**
30     * 要求JSON
31     *
32     * @var array<string,mixed>
33     */
34    private array $requestJson;
35
36    #endregion
37
38    public function __construct(LogicParameter $parameter, private AppConfiguration $config, private AppMailer $mailer, private AppTemplate $appTemplate)
39    {
40        parent::__construct($parameter);
41
42        $this->requestJson = $this->getRequestJson();
43    }
44
45    #region ApiLogicBase
46
47    protected function validateImpl(LogicCallMode $callMode): void
48    {
49        $this->validateJsonProperty($this->requestJson, 'kind', self::VALIDATE_JSON_PROPERTY_NON_EMPTY_TEXT);
50        $this->validateJsonProperty($this->requestJson, 'subject', self::VALIDATE_JSON_PROPERTY_NON_EMPTY_TEXT);
51        $this->validateJsonProperty($this->requestJson, 'content', self::VALIDATE_JSON_PROPERTY_NON_EMPTY_TEXT);
52
53        $this->validateJsonProperty($this->requestJson, 'version', self::VALIDATE_JSON_PROPERTY_NON_EMPTY_TEXT);
54        $this->validateJsonProperty($this->requestJson, 'revision', self::VALIDATE_JSON_PROPERTY_NON_EMPTY_TEXT);
55        $this->validateJsonProperty($this->requestJson, 'build', self::VALIDATE_JSON_PROPERTY_NON_EMPTY_TEXT);
56
57        $this->validateJsonProperty($this->requestJson, 'user_id', self::VALIDATE_JSON_PROPERTY_NON_EMPTY_TEXT);
58        $this->validateJsonProperty($this->requestJson, 'first_execute_timestamp', self::VALIDATE_JSON_PROPERTY_NON_EMPTY_TEXT);
59        $this->validateJsonProperty($this->requestJson, 'first_execute_version', self::VALIDATE_JSON_PROPERTY_NON_EMPTY_TEXT);
60
61        $this->validateJsonProperty($this->requestJson, 'process', self::VALIDATE_JSON_PROPERTY_NON_EMPTY_TEXT);
62        $this->validateJsonProperty($this->requestJson, 'platform', self::VALIDATE_JSON_PROPERTY_NON_EMPTY_TEXT);
63        $this->validateJsonProperty($this->requestJson, 'os', self::VALIDATE_JSON_PROPERTY_NON_EMPTY_TEXT);
64        $this->validateJsonProperty($this->requestJson, 'clr', self::VALIDATE_JSON_PROPERTY_NON_EMPTY_TEXT);
65
66        // 完全に実装ミス
67        if ($this->hasError()) {
68            throw new Exception();
69        }
70    }
71
72    protected function executeImpl(LogicCallMode $callMode): void
73    {
74        $sequence = 0;
75
76        $database = $this->openDatabase();
77        $result = $database->transaction(function (IDatabaseContext $context) use (&$sequence) {
78            $feedbacksEntityDao = new FeedbacksEntityDao($context);
79            $feedbacksEntityDao->insertFeedbacks(
80                $this->stores->special->getServer('REMOTE_ADDR'),
81                $this->requestJson['version'],
82                $this->requestJson['revision'],
83                $this->requestJson['build'],
84                $this->requestJson['user_id'],
85                $this->requestJson['first_execute_timestamp'],
86                $this->requestJson['first_execute_version'],
87                $this->requestJson['process'],
88                $this->requestJson['platform'],
89                $this->requestJson['os'],
90                $this->requestJson['clr'],
91                $this->requestJson['kind'],
92                $this->requestJson['subject'],
93                $this->requestJson['content']
94            );
95
96            $sequenceEntityDao = new SequenceEntityDao($context);
97
98            $sequence = $sequenceEntityDao->getLastSequence();
99
100            $this->setContent(Mime::JSON, [
101                'success' => true,
102                'message' => '',
103            ]);
104
105            return true;
106        });
107
108        if (!$result) {
109            // 完全に実装ミス
110            throw new Exception();
111        }
112
113        // メール送信
114        $feedbackEmails = $this->config->setting->config->address->notify->feedback;
115        $subject = $this->requestJson['subject'];
116        $this->mailer->customSubjectHeader = '[Pe-Feedback]';
117        $this->mailer->subject = "$sequence$subject";
118        $message = $this->appTemplate->createMailTemplate('feedback_email', $this->mailer->subject, $this->requestJson);
119        $this->mailer->setMessage(new EmailMessage($message));
120        $this->mailer->attachments[] = new Attachment("feedback-$sequence.json", $this->getRequestContent(), Mime::JSON);
121        foreach ($feedbackEmails as $feedbackEmail) {
122            $this->mailer->toAddresses = [
123                new EmailAddress($feedbackEmail),
124            ];
125
126            try {
127                $this->mailer->send();
128            } catch (Exception $ex) {
129                // メール送信は開発側の都合なのでエラーならログに記録するのみ
130                $this->logger->error($ex);
131            }
132        }
133    }
134
135    #endregion
136}