Passed
Push — master ( 379433...409871 )
by Alexey
02:52
created

YandexSpellerController::index()   A

Complexity

Conditions 4
Paths 7

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 21
nc 7
nop 1
dl 0
loc 32
ccs 0
cts 30
cp 0
crap 20
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
namespace CodeblogPro\YandexSpeller\Controller;
4
5
use Illuminate\Routing\Controller;
6
use CodeblogPro\YandexSpeller\Application\Services\YandexSpeller\YandexSpellerService;
7
8
class YandexSpellerController extends Controller
9
{
10
    public function index(?string $sourceString = '')
11
    {
12
        if (!empty($sourceString)) {
13
            try {
14
                $yandexSpellerService = resolve(YandexSpellerService::class);
0 ignored issues
show
Bug introduced by
The function resolve was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

14
                $yandexSpellerService = /** @scrutinizer ignore-call */ resolve(YandexSpellerService::class);
Loading history...
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']);
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        return /** @scrutinizer ignore-call */ response()->json($result['body'], $result['status']);
Loading history...
42
    }
43
44
    public function incorrectMethod()
45
    {
46
        $result = [
47
            'status' => 405,
48
            'body' => ['error' => ['message' => 'Method Not Allowed']]
49
        ];
50
51
        return response()->json($result['body'], $result['status']);
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        return /** @scrutinizer ignore-call */ response()->json($result['body'], $result['status']);
Loading history...
52
    }
53
}
54