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