Conditions | 4 |
Paths | 7 |
Total Lines | 32 |
Code Lines | 21 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 20 |
Changes | 0 |
1 | <?php |
||
10 | public function index(?string $sourceString = '') |
||
11 | { |
||
12 | if (!empty($sourceString)) { |
||
13 | try { |
||
14 | $yandexSpellerService = resolve(YandexSpellerService::class); |
||
|
|||
15 | $yandexSpellerAnswer = $yandexSpellerService->getAnswerByString($sourceString); |
||
16 | } catch (\Throwable $throwable) { |
||
17 | $yandexSpellerAnswer = null; |
||
18 | } |
||
19 | |||
20 | if (empty($yandexSpellerAnswer)) { |
||
21 | $result = [ |
||
22 | 'status' => 500, |
||
23 | 'body' => ['error' => ['message' => 'Internal error. Please, try again later']] |
||
24 | ]; |
||
25 | } else { |
||
26 | $result = [ |
||
27 | 'status' => 200, |
||
28 | 'body' => [ |
||
29 | 'source_string' => $yandexSpellerAnswer->getSourceString(), |
||
30 | 'corrected_array' => $yandexSpellerAnswer->getCorrectedArray(), |
||
31 | ] |
||
32 | ]; |
||
33 | } |
||
34 | } else { |
||
35 | $result = [ |
||
36 | 'status' => 400, |
||
37 | 'body' => ['error' => ['message' => 'Invalid string']] |
||
38 | ]; |
||
39 | } |
||
40 | |||
41 | return response()->json($result['body'], $result['status']); |
||
42 | } |
||
54 |