Code Coverage  | 
      ||||||||||
Lines  | 
       Functions and Methods  | 
       Classes and Traits  | 
      ||||||||
| Total |         | 
       100.00%  | 
       55 / 55  | 
               | 
       100.00%  | 
       6 / 6  | 
       CRAP |         | 
       100.00%  | 
       1 / 1  | 
      
| QuestionsController |         | 
       100.00%  | 
       55 / 55  | 
               | 
       100.00%  | 
       6 / 6  | 
       8 |         | 
       100.00%  | 
       1 / 1  | 
      
| __construct |         | 
       100.00%  | 
       1 / 1  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| index |         | 
       100.00%  | 
       9 / 9  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| delete |         | 
       100.00%  | 
       9 / 9  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| edit |         | 
       100.00%  | 
       3 / 3  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| save |         | 
       100.00%  | 
       29 / 29  | 
               | 
       100.00%  | 
       1 / 1  | 
       3 | |||
| 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 Carbon\Carbon; | 
| 8 | use Engelsystem\Controllers\BaseController; | 
| 9 | use Engelsystem\Controllers\HasUserNotifications; | 
| 10 | use Engelsystem\Helpers\Authenticator; | 
| 11 | use Engelsystem\Http\Redirector; | 
| 12 | use Engelsystem\Http\Request; | 
| 13 | use Engelsystem\Http\Response; | 
| 14 | use Engelsystem\Models\Question; | 
| 15 | use Psr\Log\LoggerInterface; | 
| 16 | |
| 17 | class QuestionsController extends BaseController | 
| 18 | { | 
| 19 | use HasUserNotifications; | 
| 20 | |
| 21 | /** @var array<string> */ | 
| 22 | protected array $permissions = [ | 
| 23 | 'question.add', | 
| 24 | 'question.edit', | 
| 25 | ]; | 
| 26 | |
| 27 | public function __construct( | 
| 28 | protected Authenticator $auth, | 
| 29 | protected LoggerInterface $log, | 
| 30 | protected Question $question, | 
| 31 | protected Redirector $redirect, | 
| 32 | protected Response $response | 
| 33 | ) { | 
| 34 | } | 
| 35 | |
| 36 | public function index(): Response | 
| 37 | { | 
| 38 | $questions = $this->question | 
| 39 | ->orderBy('answered_at') | 
| 40 | ->orderByDesc('created_at') | 
| 41 | ->get() | 
| 42 | ->load(['user.state', 'answerer.state']); | 
| 43 | |
| 44 | return $this->response->withView( | 
| 45 | 'pages/questions/index.twig', | 
| 46 | ['questions' => $questions, 'is_admin' => true] | 
| 47 | ); | 
| 48 | } | 
| 49 | |
| 50 | public function delete(Request $request): Response | 
| 51 | { | 
| 52 | $data = $this->validate($request, [ | 
| 53 | 'id' => 'required|int', | 
| 54 | 'delete' => 'checked', | 
| 55 | ]); | 
| 56 | |
| 57 | $question = $this->question->findOrFail($data['id']); | 
| 58 | $question->delete(); | 
| 59 | |
| 60 | $this->log->info('Deleted question {question} ({id})', ['question' => $question->text, 'id' => $question->id]); | 
| 61 | $this->addNotification('question.delete.success'); | 
| 62 | |
| 63 | return $this->redirect->to('/admin/questions'); | 
| 64 | } | 
| 65 | |
| 66 | public function edit(Request $request): Response | 
| 67 | { | 
| 68 | $questionId = (int) $request->getAttribute('question_id'); | 
| 69 | |
| 70 | $questions = $this->question->findOrFail($questionId); | 
| 71 | |
| 72 | return $this->showEdit($questions); | 
| 73 | } | 
| 74 | |
| 75 | public function save(Request $request): Response | 
| 76 | { | 
| 77 | $questionId = (int) $request->getAttribute('question_id'); | 
| 78 | |
| 79 | /** @var Question $question */ | 
| 80 | $question = $this->question->findOrFail($questionId); | 
| 81 | |
| 82 | $data = $this->validate($request, [ | 
| 83 | 'text' => 'required', | 
| 84 | 'answer' => 'required', | 
| 85 | 'delete' => 'optional|checked', | 
| 86 | 'preview' => 'optional|checked', | 
| 87 | ]); | 
| 88 | |
| 89 | if (!is_null($data['delete'])) { | 
| 90 | $question->delete(); | 
| 91 | |
| 92 | $this->log->info( | 
| 93 | 'Deleted question {question} ({id})', | 
| 94 | ['question' => $question->text, 'id' => $question->id] | 
| 95 | ); | 
| 96 | |
| 97 | $this->addNotification('question.delete.success'); | 
| 98 | |
| 99 | return $this->redirect->to('/admin/questions'); | 
| 100 | } | 
| 101 | |
| 102 | $question->text = $data['text']; | 
| 103 | $question->answer = $data['answer']; | 
| 104 | $question->answered_at = Carbon::now(); | 
| 105 | $question->answerer()->associate($this->auth->user()); | 
| 106 | |
| 107 | if (!is_null($data['preview'])) { | 
| 108 | return $this->showEdit($question); | 
| 109 | } | 
| 110 | |
| 111 | $question->save(); | 
| 112 | |
| 113 | $this->log->info( | 
| 114 | 'Saved questions "{text}" ({id}): {answer}', | 
| 115 | ['text' => $question->text, 'answer' => $question->answer, 'id' => $question->id] | 
| 116 | ); | 
| 117 | |
| 118 | $this->addNotification('question.edit.success'); | 
| 119 | |
| 120 | return $this->redirect->to('/admin/questions'); | 
| 121 | } | 
| 122 | |
| 123 | protected function showEdit(?Question $question): Response | 
| 124 | { | 
| 125 | return $this->response->withView( | 
| 126 | 'pages/questions/edit.twig', | 
| 127 | ['question' => $question, 'is_admin' => true] | 
| 128 | ); | 
| 129 | } | 
| 130 | } |