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

eliashaeussler / typo3-solver / 10780070405

09 Sep 2024 07:38PM UTC coverage: 87.159%. Remained the same
10780070405

Pull #258

github

web-flow
[TASK] Update eslint-plugin-sonarjs to v2

| datasource | package               | from  | to    |
| ---------- | --------------------- | ----- | ----- |
| npm        | eslint-plugin-sonarjs | 1.0.4 | 2.0.2 |
Pull Request #258: [TASK] Update eslint-plugin-sonarjs to v2

862 of 989 relevant lines covered (87.16%)

6.71 hits per line

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

33.33
/Classes/Middleware/SolutionMiddleware.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the TYPO3 CMS extension "solver".
7
 *
8
 * Copyright (C) 2023-2024 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\Typo3Solver\Middleware;
25

26
use EliasHaeussler\Typo3Solver\Authentication;
27
use EliasHaeussler\Typo3Solver\Cache;
28
use EliasHaeussler\Typo3Solver\Configuration;
29
use EliasHaeussler\Typo3Solver\Exception;
30
use EliasHaeussler\Typo3Solver\Formatter;
31
use EliasHaeussler\Typo3Solver\Http;
32
use EliasHaeussler\Typo3Solver\ProblemSolving;
33
use EliasHaeussler\Typo3Solver\Utility;
34
use Psr\Http\Message;
35
use Psr\Http\Server;
36
use TYPO3\CMS\Core;
37

38
/**
39
 * SolutionMiddleware
40
 *
41
 * @author Elias Häußler <elias@haeussler.dev>
42
 * @license GPL-2.0-or-later
43
 */
44
final class SolutionMiddleware implements Server\MiddlewareInterface
45
{
46
    public const ROUTE_PATH = '/tx_solver/solution';
47

48
    public function __construct(
4✔
49
        private readonly Configuration\Configuration $configuration,
50
        private readonly Cache\ExceptionsCache $exceptionsCache,
51
        private readonly Formatter\Message\ExceptionStreamFormatter $exceptionFormatter,
52
        private readonly Formatter\WebStreamFormatter $webFormatter,
53
        private readonly Authentication\StreamAuthentication $authentication,
54
    ) {}
4✔
55

56
    public function process(
4✔
57
        Message\ServerRequestInterface $request,
58
        Server\RequestHandlerInterface $handler,
59
    ): Message\ResponseInterface {
60
        // Pass through request if it's not supported
61
        if (!$this->isRequestSupported($request)) {
4✔
62
            return $handler->handle($request);
4✔
63
        }
64

65
        // Create event stream
66
        $eventStream = Http\EventStream::create();
×
67

68
        try {
69
            // Get exception identifier and stream hash
70
            $hash = $request->getQueryParams()['hash'] ?? null;
×
71
            $id = $request->getQueryParams()['exception'] ?? null;
×
72

73
            // Throw exception if stream hash is invalid
74
            if (!\is_string($hash)) {
×
75
                throw Exception\AuthenticationFailureException::create();
×
76
            }
77

78
            // Authenticate with stream hash
79
            $this->authentication->authenticate($hash);
×
80

81
            // Throw exception if exception identifier is invalid
82
            if (!\is_string($id)) {
×
83
                throw Exception\UnrecoverableExceptionException::forMissingIdentifier();
×
84
            }
85

86
            // Restore exception
87
            $exception = $this->exceptionsCache->get($id);
×
88

89
            // Throw exception if original exception cannot be restored
90
            if ($exception === null) {
×
91
                throw Exception\UnrecoverableExceptionException::create($id);
×
92
            }
93

94
            // Create solver
95
            $solver = new ProblemSolving\Solver($this->configuration, $this->webFormatter);
×
96

97
            // Send solution stream
98
            foreach ($solver->solveStreamed($exception) as $solution) {
×
99
                $eventStream->sendMessage('solutionDelta', $solution);
×
100
            }
101
        } catch (\Throwable $e) {
×
102
            $eventStream->sendMessage('solutionError', $this->exceptionFormatter->format($e));
×
103
        } finally {
104
            $eventStream->close('solutionFinished');
×
105
        }
106

107
        return new Core\Http\Response();
×
108
    }
109

110
    private function isRequestSupported(Message\ServerRequestInterface $request): bool
4✔
111
    {
112
        if ($request->getHeader('Accept') !== ['text/event-stream']) {
4✔
113
            return false;
2✔
114
        }
115

116
        return Utility\HttpUtility::uriMatchesRequest(self::ROUTE_PATH, $request);
2✔
117
    }
118
}
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

© 2025 Coveralls, Inc