diff --git a/composer.json b/composer.json index 080b2ce..0a01c8f 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "require-dev": { "doctrine/dbal": "^3.5", "ergebnis/composer-normalize": "^2.29", + "laravel/pint": "^1.8", "mockery/mockery": "^1.4.4", "nunomaduro/larastan": "^2.5.1", "orchestra/testbench": "^8.0", diff --git a/database/migrations/2017_05_21_232515_create_shareable_links_table.php b/database/migrations/2017_05_21_232515_create_shareable_links_table.php index 356c41d..2cf637e 100644 --- a/database/migrations/2017_05_21_232515_create_shareable_links_table.php +++ b/database/migrations/2017_05_21_232515_create_shareable_links_table.php @@ -1,8 +1,8 @@ route('shareable_link'); - if (!$link->isActive()) { + if (! $link->isActive()) { return redirect(config('shareable-model.redirect_routes.inactive')); } @@ -32,7 +32,7 @@ public function handle(Request $request, Closure $next) return redirect(config('shareable-model.redirect_routes.expired')); } - if ($link->requiresPassword() && !session($link->uuid)) { + if ($link->requiresPassword() && ! session($link->uuid)) { return redirect(url(config('shareable-model.redirect_routes.password_protected'), $link->uuid)); } diff --git a/src/Shareable/Shareable.php b/src/Shareable/Shareable.php index 954a20e..fbd5959 100644 --- a/src/Shareable/Shareable.php +++ b/src/Shareable/Shareable.php @@ -1,9 +1,10 @@ -password); + return ! is_null($this->password); } public function shouldNotify(): bool diff --git a/src/Shareable/ShareableLinkBuilder.php b/src/Shareable/ShareableLinkBuilder.php index 8442e7f..32771dc 100644 --- a/src/Shareable/ShareableLinkBuilder.php +++ b/src/Shareable/ShareableLinkBuilder.php @@ -1,29 +1,31 @@ - $this->expirationDate, 'uuid' => $uuid, 'url' => $this->buildUrl($uuid), - 'should_notify' => $this->shouldNotify + 'should_notify' => $this->shouldNotify, ]); return $this->entity->links()->save($link); @@ -91,7 +93,7 @@ public function build() private function buildUrl(Hexadecimal $uuid): string { - if (!$this->prefix) { + if (! $this->prefix) { return url($this->baseUrl, [$uuid]); } diff --git a/src/ShareableLinkServiceProvider.php b/src/ShareableLinkServiceProvider.php index 2b0c11c..cdb2f40 100644 --- a/src/ShareableLinkServiceProvider.php +++ b/src/ShareableLinkServiceProvider.php @@ -1,19 +1,47 @@ -app->singleton('baseshareablelink', function () { + return new BaseShareableLink(); + }); + + $this->mergeConfigFrom(__DIR__.'/../config/shareable-model.php', 'shareable-model'); + $this->loadRoutesFrom(__DIR__.'/../routes/web.php'); + $this->loadViewsFrom(__DIR__.'/../resources/views', 'shareable-model'); + } + /** * Perform post-registration booting of services. */ public function boot(): void + { + $this->registerRouteModelBinding(); + $this->registerMigrations(); + $this->registerPublishing(); + } + + /** + * Register route binding resolution. + * + * @return void + */ + protected function registerRouteModelBinding() { Route::bind('shareable_link', function ($value) { try { @@ -22,19 +50,33 @@ public function boot(): void throw new ModelNotFoundException($e->getMessage()); } }); + } - $this->publishes([ - __DIR__ . '/../config/shareable-model.php' => config_path('shareable-model.php'), - __DIR__ . '/../resources/views/password.blade.php' => resource_path('views/vendor/shareable-model'), - ]); - - $this->loadMigrationsFrom(__DIR__ . '/../database/migrations'); + /** + * Register migration files. + * + * @return void + */ + protected function registerMigrations() + { + if ($this->app->runningInConsole() && BaseShareableLink::$runsMigrations) { + $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); + } } - public function register(): void + /** + * Register the package's publishable resources. + * + * @return void + */ + protected function registerPublishing() { - $this->mergeConfigFrom(__DIR__ . '/../config/shareable-model.php', 'shareable-model'); - $this->loadRoutesFrom(__DIR__ . '/../routes/web.php'); - $this->loadViewsFrom(__DIR__ . '/../resources/views', 'shareable-model'); + if ($this->app->runningInConsole()) { + $this->publishes([ + __DIR__.'/../database/migrations' => database_path('migrations'), + __DIR__.'/../config/shareable-model.php' => config_path('shareable-model.php'), + __DIR__.'/../resources/views/password.blade.php' => resource_path('views/vendor/shareable-model'), + ]); + } } } diff --git a/tests/Http/Controller/ShareableLinkPasswordControllerTest.php b/tests/Http/Controller/ShareableLinkPasswordControllerTest.php index b4a1098..27958cc 100644 --- a/tests/Http/Controller/ShareableLinkPasswordControllerTest.php +++ b/tests/Http/Controller/ShareableLinkPasswordControllerTest.php @@ -2,9 +2,9 @@ namespace Sassnowski\LaravelShareableModel\Tests\Http\Controller; -use Sassnowski\LaravelShareableModel\Tests\TestCase; -use Sassnowski\LaravelShareableModel\Tests\Models\Upload; use Sassnowski\LaravelShareableModel\Shareable\ShareableLink; +use Sassnowski\LaravelShareableModel\Tests\Models\Upload; +use Sassnowski\LaravelShareableModel\Tests\TestCase; class ShareableLinkPasswordControllerTest extends TestCase { diff --git a/tests/Http/Middleware/ValidateShareableLinkTest.php b/tests/Http/Middleware/ValidateShareableLinkTest.php index 87911cd..c1a8cb0 100644 --- a/tests/Http/Middleware/ValidateShareableLinkTest.php +++ b/tests/Http/Middleware/ValidateShareableLinkTest.php @@ -1,13 +1,15 @@ -