Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
36 / 36 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| TagController | |
100.00% |
36 / 36 |
|
100.00% |
6 / 6 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| list | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| edit | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| save | |
100.00% |
19 / 19 |
|
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\Controllers\NotificationType; |
| 10 | use Engelsystem\Http\Redirector; |
| 11 | use Engelsystem\Http\Request; |
| 12 | use Engelsystem\Http\Response; |
| 13 | use Engelsystem\Models\Tag; |
| 14 | use Psr\Log\LoggerInterface; |
| 15 | |
| 16 | class TagController extends BaseController |
| 17 | { |
| 18 | use HasUserNotifications; |
| 19 | |
| 20 | /** @var array<string> */ |
| 21 | protected array $permissions = [ |
| 22 | 'tag.edit', |
| 23 | ]; |
| 24 | |
| 25 | public function __construct( |
| 26 | protected LoggerInterface $log, |
| 27 | protected Tag $tag, |
| 28 | protected Redirector $redirect, |
| 29 | protected Response $response |
| 30 | ) { |
| 31 | } |
| 32 | |
| 33 | public function list(): Response |
| 34 | { |
| 35 | $items = $this->tag->all(); |
| 36 | return $this->response->withView( |
| 37 | 'pages/tag/index.twig', |
| 38 | ['items' => $items] |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | public function edit(Request $request): Response |
| 43 | { |
| 44 | $tagId = $request->getAttribute('tag_id'); // optional |
| 45 | $tag = $this->tag->find($tagId); |
| 46 | |
| 47 | return $this->showEdit($tag); |
| 48 | } |
| 49 | |
| 50 | public function save(Request $request): Response |
| 51 | { |
| 52 | $tagId = $request->getAttribute('tag_id'); // optional |
| 53 | |
| 54 | /** @var Tag $tag */ |
| 55 | $tag = $this->tag->findOrNew($tagId); |
| 56 | |
| 57 | if ($request->request->has('delete')) { |
| 58 | return $this->delete($tag); |
| 59 | } |
| 60 | |
| 61 | $data = $this->validate($request, [ |
| 62 | 'name' => 'required|max:255', |
| 63 | 'delete' => 'optional|checked', |
| 64 | ]); |
| 65 | |
| 66 | $tag->name = $data['name']; |
| 67 | |
| 68 | if ( |
| 69 | $this->tag |
| 70 | ->where('name', $tag->name) |
| 71 | ->whereNot('id', $tag->id) |
| 72 | ->exists() |
| 73 | ) { |
| 74 | $this->addNotification('tag.edit.duplicate', NotificationType::ERROR); |
| 75 | |
| 76 | return $this->showEdit($tag); |
| 77 | } |
| 78 | |
| 79 | $tag->save(); |
| 80 | |
| 81 | $this->log->info('Saved tag "{name}" ({id})', ['name' => $tag->name, 'id' => $tag->id]); |
| 82 | $this->addNotification('tag.edit.success'); |
| 83 | |
| 84 | return $this->redirect->to('/admin/tags'); |
| 85 | } |
| 86 | |
| 87 | protected function delete(Tag $tag): Response |
| 88 | { |
| 89 | $tag->delete(); |
| 90 | |
| 91 | $this->log->info('Deleted tag "{name}" ({id})', ['name' => $tag->name, 'id' => $tag->id]); |
| 92 | $this->addNotification('tag.delete.success'); |
| 93 | |
| 94 | return $this->redirect->to('/admin/tags'); |
| 95 | } |
| 96 | |
| 97 | protected function showEdit(?Tag $tag): Response |
| 98 | { |
| 99 | return $this->response->withView( |
| 100 | 'pages/tag/edit.twig', |
| 101 | ['tag' => $tag] |
| 102 | ); |
| 103 | } |
| 104 | } |