Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
FaqController
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
4 / 4
5
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
 index
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 tagged
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 showFaqs
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Controllers;
6
7use Engelsystem\Config\Config;
8use Engelsystem\Http\Exceptions\HttpNotFound;
9use Engelsystem\Http\Request;
10use Engelsystem\Http\Response;
11use Engelsystem\Models\Faq;
12use Engelsystem\Models\Tag;
13use Illuminate\Database\Eloquent\Collection;
14
15class FaqController extends BaseController
16{
17    use HasUserNotifications;
18
19    /** @var string[] */
20    protected array $permissions = [
21        'faq.view',
22    ];
23
24    public function __construct(
25        protected Config $config,
26        protected Faq $faq,
27        protected Response $response
28    ) {
29    }
30
31    public function index(): Response
32    {
33        $faq = $this->faq
34            ->with('tags')
35            ->orderBy('question')
36            ->get();
37        $tags = $faq->pluck('tags')
38            ->flatten()
39            ->unique('id')
40            ->sortBy('name');
41
42        return $this->showFaqs($faq, ['tags' => $tags]);
43    }
44
45    public function tagged(Request $request): Response
46    {
47        $tagId = $request->getAttribute('tag_id');
48        $tag = Tag::findOrFail($tagId);
49
50        $faq = $tag->faqs()
51            ->with('tags')
52            ->orderBy('question')
53            ->get();
54        if ($faq->isEmpty()) {
55            throw new HttpNotFound();
56        }
57
58        return $this->showFaqs($faq, ['tag' => $tag]);
59    }
60
61    protected function showFaqs(Collection $faqs, array $data): Response
62    {
63        $text = $this->config->get('faq_text');
64
65        return $this->response->withView(
66            'pages/faq/index',
67            array_merge(['text' => $text, 'items' => $faqs], $data)
68        );
69    }
70}