Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
45 / 45 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
UserGoodieController | |
100.00% |
45 / 45 |
|
100.00% |
4 / 4 |
11 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
checkActive | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
editGoodie | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
saveGoodie | |
100.00% |
32 / 32 |
|
100.00% |
1 / 1 |
7 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Engelsystem\Controllers\Admin; |
6 | |
7 | use Engelsystem\Config\Config; |
8 | use Engelsystem\Config\GoodieType; |
9 | use Engelsystem\Controllers\BaseController; |
10 | use Engelsystem\Controllers\HasUserNotifications; |
11 | use Engelsystem\Helpers\Authenticator; |
12 | use Engelsystem\Http\Exceptions\HttpNotFound; |
13 | use Engelsystem\Http\Redirector; |
14 | use Engelsystem\Http\Request; |
15 | use Engelsystem\Http\Response; |
16 | use Engelsystem\Models\User\User; |
17 | use Psr\Log\LoggerInterface; |
18 | |
19 | class UserGoodieController extends BaseController |
20 | { |
21 | use HasUserNotifications; |
22 | |
23 | /** @var array<string, string> */ |
24 | protected array $permissions = [ |
25 | 'editGoodie' => 'user.goodie.edit', |
26 | 'saveGoodie' => 'user.goodie.edit', |
27 | ]; |
28 | |
29 | public function __construct( |
30 | protected Authenticator $auth, |
31 | protected Config $config, |
32 | protected LoggerInterface $log, |
33 | protected Redirector $redirect, |
34 | protected Response $response, |
35 | protected User $user |
36 | ) { |
37 | } |
38 | |
39 | private function checkActive(): void |
40 | { |
41 | if (GoodieType::from(config('goodie_type')) == GoodieType::None) { |
42 | throw new HttpNotFound(); |
43 | } |
44 | } |
45 | |
46 | public function editGoodie(Request $request): Response |
47 | { |
48 | $this->checkActive(); |
49 | $userId = (int) $request->getAttribute('user_id'); |
50 | |
51 | $user = $this->user->findOrFail($userId); |
52 | |
53 | return $this->response->withView( |
54 | 'admin/user/edit-goodie.twig', |
55 | [ |
56 | 'userdata' => $user, |
57 | 'is_tshirt' => $this->config->get('goodie_type') === GoodieType::Tshirt->value, |
58 | ] |
59 | ); |
60 | } |
61 | |
62 | public function saveGoodie(Request $request): Response |
63 | { |
64 | $this->checkActive(); |
65 | $userId = (int) $request->getAttribute('user_id'); |
66 | $shirtEnabled = $this->config->get('goodie_type') === GoodieType::Tshirt->value; |
67 | /** @var User $user */ |
68 | $user = $this->user->findOrFail($userId); |
69 | |
70 | $data = $this->validate($request, [ |
71 | 'shirt_size' => ($shirtEnabled ? 'required' : 'optional') . '|shirt_size', |
72 | 'arrived' => 'optional|checked', |
73 | 'active' => 'optional|checked', |
74 | 'got_goodie' => 'optional|checked', |
75 | ]); |
76 | |
77 | if ($shirtEnabled) { |
78 | $user->personalData->shirt_size = $data['shirt_size']; |
79 | $user->personalData->save(); |
80 | } |
81 | |
82 | if ($this->auth->can('admin_arrive')) { |
83 | $user->state->arrived = (bool) $data['arrived']; |
84 | } |
85 | |
86 | $user->state->active = (bool) $data['active']; |
87 | $user->state->got_goodie = (bool) $data['got_goodie']; |
88 | $user->state->save(); |
89 | |
90 | $this->log->info( |
91 | 'Updated user goodie state "{user}" ({id}): ' |
92 | . '{size}, arrived: {arrived}, active: {active}, got goodie: {got_goodie}', |
93 | [ |
94 | 'id' => $user->id, |
95 | 'user' => $user->name, |
96 | 'size' => $user->personalData->shirt_size, |
97 | 'arrived' => $user->state->arrived ? 'yes' : 'no', |
98 | 'active' => $user->state->active ? 'yes' : 'no', |
99 | 'got_goodie' => $user->state->got_goodie ? 'yes' : 'no', |
100 | ] |
101 | ); |
102 | |
103 | $this->addNotification('user.edit.success'); |
104 | |
105 | return $this->redirect->back(); |
106 | } |
107 | } |