Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
80.00% |
8 / 10 |
CRAP | |
81.58% |
31 / 38 |
| OcrController | |
0.00% |
0 / 1 |
|
80.00% |
8 / 10 |
18.81 | |
81.58% |
31 / 38 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| index | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| store | |
100.00% |
1 / 1 |
2 | |
100.00% |
8 / 8 |
|||
| show | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| showResponse | |
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
|||
| evalResponse | |
0.00% |
0 / 1 |
3.04 | |
83.33% |
5 / 6 |
|||
| update | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
| destroy | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| validateData | |
100.00% |
1 / 1 |
2 | |
100.00% |
6 / 6 |
|||
| storeBase46Image | |
0.00% |
0 / 1 |
3.69 | |
25.00% |
2 / 8 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\Ocr\Controllers; |
| 4 | |
| 5 | use Illuminate\Http\Response; |
| 6 | use Illuminate\Http\Request; |
| 7 | use Illuminate\Support\Facades\Storage; |
| 8 | use Qmp\Laravel\MicroService\Controllers\AbstractMicroServiceController; |
| 9 | use Illuminate\Validation\Rule; |
| 10 | use Qmp\Laravel\Ocr\Ocr\Config; |
| 11 | use Illuminate\Support\Str; |
| 12 | |
| 13 | use Qmp\Laravel\Ocr\Models\Ocr; |
| 14 | use Qmp\Laravel\Ocr\Jobs\Ocr as OcrJob; |
| 15 | |
| 16 | class OcrController extends AbstractMicroServiceController |
| 17 | { |
| 18 | public function __construct(Request $request) |
| 19 | { |
| 20 | //$this->middleware('perm:xxx'); |
| 21 | |
| 22 | parent::__construct($request); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Display a listing of the resource. |
| 27 | * |
| 28 | * @return \Illuminate\Http\Response |
| 29 | */ |
| 30 | public function index() |
| 31 | { |
| 32 | return Ocr::all(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Store a newly created resource in storage. |
| 37 | * |
| 38 | * @param \Illuminate\Http\Request $request |
| 39 | * @return \Illuminate\Http\Response |
| 40 | */ |
| 41 | public function store(Request $request, Config $config) |
| 42 | { |
| 43 | $validatedData = $this->validateData($request); |
| 44 | |
| 45 | $this->storeBase46Image($validatedData['img_link']); |
| 46 | |
| 47 | $ocr = Ocr::create([ |
| 48 | 'img_link' => $validatedData['img_link'], |
| 49 | 'data_type' => $validatedData['data_type'], |
| 50 | 'callback_url' => isset($validatedData['callback_url']) ? $validatedData['callback_url'] : null, |
| 51 | ]); |
| 52 | |
| 53 | OcrJob::dispatch($config, $ocr->id); |
| 54 | return response()->json(['status' => 'ok', 'id' => $ocr->id], Response::HTTP_CREATED); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Display the specified resource. |
| 59 | * |
| 60 | * @return \Illuminate\Http\Response |
| 61 | */ |
| 62 | public function show(Ocr $ocr) |
| 63 | { |
| 64 | return $ocr; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Display the specified resource. |
| 69 | * |
| 70 | * @return \Illuminate\Http\Response |
| 71 | */ |
| 72 | public function showResponse(Ocr $ocr) |
| 73 | { |
| 74 | return response()->json(['status' => $ocr->succeeded ? 'ok' : 'ko', 'message' => $ocr->response]); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Display the specified resource. |
| 79 | * |
| 80 | * @return \Illuminate\Http\Response |
| 81 | */ |
| 82 | public function evalResponse(Request $request, Ocr $ocr) |
| 83 | { |
| 84 | if (!$ocr->succeeded) { |
| 85 | return response()->json(['status' => 'ko', 'message' => $ocr->response]); |
| 86 | } |
| 87 | |
| 88 | $validatedData = $request->validate(['messages' => 'required|array']); |
| 89 | foreach ($validatedData['messages'] as &$message) { |
| 90 | $message = Str::contains(Str::lower($ocr->response), Str::lower($message)); |
| 91 | } |
| 92 | |
| 93 | return response()->json(['status' => 'ok', 'message' => $validatedData['messages']]); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Update the specified resource in storage. |
| 98 | * |
| 99 | * @param \Illuminate\Http\Request $request |
| 100 | * @return \Illuminate\Http\Response |
| 101 | */ |
| 102 | public function update(Request $request, Ocr $ocr) |
| 103 | { |
| 104 | $validatedData = $this->validateData($request, $ocr->id); |
| 105 | |
| 106 | $result = $ocr->update($validatedData); |
| 107 | |
| 108 | return response()->json(['status' => $result ? 'ok' : 'ko']); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Remove the specified resource from storage. |
| 113 | * |
| 114 | * @return \Illuminate\Http\Response |
| 115 | */ |
| 116 | public function destroy(Ocr $ocr) |
| 117 | { |
| 118 | $ocr->delete(); |
| 119 | return response()->json(['status' => 'ok'], Response::HTTP_OK); |
| 120 | } |
| 121 | |
| 122 | protected function validateData(Request $request, $id = null) |
| 123 | { |
| 124 | $rules = [ |
| 125 | 'img_link' => 'required|string', |
| 126 | 'data_type' => 'required|string', |
| 127 | 'callback_url' => 'nullable|url', |
| 128 | ]; |
| 129 | |
| 130 | if ($id != null) { |
| 131 | $rules['id'] = [ |
| 132 | 'required', |
| 133 | Rule::in([$id]) |
| 134 | ]; |
| 135 | } |
| 136 | |
| 137 | return $request->validate($rules); |
| 138 | } |
| 139 | |
| 140 | private function storeBase46Image(&$img) |
| 141 | { |
| 142 | if (Str::contains($img, ';base64,')) { |
| 143 | $imgParts = explode(';base64,', $img); |
| 144 | $imgExt = explode('image/', $imgParts[0]); |
| 145 | $imgExt = end($imgExt); |
| 146 | |
| 147 | $filename = uniqid('ocr_') . '.' . $imgExt; |
| 148 | Storage::disk('ocr')->put($filename, base64_decode($imgParts[1])); |
| 149 | $img = Storage::disk('ocr')->path($filename); |
| 150 | } |
| 151 | } |
| 152 | } |