Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ManagementFeedbackDetailLogic
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 3
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
 validateImpl
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 executeImpl
0.00% covered (danger)
0.00%
0 / 31
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 Exception;
8use PeServer\App\Models\AppDatabaseCache;
9use PeServer\App\Models\Dao\Domain\FeedbackDomainDao;
10use PeServer\App\Models\Dao\Entities\FeedbackCommentsEntityDao;
11use PeServer\App\Models\Dao\Entities\FeedbacksEntityDao;
12use PeServer\App\Models\Domain\AppArchiver;
13use PeServer\App\Models\Domain\Page\PageLogicBase;
14use PeServer\Core\Collection\Arr;
15use PeServer\Core\Database\IDatabaseContext;
16use PeServer\Core\Http\HttpStatus;
17use PeServer\Core\Mvc\LogicCallMode;
18use PeServer\Core\Mvc\LogicParameter;
19use PeServer\Core\Throws\HttpStatusException;
20use PeServer\Core\Stopwatch;
21use PeServer\Core\Store\SpecialStore;
22use PeServer\Core\Text;
23
24class ManagementFeedbackDetailLogic extends PageLogicBase
25{
26    public function __construct(LogicParameter $parameter, private SpecialStore $specialStore)
27    {
28        parent::__construct($parameter);
29    }
30
31    protected function validateImpl(LogicCallMode $callMode): void
32    {
33        $this->validation('sequence', function ($key, $value) {
34            $hasSequence = $this->validator->isNotEmpty($key, $value);
35
36            if ($hasSequence) {
37                $database = $this->openDatabase();
38                $feedbacksEntityDao = new FeedbacksEntityDao($database);
39
40                $seq = (int)$value;
41                $exists = $feedbacksEntityDao->selectExistsFeedbacksBySequence($seq);
42                if (!$exists) {
43                    throw new HttpStatusException(HttpStatus::NotFound);
44                }
45            } else {
46                throw new HttpStatusException(HttpStatus::BadRequest);
47            }
48        });
49    }
50
51    protected function executeImpl(LogicCallMode $callMode): void
52    {
53        $sequence = (int)$this->getRequest('sequence');
54        $this->result['sequence'] = $sequence;
55
56        $database = $this->openDatabase();
57        $feedbackDomainDao = new FeedbackDomainDao($database);
58
59        $detail = $feedbackDomainDao->selectFeedbackDetailBySequence($sequence);
60
61        $this->setValue('detail', $detail);
62
63        $this->setValue('developer_title', "[FB:$sequence] (edit title)");
64
65        $content = Text::join(PHP_EOL, Arr::map(Text::splitLines($detail->content), fn($a) => "$a"));
66
67        $body = Text::replaceMap(
68            <<<STR
69{URL}
70
71{VERSION}
72
73{CONTENT}
74
75---
76
77(edit body)
78
79STR,
80            [
81                'URL' => $this->specialStore->getRequestUrl()->toString(),
82                'VERSION' => $detail->version,
83                'CONTENT' => $content
84            ]
85        );
86        $this->setValue('developer_body', $body);
87
88        if ($callMode === LogicCallMode::Initialize) {
89            $this->setValue('developer-comment', $detail->developerComment);
90            return;
91        }
92
93        $developerComment = (string)$this->getRequest('developer-comment');
94
95        $result = $database->transaction(function (IDatabaseContext $context) use ($sequence, $developerComment) {
96            $feedbackCommentsEntityDao = new FeedbackCommentsEntityDao($context);
97
98            if ($feedbackCommentsEntityDao->selectExistsFeedbackCommentsBySequence($sequence)) {
99                $feedbackCommentsEntityDao->updateFeedbackComments($sequence, $developerComment);
100            } else {
101                $feedbackCommentsEntityDao->insertFeedbackComments($sequence, $developerComment);
102            }
103
104            return true;
105        });
106        if (!$result) {
107            throw new HttpStatusException(HttpStatus::InternalServerError);
108        }
109    }
110}