Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
UserVoucherController
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
4 / 4
7
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%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
 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                'eligibleVoucherCount' => UserVouchers::eligibleVoucherCount($user),
63            ]
64        );
65    }
66
67    public function saveVoucher(Request $request): Response
68    {
69        $this->checkActive();
70        $userId = (int) $request->getAttribute('user_id');
71        /** @var User $user */
72        $user = $this->user->findOrFail($userId);
73
74        $data = $this->validate($request, [
75            'got_voucher' => 'int|min:0',
76        ]);
77
78        $user->state->got_voucher = (int) $data['got_voucher'];
79        $user->state->save();
80
81        $this->log->info(
82            '{name} ({id}) got {got_voucher} vouchers.',
83            [
84                'name' => $user->name,
85                'id' => $user->id,
86                'got_voucher' => $user->state->got_voucher,
87            ]
88        );
89
90        if (in_array('application/json', $request->getAcceptableContentTypes())) {
91            // This was an async request, send a JSON response.
92            return $this->response
93                ->withHeader('content-type', 'application/json')
94                ->withContent(json_encode([
95                'issued' => $user->state->got_voucher,
96                'eligible' => $user->state->got_voucher + UserVouchers::eligibleVoucherCount($user),
97                'total' => (int) State::query()->sum('got_voucher'),
98            ]));
99        }
100
101        $this->addNotification('voucher.save.success');
102
103        return $this->redirect->to('/users?action=view&user_id=' . $user->id);
104        // TODO Once User_view.php gets removed, change this to withView + getNotifications
105    }
106}