Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
FaqController
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
5 / 5
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 edit
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 save
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
3
 delete
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 showEdit
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Controllers\Admin;
6
7use Engelsystem\Controllers\BaseController;
8use Engelsystem\Controllers\HasUserNotifications;
9use Engelsystem\Http\Redirector;
10use Engelsystem\Http\Request;
11use Engelsystem\Http\Response;
12use Engelsystem\Models\Faq;
13use Psr\Log\LoggerInterface;
14
15class 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}