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

klinge / sl-webapp / 18972069596

31 Oct 2025 12:08PM UTC coverage: 74.73% (+11.1%) from 63.602%
18972069596

Pull #115

github

web-flow
Merge c283235e4 into 63f7c79ae
Pull Request #115: 106 refactor controllers

677 of 783 new or added lines in 31 files covered. (86.46%)

80 existing lines in 4 files now uncovered.

1662 of 2224 relevant lines covered (74.73%)

3.82 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 $params The route parameters, must contain 'id'
68
     */
69
    public function edit(ServerRequestInterface $request, array $params): ResponseInterface
70
    {
71
        $id = (int) $params['id'];
2✔
72

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

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

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

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

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

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

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

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

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

156
        $result = $this->medlemService->createMember($postData);
1✔
157

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

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

178
        $result = $this->medlemService->deleteMember($id);
1✔
179

180
        if ($result->success) {
1✔
181
            return $this->redirectWithSuccess($result->redirectRoute, $result->message);
1✔
182
        } else {
NEW
183
            return $this->redirectWithError($result->redirectRoute, $result->message);
×
184
        }
185
    }
186
}
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