Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
UserVoucherController
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
4 / 4
8
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
 editVoucher
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
 saveVoucher
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Controllers\Admin;
6
7use Engelsystem\Config\Config;
8use Engelsystem\Controllers\BaseController;
9use Engelsystem\Controllers\HasUserNotifications;
10use Engelsystem\Helpers\Authenticator;
11use Engelsystem\Helpers\UserVouchers;
12use Engelsystem\Http\Exceptions\HttpNotFound;
13use Engelsystem\Http\Redirector;
14use Engelsystem\Http\Request;
15use Engelsystem\Http\Response;
16use Engelsystem\Models\User\State;
17use Engelsystem\Models\User\User;
18use Engelsystem\Models\Worklog;
19use Psr\Log\LoggerInterface;
20
21class UserVoucherController extends BaseController
22{
23    use HasUserNotifications;
24
25    /** @var array<string> */
26    protected array $permissions = [
27        'voucher.edit',
28    ];
29
30    public function __construct(
31        protected Authenticator $auth,
32        protected Config $config,
33        protected LoggerInterface $log,
34        protected Worklog $worklog,
35        protected Redirector $redirect,
36        protected Response $response,
37        protected User $user
38    ) {
39    }
40
41    private function checkActive(): void
42    {
43        if (!config('enable_voucher')) {
44            throw new HttpNotFound();
45        }
46    }
47
48    public function editVoucher(Request $request): Response
49    {
50        $this->checkActive();
51        $userId = (int) $request->getAttribute('user_id');
52
53        /** @var User $user */
54        $user = $this->user->findOrFail($userId);
55
56        return $this->response->withView(
57            'admin/user/edit-voucher.twig',
58            [
59                'userdata' => $user,
60                'gotVoucher' => $user->state->got_voucher ?? 0,
61                'forceActive' => $user->state->force_active && config('enable_force_active'),
62                'forceFood' => $user->state->force_food && config('enable_force_food'),
63                'eligibleVoucherCount' => UserVouchers::eligibleVoucherCount($user),
64            ]
65        );
66    }
67
68    public function saveVoucher(Request $request): Response
69    {
70        $this->checkActive();
71        $userId = (int) $request->getAttribute('user_id');
72        /** @var User $user */
73        $user = $this->user->findOrFail($userId);
74
75        $data = $this->validate($request, [
76            'got_voucher' => 'int|min:0',
77        ]);
78
79        $user->state->got_voucher = (int) $data['got_voucher'];
80        $user->state->save();
81
82        $this->log->info(
83            '{name} ({id}) got {got_voucher} vouchers.',
84            [
85                'name' => $user->name,
86                'id' => $user->id,
87                'got_voucher' => $user->state->got_voucher,
88            ]
89        );
90
91        if (in_array('application/json', $request->getAcceptableContentTypes())) {
92            // This was an async request, send a JSON response.
93            return $this->response
94                ->withHeader('content-type', 'application/json')
95                ->withContent(json_encode([
96                'issued' => $user->state->got_voucher,
97                'eligible' => $user->state->got_voucher + UserVouchers::eligibleVoucherCount($user),
98                'total' => (int) State::query()->sum('got_voucher'),
99            ]));
100        }
101
102        $this->addNotification('voucher.save.success');
103
104        return $this->redirect->to('/users?action=view&user_id=' . $user->id);
105        // TODO Once User_view.php gets removed, change this to withView + getNotifications
106    }
107}