Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
CRAP | |
58.33% |
7 / 12 |
| GoogleProvider | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
6.81 | |
58.33% |
7 / 12 |
| initClient | |
0.00% |
0 / 1 |
2.26 | |
60.00% |
3 / 5 |
|||
| retrieveResponse | |
0.00% |
0 / 1 |
3.71 | |
57.14% |
4 / 7 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\Ocr\Ocr\Providers\Google; |
| 4 | |
| 5 | use Exception; |
| 6 | use Qmp\Laravel\Ocr\Ocr\Providers\ProviderAbstract; |
| 7 | use Qmp\Laravel\Ocr\Models\Ocr; |
| 8 | |
| 9 | // http://googleapis.github.io/google-cloud-php/#/docs/cloud-vision/v1.5.0/vision/v1/imageannotatorclient |
| 10 | // https://github.com/googleapis/google-cloud-php/blob/master/AUTHENTICATION.md |
| 11 | // https://github.com/googleapis/google-cloud-php-vision |
| 12 | // https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/vision |
| 13 | use Google\Cloud\Vision\V1\ImageAnnotatorClient; |
| 14 | use Qmp\Laravel\Tests\Ocr\Client\Google\GoogleClientFake; |
| 15 | use Qmp\Laravel\Ocr\Exceptions\OcrException; |
| 16 | |
| 17 | class GoogleProvider extends ProviderAbstract |
| 18 | { |
| 19 | private $client; |
| 20 | |
| 21 | protected function initClient() |
| 22 | { |
| 23 | $this->client = config('app.env') === 'testing' |
| 24 | ? new GoogleClientFake() |
| 25 | : new ImageAnnotatorClient([ |
| 26 | 'credentials' => config('ocr.google') |
| 27 | ]); |
| 28 | } |
| 29 | |
| 30 | public function retrieveResponse(Ocr $ocr): string |
| 31 | { |
| 32 | try { |
| 33 | $image = file_get_contents($ocr->img_link); |
| 34 | } catch (Exception $e) { |
| 35 | throw new OcrException("Document doesn't exists, " . $e->getMessage()); |
| 36 | } |
| 37 | |
| 38 | $textAnnotation = $this->client->documentTextDetection($image)->getFullTextAnnotation(); |
| 39 | if (!$textAnnotation) { |
| 40 | throw new OcrException("Document is empty or unreadable"); |
| 41 | } |
| 42 | return $textAnnotation->getText(); |
| 43 | } |
| 44 | } |