Skip to content

Commit

Permalink
Merge pull request #10 from kumasakatakashi/added-service-provider
Browse files Browse the repository at this point in the history
Added service provider
  • Loading branch information
kumasakatakashi committed Jun 25, 2019
2 parents 4fe211c + d8dd962 commit e8e0d6e
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 12 deletions.
17 changes: 13 additions & 4 deletions app/Http/Controllers/ImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@
use Illuminate\Support\Facades\Storage;
use App\Http\Requests\UploadImageRequest;
use App\Http\Requests\DetectImageRequest;
use App\Support\Api\GoogleVisionApi;
use App\Services\OcrService;

class ImageController extends Controller
{
/**
* @var App\Services\OcrServiceContract
*/
protected $ocrService;

public function __construct(OcrService $ocrService)
{
$this->ocrService = $ocrService;
}

public function index()
{
return view('image.index');
Expand Down Expand Up @@ -38,9 +48,8 @@ public function detect(DetectImageRequest $request)
{
$filepath = $request->input('filepath');

// OCR実行 by GoogleVisionAPI
$api = new GoogleVisionApi();
$text = $api->document_text_detection($filepath);
// OCR実行
$text = $this->ocrService->detect($filepath);

return view('image.index')->with([
'filepath' => $filepath,
Expand Down
31 changes: 31 additions & 0 deletions app/Providers/ServiceBindServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ServiceBindServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->app->singleton(
\App\Services\OcrService::class,
\App\Services\Impl\OcrServiceViaGoogle::class
);
}

/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
}
29 changes: 29 additions & 0 deletions app/Services/Impl/OcrServiceViaGoogle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Services\Impl;

use App\Services\OcrService;
use App\Support\Api\GoogleVisionApi;

class OcrServiceViaGoogle implements OcrService
{
/**
* @var App\Support\Api\GoogleVisionApi
*/
private $api;

public function __construct() {
$this->api = new GoogleVisionApi();
}

/**
*
* @see App\Services\OcrServiceContract
* @param string $image_path
* @return string result_text
*/
public function detect(string $image_path) {
$text = $this->api->document_text_detection($image_path);
return $text;
}
}
8 changes: 8 additions & 0 deletions app/Services/OcrService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Services;

interface OcrService
{
public function detect(string $image_path);
}
1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,

App\Providers\ServiceBindServiceProvider::class,
],

/*
Expand Down
24 changes: 16 additions & 8 deletions tests/Http/Controllers/ImageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@ public function testUpload()
public function testDetect()
{
// テスト用にValidator(ファイル存在チェック)をモック化
// inputの結果をstringで何かしら値を返す必要がある。
// detectメソッドのタイプヒンティングに
// stringを指定しており、nullが渡るとエラーになるため
$mock_request = m::mock('overload:\App\Http\Requests\DetectImageRequest');
$mock_request->shouldReceive('input')
->once();

$mock_googleapi = m::mock('overload:\App\Support\Api\GoogleVisionApi');
$mock_googleapi->shouldReceive('document_text_detection')
->once()
->with(\Mockery::any())
->andReturn('test');
$mock_request->shouldReceive('input')->once()->andReturn('/path/test.jpg');

// ダミーのサービスクラスへ切り替え
\App::singleton(\App\Services\OcrService::class, function (){
return new OcrServiceDummy();
});

$response = $this->post('detect', [
'filepath' => '/path/to/test.jpg',
Expand All @@ -60,3 +61,10 @@ public function testDetect()
$response->assertViewHas('result_text');
}
}

class OcrServiceDummy implements \App\Services\OcrService
{
public function detect(string $image_path) {
return 'ダミーです';
}
}

0 comments on commit e8e0d6e

Please sign in to comment.