• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

klinge / sl-webapp / 19065177774

04 Nov 2025 10:12AM UTC coverage: 75.865% (-0.08%) from 75.944%
19065177774

push

github

klinge
Temp reverting back to phpstan lvl 5

1688 of 2225 relevant lines covered (75.87%)

3.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.92
/App/Controllers/MedlemController.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace App\Controllers;
6

7
use Exception;
8
use App\Services\MedlemService;
9
use App\Services\UrlGeneratorService;
10
use App\Utils\View;
11
use App\Traits\ResponseFormatter;
12
use Psr\Http\Message\ServerRequestInterface;
13
use Psr\Http\Message\ResponseInterface;
14
use Monolog\Logger;
15

16
/**
17
 * MedlemController handles operations related to members (medlemmar).
18
 *
19
 * This controller manages CRUD operations for members, including listing,
20
 * editing, creating, and deleting member records. It also handles related
21
 * operations such as managing roles and payments for members.
22
 */
23
class MedlemController extends BaseController
24
{
25
    use ResponseFormatter;
26

27

28

29
    public function __construct(
30
        private MedlemService $medlemService,
31
        private View $view,
32
        UrlGeneratorService $urlGenerator
33
    ) {
34
        $this->urlGenerator = $urlGenerator;
9✔
35
    }
36

37
    /**
38
     * Lists all members.
39
     *
40
     * Fetches all members from the repository and renders them in a view.
41
     */
42
    public function listAll(): ResponseInterface
43
    {
44
        $result = $this->medlemService->getAllMembers();
1✔
45

46
        $data = [
1✔
47
            "title" => "Medlemmar",
1✔
48
            "items" => $result,
1✔
49
            'newAction' => $this->createUrl('medlem-new')
1✔
50
        ];
1✔
51
        return $this->view->render('viewMedlem', $data);
1✔
52
    }
53

54
    public function listJson(): ResponseInterface
55
    {
56
        $result = $this->medlemService->getAllMembers();
1✔
57
        return $this->jsonResponse($result);
1✔
58
    }
59

60
    /**
61
     * Edits a specific member.
62
     *
63
     * Fetches member data, roles, sailings, and payments for the specified member ID
64
     * and renders them in an edit view.
65
     *
66
     * @param ServerRequestInterface $request The request object
67
     * @param array<string, mixed> $params The route parameters, must contain 'id'
68
     * @return ResponseInterface View response with member edit form or error redirect
69
     */
70
    public function edit(ServerRequestInterface $request, array $params): ResponseInterface
71
    {
72
        $id = (int) $params['id'];
2✔
73

74
        try {
75
            $memberData = $this->medlemService->getMemberEditData($id);
2✔
76

77
            $data = [
1✔
78
                "title" => "Visa medlem",
1✔
79
                "items" => $memberData['medlem'],
1✔
80
                "roles" => $memberData['roller'],
1✔
81
                'seglingar' => $memberData['seglingar'],
1✔
82
                'betalningar' => $memberData['betalningar'],
1✔
83
                'formAction' => $this->createUrl('medlem-update', ['id' => $id]),
1✔
84
                'createBetalningAction' => $this->createUrl('betalning-medlem', ['id' => $id]),
1✔
85
                'listBetalningAction' => $this->createUrl('betalning-medlem', ['id' => $id]),
1✔
86
                'deleteAction' => $this->createUrl('medlem-delete')
1✔
87
            ];
1✔
88

89
            return $this->view->render('viewMedlemEdit', $data);
1✔
90
        } catch (Exception $e) {
1✔
91
            return $this->redirectWithError('medlem-list', 'Kunde inte hämta medlem!');
1✔
92
        }
93
    }
94

95
    /**
96
     * Saves changes to a member.
97
     *
98
     * Sanitizes input data, updates the member record, and redirects to the member list.
99
     *
100
     * @param ServerRequestInterface $request The request object
101
     * @param array<string, mixed> $params The route parameters, must contain 'id'
102
     * @return ResponseInterface Redirect response with success or error message
103
     */
104
    public function update(ServerRequestInterface $request, array $params): ResponseInterface
105
    {
106
        $id = (int) $params['id'];
2✔
107
        $postData = $this->request->getParsedBody();
2✔
108

109
        $result = $this->medlemService->updateMember($id, $postData);
2✔
110

111
        if ($result->success) {
2✔
112
            return $this->redirectWithSuccess($result->redirectRoute, $result->message);
1✔
113
        } else {
114
            return $this->redirectWithError($result->redirectRoute, $result->message);
1✔
115
        }
116
    }
117

118
    /**
119
     * Prepares and displays the form for adding a new member.
120
     *
121
     * This method:
122
     * 1. Retrieves all available roles from the Roll model.
123
     * 2. Prepares data for the view
124
     * 3. Renders the 'viewMedlemNew' template with the prepared data.
125
     *
126
     * @return ResponseInterface
127
     */
128
    public function showNewForm(): ResponseInterface
129
    {
130
        $roller = $this->medlemService->getAllRoles();
1✔
131

132
        $data = [
1✔
133
            "title" => "Lägg till medlem",
1✔
134
            "roller" => $roller,
1✔
135
            'formAction' => $this->createUrl('medlem-create')
1✔
136
        ];
1✔
137
        return $this->view->render('viewMedlemNew', $data);
1✔
138
    }
139

140
    /**
141
     * Inserts a new member into the system.
142
     *
143
     * This method handles the creation of a new member based on submitted POST data.
144
     * It performs the following steps:
145
     * 1. Creates a new Medlem object.
146
     * 2. Prepares and sanitizes the submitted member data.
147
     * 3. Attempts to create the new member record in the database.
148
     * 4. Sets a flash message indicating the result of the operation.
149
     * 5. Redirects to the member list page.
150
     *
151
     * @return ResponseInterface
152
     * @throws Exception If there's an error during the member creation process
153
     */
154
    public function create(): ResponseInterface
155
    {
156
        $postData = $this->request->getParsedBody();
1✔
157

158
        $result = $this->medlemService->createMember($postData);
1✔
159

160
        if ($result->success) {
1✔
161
            return $this->redirectWithSuccess($result->redirectRoute, $result->message);
1✔
162
        } else {
163
            return $this->redirectWithError($result->redirectRoute, $result->message);
×
164
        }
165
    }
166

167
    /**
168
     * Deletes a member from the system.
169
     *
170
     * This method handles the deletion of a member based on the provided ID.
171
     * It attempts to delete the member and sets a flash message indicating
172
     * the result of the operation. After deletion, it redirects to the member list.
173
     *
174
     * @throws Exception If there's an error during the deletion process
175
     */
176
    public function delete(): ResponseInterface
177
    {
178
        $id = (int) $this->request->getParsedBody()['id'];
1✔
179

180
        $result = $this->medlemService->deleteMember($id);
1✔
181

182
        if ($result->success) {
1✔
183
            return $this->redirectWithSuccess($result->redirectRoute, $result->message);
1✔
184
        } else {
185
            return $this->redirectWithError($result->redirectRoute, $result->message);
×
186
        }
187
    }
188
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc