Skip to content

Commit d0b7761

Browse files
committed
refactoring
1 parent 2c64027 commit d0b7761

File tree

10 files changed

+84
-25
lines changed

10 files changed

+84
-25
lines changed

database/migrations/create_csv_imports_table.php.stub

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ return new class extends Migration
1010
{
1111
Schema::create('csv_imports', function (Blueprint $table) {
1212
$table->id();
13-
$table->nullableMorphs('importable');
14-
$table->foreignId('user_id')->nullable();
13+
$table->foreignId('user_id')->constrained();
14+
$table->string('model');
1515
$table->string('file_path');
1616
$table->string('file_name');
1717
$table->unsignedBigInteger('total_rows');

src/Concerns/HasCsvImports.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
namespace Coderflex\LaravelCsv\Concerns;
44

55
use Coderflex\LaravelCsv\Models\Import;
6-
use Illuminate\Database\Eloquent\Relations\MorphMany;
6+
use Illuminate\Database\Eloquent\Relations\HasMany;
77

88
trait HasCsvImports
99
{
1010
/**
1111
* Has imports relationship
1212
*
13-
* @return \Illuminate\Database\Eloquent\Relations\MorphMany<Import>
13+
* @return \Illuminate\Database\Eloquent\Relations\HasMany<Import>
1414
*/
15-
public function imports(): MorphMany
15+
public function imports(): HasMany
1616
{
17-
return $this->morphMany(Import::class, 'importable');
17+
return $this->hasMany(Import::class, 'user_id');
1818
}
1919
}

src/Http/Livewire/CsvImporter.php

+14-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Coderflex\LaravelCsv\Concerns;
66
use Coderflex\LaravelCsv\Jobs\ImportCsv;
77
use Coderflex\LaravelCsv\Utilities\ChunkIterator;
8+
use Illuminate\Support\Facades\Auth;
89
use Illuminate\Support\Facades\Bus;
910
use Illuminate\Support\MessageBag;
1011
use Illuminate\Validation\Validator;
@@ -108,16 +109,6 @@ protected function setCsvProperties()
108109
[$this->fileHeaders, $this->fileRowCount] = $this->handleCsvProperties();
109110
}
110111

111-
protected function createNewImport()
112-
{
113-
return (new $this->model)->imports()->create([
114-
'user_id' => auth()->id() ?? null,
115-
'file_path' => $this->file->getRealPath(),
116-
'file_name' => $this->file->getClientOriginalName(),
117-
'total_rows' => $this->fileRowCount,
118-
]);
119-
}
120-
121112
protected function importCsv()
122113
{
123114
$import = $this->createNewImport();
@@ -139,4 +130,17 @@ protected function importCsv()
139130
fn () => $import->touch('compoleted_at')
140131
)->dispatch();
141132
}
133+
134+
protected function createNewImport()
135+
{
136+
/** @var \Illuminate\Foundation\Auth\User */
137+
$user = auth()->user();
138+
139+
return $user->imports()->create([
140+
'model' => $this->model,
141+
'file_path' => $this->file->getRealPath(),
142+
'file_name' => $this->file->getClientOriginalName(),
143+
'total_rows' => $this->fileRowCount,
144+
]);
145+
}
142146
}

src/Scopes/ImportScope.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function scopeNotCompleted(Builder $builder): Builder
3535
*/
3636
public function scopeForModel(Builder $builder, string $model): Builder
3737
{
38-
return $builder->where('importable_type', $model);
38+
return $builder->where('model', $model);
3939
}
4040

4141
/**

tests/CsvImporterTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Coderflex\LaravelCsv\Http\Livewire\CsvImporter;
44
use Coderflex\LaravelCsv\Models\Import;
55
use Coderflex\LaravelCsv\Tests\Models\Customer;
6+
use Coderflex\LaravelCsv\Tests\Models\User;
67
use Illuminate\Bus\PendingBatch;
78
use Illuminate\Http\UploadedFile;
89
use Illuminate\Support\Facades\Bus;
@@ -223,6 +224,8 @@
223224
});
224225

225226
it('ensures the imports is batched', function () {
227+
$this->actingAs(User::factory()->create());
228+
226229
Storage::fake('documents');
227230
Bus::fake();
228231

@@ -258,6 +261,8 @@
258261
});
259262

260263
it('creates customers records on top of csv file', function () {
264+
$this->actingAs(User::factory()->create());
265+
261266
$file = UploadedFile::fake()
262267
->createWithContent(
263268
'customers.csv',
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelCsv\Tests\Database\Factories;
4+
5+
use Coderflex\LaravelCsv\Tests\Models\User;
6+
7+
class UserFactory extends \Orchestra\Testbench\Factories\UserFactory
8+
{
9+
protected $model = User::class;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class() extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::create('users', function (Blueprint $table) {
12+
$table->id();
13+
$table->string('name');
14+
$table->string('email')->unique();
15+
$table->timestamp('email_verified_at')->nullable();
16+
$table->string('password');
17+
$table->rememberToken();
18+
$table->timestamps();
19+
});
20+
}
21+
};

tests/Models/Customer.php

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
class Customer extends Model
1111
{
1212
use HasFactory;
13-
use HasCsvImports;
1413

1514
protected $guarded = [];
1615

tests/Models/User.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelCsv\Tests\Models;
4+
5+
use Coderflex\LaravelCsv\Concerns\HasCsvImports;
6+
use Coderflex\LaravelCsv\Tests\Database\Factories\UserFactory;
7+
use Illuminate\Database\Eloquent\Factories\HasFactory;
8+
9+
class User extends \Illuminate\Foundation\Auth\User
10+
{
11+
use HasFactory;
12+
use HasCsvImports;
13+
14+
protected $guarded = [];
15+
16+
protected static function newFactory()
17+
{
18+
return UserFactory::new();
19+
}
20+
}

tests/TestCase.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ public function getEnvironmentSetUp($app)
3737
{
3838
config()->set('database.default', 'testing');
3939

40-
$migration = include __DIR__.'/../database/migrations/create_csv_imports_table.php.stub';
41-
$migration->up();
42-
43-
$migration = include __DIR__.'/Database/Migrations/create_customers_table.php';
44-
$migration->up();
40+
$migrations = [
41+
include __DIR__ . '/../database/migrations/create_csv_imports_table.php.stub',
42+
include __DIR__ . '/Database/Migrations/create_customers_table.php',
43+
include __DIR__ . '/Database/Migrations/create_users_table.php',
44+
include __DIR__ . '/Database/Migrations/create_job_batches_table.php',
45+
];
4546

46-
$migration = include __DIR__.'/Database/Migrations/create_job_batches_table.php';
47-
$migration->up();
47+
collect($migrations)->each(fn ($path) => $path->up());
4848
}
4949

5050
public function registerLivewireComponents()

0 commit comments

Comments
 (0)