Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
30 / 30 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
FaqController | |
100.00% |
30 / 30 |
|
100.00% |
5 / 5 |
7 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
edit | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
save | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
3 | |||
delete | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
showEdit | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Engelsystem\Controllers\Admin; |
6 | |
7 | use Engelsystem\Controllers\BaseController; |
8 | use Engelsystem\Controllers\HasUserNotifications; |
9 | use Engelsystem\Http\Redirector; |
10 | use Engelsystem\Http\Request; |
11 | use Engelsystem\Http\Response; |
12 | use Engelsystem\Models\Faq; |
13 | use Psr\Log\LoggerInterface; |
14 | |
15 | class FaqController extends BaseController |
16 | { |
17 | use HasUserNotifications; |
18 | |
19 | /** @var array<string> */ |
20 | protected array $permissions = [ |
21 | 'faq.view', |
22 | 'faq.edit', |
23 | ]; |
24 | |
25 | public function __construct( |
26 | protected LoggerInterface $log, |
27 | protected Faq $faq, |
28 | protected Redirector $redirect, |
29 | protected Response $response |
30 | ) { |
31 | } |
32 | |
33 | public function edit(Request $request): Response |
34 | { |
35 | $faqId = $request->getAttribute('faq_id'); // optional |
36 | |
37 | $faq = $this->faq->find($faqId); |
38 | |
39 | return $this->showEdit($faq); |
40 | } |
41 | |
42 | public function save(Request $request): Response |
43 | { |
44 | $faqId = $request->getAttribute('faq_id'); // optional |
45 | |
46 | /** @var Faq $faq */ |
47 | $faq = $this->faq->findOrNew($faqId); |
48 | |
49 | if ($request->request->has('delete')) { |
50 | return $this->delete($faq); |
51 | } |
52 | |
53 | $data = $this->validate($request, [ |
54 | 'question' => 'required|max:255', |
55 | 'text' => 'required', |
56 | 'delete' => 'optional|checked', |
57 | 'preview' => 'optional|checked', |
58 | ]); |
59 | |
60 | $faq->question = $data['question']; |
61 | $faq->text = $data['text']; |
62 | |
63 | if (!is_null($data['preview'])) { |
64 | return $this->showEdit($faq); |
65 | } |
66 | |
67 | $faq->save(); |
68 | |
69 | $this->log->info('Updated faq "{question}": {text}', ['question' => $faq->question, 'text' => $faq->text]); |
70 | |
71 | $this->addNotification('faq.edit.success'); |
72 | |
73 | return $this->redirect->to('/faq#faq-' . $faq->id); |
74 | } |
75 | |
76 | protected function delete(Faq $faq): Response |
77 | { |
78 | $faq->delete(); |
79 | |
80 | $this->log->info('Deleted faq "{question}"', ['question' => $faq->question]); |
81 | |
82 | $this->addNotification('faq.delete.success'); |
83 | |
84 | return $this->redirect->to('/faq'); |
85 | } |
86 | |
87 | protected function showEdit(?Faq $faq): Response |
88 | { |
89 | return $this->response->withView( |
90 | 'pages/faq/edit.twig', |
91 | ['faq' => $faq] |
92 | ); |
93 | } |
94 | } |