Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (warning)
50.00%
1 / 2
CRAP
12.90% covered (danger)
12.90%
4 / 31
ExecuteCmd
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (warning)
50.00%
1 / 2
62.52
12.90% covered (danger)
12.90%
4 / 31
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 handle
0.00% covered (danger)
0.00%
0 / 1
72
0.00% covered (danger)
0.00%
0 / 27
1<?php
2
3namespace Qmp\Laravel\Ocr\Console;
4
5use Illuminate\Support\Str;
6use Qmp\Laravel\CommandsLaravel\Middleware\Library\Monitoring\CreateParentProcess;
7use Qmp\Laravel\CommandsLaravel\Middleware\Library\Monitoring\UpdateParentProcess;
8use Qmp\Laravel\Ocr\Models\Ocr;
9use Qmp\Laravel\Ocr\Ocr\Config;
10use Qmp\Laravel\Ocr\Ocr\Providers\ProviderInterface;
11use Qmp\Laravel\ToolsLaravel\Traits\Timer;
12
13class ExecuteCmd extends \Commando
14{
15
16    use Timer;
17
18    /**
19     * The name and signature of the console command.
20     *
21     * @var string
22     */
23    protected $signature = 'ocr:execute {--ocr-id= : Id of ocr request}';
24
25    /**
26     * The console command description.
27     *
28     * @var string
29     */
30    protected $description = 'Execute an OCR detection';
31
32    /**
33     * Create a new command instance.
34     *
35     * @return void
36     */
37    public function __construct()
38    {
39        parent::__construct();
40        $this->middleware([
41            CreateParentProcess::class,
42            UpdateParentProcess::class
43        ]);
44    }
45
46    /**
47     * Execute the console command.
48     *
49     * @return mixed
50     */
51    public function handle()
52    {
53        $this->startTimer('total');
54
55        $data = $this->middlewareData(UpdateParentProcess::class)
56            ->transfert(CreateParentProcess::class, ['id']);
57
58        $ocrId = $this->option('ocr-id');
59        if (empty($ocrId)) {
60            $ocrId = $this->ask('Ocr id');
61        }
62
63        $ocr = Ocr::findOrFail($ocrId);
64        $this->info('Retrieve ocr request');
65
66        if ($ocr->succeeded === 0) {
67            $this->info($ocr->response);
68            $resendRequest = Str::lower($this->ask('Do you want to resend request ? [y/n] (default: n)'));
69        }
70
71        if (!isset($resendRequest) || $resendRequest === 'y'|| $resendRequest === 'yes') {
72            $config = resolve(Config::class);
73            $config->setData(['data_type' => $ocr->data_type]);
74
75            $provider = resolve(ProviderInterface::class);
76            $this->info('Resolve provider');
77            $value = $provider->retrieveResponse($ocr);
78            $this->info('Retrieve the response');
79
80            $ocr->update([
81                'response' => $value,
82                'succeeded' => true
83            ]);
84
85            $sendCallback = Str::lower($this->ask('Send callback ? [y/n] (default: n)'));
86            if ($sendCallback === 'y'|| $sendCallback === 'yes') {
87                $provider->sendCallbackResponse($ocr);
88                $this->info('Callback sent');
89            } else {
90                $this->info('Callback ignored');
91            }
92        }
93
94        $data->log['duration'] = $this->getTimer('total');
95
96        return 0;
97    }
98}