Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat - Ignore Migration #27

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateShareableLinksTable extends Migration
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddShouldNotifyColumnToShareableLinksTable extends Migration
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class RemoveHashColumnFromShareableLinksTable extends Migration
{
Expand Down
25 changes: 25 additions & 0 deletions src/BaseShareableLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Sassnowski\LaravelShareableModel;

class BaseShareableLink
{
/**
* Indicates if migrations will be run.
*
* @var bool
*/
public static $runsMigrations = true;

/**
* Configure to not register its migrations.
*
* @return static
*/
public static function ignoreMigrations()
{
static::$runsMigrations = false;

return new static;
}
}
6 changes: 4 additions & 2 deletions src/Events/LinkWasVisited.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Sassnowski\LaravelShareableModel\Events;

use Sassnowski\LaravelShareableModel\Shareable\ShareableLink;

class LinkWasVisited
{
/** @var ShareableLink */
/** @var ShareableLink */
public $link;

public function __construct(ShareableLink $link)
Expand Down
8 changes: 5 additions & 3 deletions src/Http/Controllers/ShareableLinkPasswordController.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Sassnowski\LaravelShareableModel\Http\Controllers;

use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Sassnowski\LaravelShareableModel\Shareable\ShareableLink;

class ShareableLinkPasswordController
Expand Down
12 changes: 6 additions & 6 deletions src/Http/Middleware/ValidateShareableLink.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Sassnowski\LaravelShareableModel\Http\Middleware;

use Closure;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Http\RedirectResponse;
use Sassnowski\LaravelShareableModel\Events\LinkWasVisited;
use Sassnowski\LaravelShareableModel\Shareable\ShareableLink;

Expand All @@ -14,8 +16,6 @@ class ValidateShareableLink
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
*
* @return RedirectResponse|Response
*/
Expand All @@ -24,15 +24,15 @@ public function handle(Request $request, Closure $next)
/** @var ShareableLink $link */
$link = $request->route('shareable_link');

if (!$link->isActive()) {
if (! $link->isActive()) {
return redirect(config('shareable-model.redirect_routes.inactive'));
}

if ($link->isExpired()) {
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));
}

Expand Down
5 changes: 3 additions & 2 deletions src/Shareable/Shareable.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Sassnowski\LaravelShareableModel\Shareable;

use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;

trait Shareable
{
Expand Down
10 changes: 6 additions & 4 deletions src/Shareable/ShareableLink.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Sassnowski\LaravelShareableModel\Shareable;

Expand All @@ -10,9 +12,9 @@
* @property string|null $password
* @property string $uuid
* @property string $url
* @property boolean $active
* @property bool $active
* @property string|null $expires_at
* @property boolean $should_notify
* @property bool $should_notify
*/
class ShareableLink extends Model
{
Expand Down Expand Up @@ -58,7 +60,7 @@ public function isExpired(): bool

public function requiresPassword(): bool
{
return !is_null($this->password);
return ! is_null($this->password);
}

public function shouldNotify(): bool
Expand Down
20 changes: 11 additions & 9 deletions src/Shareable/ShareableLinkBuilder.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Sassnowski\LaravelShareableModel\Shareable;

use Carbon\Carbon;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Type\Hexadecimal;
use Ramsey\Uuid\Uuid;

class ShareableLinkBuilder
{
/** @var ShareableInterface */
/** @var ShareableInterface */
private $entity;

/** @var string */
/** @var string */
private $prefix = '';

/** @var string|null */
private $password = null;

/** @var bool */
/** @var bool */
private $active = false;

/** @var Carbon|null */
/** @var Carbon|null */
private $expirationDate = null;

/** @var bool */
/** @var bool */
private $shouldNotify = false;

/** @var string */
Expand Down Expand Up @@ -83,15 +85,15 @@ public function build()
'expires_at' => $this->expirationDate,
'uuid' => $uuid,
'url' => $this->buildUrl($uuid),
'should_notify' => $this->shouldNotify
'should_notify' => $this->shouldNotify,
]);

return $this->entity->links()->save($link);
}

private function buildUrl(Hexadecimal $uuid): string
{
if (!$this->prefix) {
if (! $this->prefix) {
return url($this->baseUrl, [$uuid]);
}

Expand Down
68 changes: 55 additions & 13 deletions src/ShareableLinkServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Sassnowski\LaravelShareableModel;

use Illuminate\Support\Facades\Route;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Sassnowski\LaravelShareableModel\Shareable\ShareableLink;

class ShareableLinkServiceProvider extends ServiceProvider
{
/**
* Register the application services.
*/
public function register(): void
{
$this->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 {
Expand All @@ -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'),
]);
}
}
}
4 changes: 2 additions & 2 deletions tests/Http/Controller/ShareableLinkPasswordControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
8 changes: 5 additions & 3 deletions tests/Http/Middleware/ValidateShareableLinkTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Sassnowski\LaravelShareableModel\Tests\Http\Middleware;

use Carbon\Carbon;
use Illuminate\Support\Facades\Event;
use Sassnowski\LaravelShareableModel\Tests\TestCase;
use Sassnowski\LaravelShareableModel\Tests\Models\Upload;
use Sassnowski\LaravelShareableModel\Events\LinkWasVisited;
use Sassnowski\LaravelShareableModel\Shareable\ShareableLink;
use Sassnowski\LaravelShareableModel\Tests\Models\Upload;
use Sassnowski\LaravelShareableModel\Tests\TestCase;

class ValidateShareableLinkTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Shareable/ShareableLinkBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Sassnowski\LaravelShareableModel\Tests\Models;

use Carbon\Carbon;
use Sassnowski\LaravelShareableModel\Tests\TestCase;
use Sassnowski\LaravelShareableModel\Shareable\ShareableLinkBuilder;
use Sassnowski\LaravelShareableModel\Tests\TestCase;

class ShareableLinkBuilderTest extends TestCase
{
Expand Down
Loading