• 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

92.65
/App/Controllers/SeglingController.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace App\Controllers;
6

7
use Exception;
8
use App\Services\SeglingService;
9
use App\Services\UrlGeneratorService;
10
use App\Utils\View;
11
use App\Utils\Session;
12
use Psr\Http\Message\ServerRequestInterface;
13
use Psr\Http\Message\ResponseInterface;
14
use Laminas\Diactoros\Response\RedirectResponse;
15
use Laminas\Diactoros\Response\JsonResponse;
16
use Monolog\Logger;
17

18
class SeglingController extends BaseController
19
{
20
    public function __construct(
21
        private SeglingService $seglingService,
22
        private View $view,
23
        UrlGeneratorService $urlGenerator
24
    ) {
25
        $this->urlGenerator = $urlGenerator;
14✔
26
    }
27

28
    /**
29
     * Lists all seglingar (sailing trips).
30
     *
31
     * @return ResponseInterface View response with list of all seglingar
32
     */
33
    public function list(): ResponseInterface
34
    {
35
        $seglingar = $this->seglingService->getAllSeglingar();
1✔
36

37
        $data = [
1✔
38
            "title" => "Bokningslista",
1✔
39
            "newAction" => $this->createUrl('segling-show-create'),
1✔
40
            "items" => $seglingar
1✔
41
        ];
1✔
42
        return $this->view->render('viewSegling', $data);
1✔
43
    }
44

45
    /**
46
     * Displays the edit form for a specific segling (sailing trip).
47
     *
48
     * @param ServerRequestInterface $request The HTTP request
49
     * @param array<string, mixed> $params Route parameters containing segling 'id'
50
     * @return ResponseInterface View response with segling edit form or 404 if not found
51
     */
52
    public function edit(ServerRequestInterface $request, array $params): ResponseInterface
53
    {
54
        $id = (int) $params['id'];
2✔
55
        $formAction = $this->createUrl('segling-save', ['id' => $id]);
2✔
56

57
        try {
58
            $editData = $this->seglingService->getSeglingEditData($id);
2✔
59

60
            $data = [
1✔
61
                "title" => "Visa segling",
1✔
62
                "items" => $editData['segling'],
1✔
63
                "roles" => $editData['roles'],
1✔
64
                "allaSkeppare" => $editData['allaSkeppare'],
1✔
65
                "allaBatsman" => $editData['allaBatsman'],
1✔
66
                "allaKockar" => $editData['allaKockar'],
1✔
67
                "formUrl" => $formAction
1✔
68
            ];
1✔
69
            return $this->view->render('viewSeglingEdit', $data);
1✔
70
        } catch (Exception $e) {
1✔
71
            return $this->notFoundResponse();
1✔
72
        }
73
    }
74

75
    /**
76
     * Saves changes to an existing segling.
77
     *
78
     * @param ServerRequestInterface $request The HTTP request containing form data
79
     * @param array<string, mixed> $params Route parameters containing segling 'id'
80
     * @return ResponseInterface Redirect response on success or JSON error response
81
     */
82
    public function save(ServerRequestInterface $request, array $params): ResponseInterface
83
    {
84
        $id = (int) $params['id'];
2✔
85
        $postData = $this->request->getParsedBody();
2✔
86
        $result = $this->seglingService->updateSegling($id, $postData);
2✔
87

88
        if ($result->success) {
2✔
89
            Session::setFlashMessage('success', $result->message);
1✔
90
            $redirectUrl = $this->createUrl($result->redirectRoute);
1✔
91
            return new RedirectResponse($redirectUrl);
1✔
92
        } else {
93
            return $this->jsonResponse(['success' => false, 'message' => $result->message]);
1✔
94
        }
95
    }
96

97
    /**
98
     * Deletes a segling by ID.
99
     *
100
     * @param ServerRequestInterface $request The HTTP request
101
     * @param array<string, mixed> $params Route parameters containing segling 'id'
102
     * @return ResponseInterface Redirect response with success or error message
103
     */
104
    public function delete(ServerRequestInterface $request, array $params): ResponseInterface
105
    {
106
        $id = (int) $params['id'];
2✔
107
        $result = $this->seglingService->deleteSegling($id);
2✔
108

109
        Session::setFlashMessage($result->success ? 'success' : 'error', $result->message);
2✔
110
        $redirectUrl = $this->createUrl($result->redirectRoute);
2✔
111
        return new RedirectResponse($redirectUrl);
2✔
112
    }
113

114
    /**
115
     * Displays the form for creating a new segling.
116
     *
117
     * @return ResponseInterface View response with new segling creation form
118
     */
119
    public function showCreate(): ResponseInterface
120
    {
121
        $formAction = $this->createUrl('segling-create');
1✔
122
        $data = [
1✔
123
            "title" => "Skapa ny segling",
1✔
124
            "formUrl" => $formAction
1✔
125
        ];
1✔
126
        return $this->view->render('viewSeglingNew', $data);
1✔
127
    }
128

129
    /**
130
     * Creates a new segling from form data.
131
     *
132
     * @return ResponseInterface Redirect response to edit page on success or back to list on error
133
     */
134
    public function create(): ResponseInterface
135
    {
136
        $postData = $this->request->getParsedBody();
2✔
137
        $result = $this->seglingService->createSegling($postData);
2✔
138

139
        Session::setFlashMessage($result->success ? 'success' : 'error', $result->message);
2✔
140

141
        if ($result->success && $result->seglingId) {
2✔
142
            $redirectUrl = $this->createUrl($result->redirectRoute, ['id' => $result->seglingId]);
1✔
143
        } else {
144
            $redirectUrl = $this->createUrl($result->redirectRoute);
1✔
145
        }
146

147
        return new RedirectResponse($redirectUrl);
2✔
148
    }
149

150
    /**
151
     * Adds a member to a segling.
152
     *
153
     * @return ResponseInterface JSON response with success status and message
154
     */
155
    public function saveMedlem(): ResponseInterface
156
    {
157
        $postData = $this->request->getParsedBody();
2✔
158
        $result = $this->seglingService->addMemberToSegling($postData);
2✔
159

160
        return $this->jsonResponse([
2✔
161
            'success' => $result->success,
2✔
162
            'message' => $result->message
2✔
163
        ]);
2✔
164
    }
165

166
    /**
167
     * Removes a member from a segling.
168
     *
169
     * Handles both JSON and form-encoded request bodies.
170
     *
171
     * @return ResponseInterface JSON response with status and error information
172
     */
173
    public function deleteMedlemFromSegling(): ResponseInterface
174
    {
175
        // Handle JSON request body
176
        $contentType = $this->request->getHeaderLine('Content-Type');
2✔
177
        if (strpos($contentType, 'application/json') !== false) {
2✔
178
            $body = $this->request->getBody();
×
179
            if ($body->isSeekable()) {
×
180
                $body->rewind();
×
181
            }
182
            $json = $body->getContents();
×
183
            $data = json_decode($json, true);
×
184
        } else {
185
            $data = $this->request->getParsedBody();
2✔
186
        }
187

188
        $result = $this->seglingService->removeMemberFromSegling($data);
2✔
189

190
        return $this->jsonResponse([
2✔
191
            'status' => $result->success ? 'ok' : 'fail',
2✔
192
            'error' => $result->success ? null : $result->message
2✔
193
        ]);
2✔
194
    }
195
}
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