Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
50.00% |
1 / 2 |
CRAP | |
23.08% |
6 / 26 |
| Ocr | |
0.00% |
0 / 1 |
|
50.00% |
1 / 2 |
11.28 | |
23.08% |
6 / 26 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
6 / 6 |
|||
| handle | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 20 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\Ocr\Jobs; |
| 4 | |
| 5 | use Illuminate\Bus\Queueable; |
| 6 | use Illuminate\Queue\SerializesModels; |
| 7 | use Illuminate\Queue\InteractsWithQueue; |
| 8 | use Illuminate\Contracts\Queue\ShouldQueue; |
| 9 | use Illuminate\Foundation\Bus\Dispatchable; |
| 10 | use Illuminate\Support\Facades\Log; |
| 11 | use Qmp\Laravel\Ocr\Models\Ocr as OcrModel; |
| 12 | use Qmp\Laravel\CommandsLaravel\Middleware\Library\Monitoring\CreateParentProcess; |
| 13 | use Qmp\Laravel\CommandsLaravel\Middleware\Library\Monitoring\UpdateParentProcess; |
| 14 | use Qmp\Laravel\Ocr\Exceptions\OcrException; |
| 15 | use Qmp\Laravel\Ocr\Ocr\Config; |
| 16 | use Qmp\Laravel\Ocr\Ocr\Providers\ProviderInterface; |
| 17 | use Qmp\Laravel\ToolsLaravel\Traits\Timer; |
| 18 | |
| 19 | class Ocr extends \Jobiden implements ShouldQueue |
| 20 | { |
| 21 | use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Timer; |
| 22 | |
| 23 | /** |
| 24 | * Undocumented variable |
| 25 | * |
| 26 | * @var integer |
| 27 | */ |
| 28 | public $tries = 3; |
| 29 | |
| 30 | /** |
| 31 | * Undocumented variable |
| 32 | * |
| 33 | * @var [type] |
| 34 | */ |
| 35 | protected $ocr; |
| 36 | |
| 37 | /** |
| 38 | * Undocumented variable |
| 39 | * |
| 40 | * @var [type] |
| 41 | */ |
| 42 | protected $provider; |
| 43 | |
| 44 | /** |
| 45 | * Undocumented variable |
| 46 | * |
| 47 | * @var [type] |
| 48 | */ |
| 49 | protected $config; |
| 50 | |
| 51 | //public $timeout = 60; |
| 52 | |
| 53 | |
| 54 | /** |
| 55 | * Create a new job instance. |
| 56 | * |
| 57 | * @return void |
| 58 | */ |
| 59 | public function __construct(Config $config, int $ocrId) |
| 60 | { |
| 61 | Log::debug('in construct'); |
| 62 | $this->config = $config; |
| 63 | $this->ocr = OcrModel::findOrFail($ocrId); |
| 64 | $this->middleware = [ |
| 65 | CreateParentProcess::class, |
| 66 | UpdateParentProcess::class |
| 67 | ]; |
| 68 | |
| 69 | $this->onConnection('ocr'); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Execute the job. |
| 74 | * |
| 75 | * @return void |
| 76 | */ |
| 77 | public function handle() |
| 78 | { |
| 79 | $this->startTimer('total'); |
| 80 | |
| 81 | Log::debug('in handle in queue'); |
| 82 | $data = $this->middlewareData(UpdateParentProcess::class) |
| 83 | ->transfert(CreateParentProcess::class, ['id']); |
| 84 | try { |
| 85 | $config = resolve(Config::class); |
| 86 | $config->setData($this->config->getData()); |
| 87 | |
| 88 | $this->provider = resolve(ProviderInterface::class); |
| 89 | $value = $this->provider->retrieveResponse($this->ocr); |
| 90 | |
| 91 | $this->ocr->update([ |
| 92 | 'response' => $value, |
| 93 | 'succeeded' => true |
| 94 | ]); |
| 95 | $this->provider->sendCallbackResponse($this->ocr); |
| 96 | |
| 97 | $data->log = [ |
| 98 | 'duration' => $this->getTimer('total'), |
| 99 | ]; |
| 100 | } catch (OcrException $e) { |
| 101 | $data->error = $this->reportException($e); |
| 102 | |
| 103 | if ($e->getNeedRealoadJob()) { |
| 104 | $this::dispatch($config, $this->ocr->id); |
| 105 | } else { |
| 106 | $this->ocr->update([ |
| 107 | 'response' => $e->getOriginalMessage(), |
| 108 | 'succeeded' => false |
| 109 | ]); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |