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(import): dynamic form action #21

Merged
merged 9 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@
"require-dev": {
"laravel/scout": "^10.10",
"nunomaduro/collision": "^8.1",
"nunomaduro/larastan": "^2.0",
"nunomaduro/larastan": "^3.0",
"orchestra/testbench": "^9.0",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan": "^2.0",
"phpunit/phpunit": "^10.1",
"spatie/laravel-ray": "^1.30",
"squizlabs/php_codesniffer": "^3.7",
"vimeo/psalm": "^5.0"
},
Expand Down
25 changes: 8 additions & 17 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,11 @@ includes:
- ./vendor/nunomaduro/larastan/extension.neon

parameters:

paths:
- src/

# Level 9 is the highest level
level: 6

# ignoreErrors:
# - '#PHPDoc tag @var#'
#
# excludePaths:
# - ./*/*/FileToBeExcluded.php
#
# checkMissingIterableValueType: false
checkMissingIterableValueType: false

checkGenericClassInNonGenericObjectType: false
level: 6
paths:
- src
ignoreErrors:
- '#Class [a-zA-Z0-9\\_]+ implements generic interface [a-zA-Z0-9\\_]+ but does not specify its types: [a-zA-Z0-9\\_]+#'
- '#Method [a-zA-Z0-9\\_]+::[a-zA-Z0-9\\_]+\(\) return type with generic class [a-zA-Z0-9\\_]+ does not specify its types: .+#'
- '#Trait [a-zA-Z0-9\\_]+ is used zero times and is not analysed.#'
- '#Unable to resolve the template type TRelatedModel in call to method [a-zA-Z0-9\\_]+::[a-zA-Z0-9\\_]+\(\)#'
2 changes: 1 addition & 1 deletion resources/views/pages/import.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<div class="{{ $crud->getEditContentClass() }}">
@include('crud::inc.grouped_errors')

<form action="{{ url($crud->route . '/import') }}" enctype="multipart/form-data" method="post">
<form action="{{ $action }}" enctype="multipart/form-data" method="post">
@csrf

@includeFirst(
Expand Down
3 changes: 3 additions & 0 deletions src/Http/Controllers/Admin/Interfaces/ExportableCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ interface ExportableCrud
{
public function getExport(): ImportExport;

/**
* @return array<string, mixed>
*/
public function getExportParameters(): array;
}
3 changes: 3 additions & 0 deletions src/Http/Controllers/Admin/Interfaces/ImportableCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ interface ImportableCrud
{
public function getImport(): ImportExport;

/**
* @return array<string, mixed>
*/
public function getImportParameters(): array;
}
3 changes: 3 additions & 0 deletions src/Http/Controllers/Admin/Interfaces/MultiExportableCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ interface MultiExportableCrud
public const QUERY_PARAM = 'export';
public const DEFAULT_EXPORT_NAME = 'default';

/**
* @return array<string, mixed>
*/
public function getAvailableExports(): array;
}
1 change: 1 addition & 0 deletions src/Http/Controllers/Admin/Traits/HasImportButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function import(): View

$this->data['crud'] = $this->crud;
$this->data['title'] = $this->crud->getTitle();
$this->data['action'] = url($this->crud->route . '/import');

return view('backpack-async-export::pages.import', $this->data);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Http/Requests/ImportRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class ImportRequest extends FormRequest
{
const PARAM_FILE = 'file';

/**
* @return array<string, mixed>
*/
public function rules(): array
{
return [
Expand Down
6 changes: 4 additions & 2 deletions src/Jobs/ExportJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ class ExportJob implements ShouldQueue
use SerializesModels;

private ImportExport $export;
private array $exportParameters;

public function __construct(ImportExport $export, array $exportParameters)
/**
* @param array<string, mixed> $exportParameters
*/
public function __construct(ImportExport $export, private array $exportParameters)
{
$this->export = $export;
$this->exportParameters = $exportParameters;
Expand Down
5 changes: 4 additions & 1 deletion src/Jobs/ImportJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class ImportJob implements ShouldQueue
use Queueable;
use SerializesModels;

/**
* @param array<string, mixed> $exportParameters
*/
public function __construct(
private ImportExport $export,
private array $exportParameters
Expand Down Expand Up @@ -70,7 +73,7 @@ public function handle(): void
->implode(', ');
throw new \Exception($message);
}
} catch (\Exception|\Throwable $exception) {
} catch (\Throwable $exception) {
$this->export->update([
ImportExport::COLUMN_STATUS => ImportExportStatus::Error,
ImportExport::COLUMN_ERROR => $exception->getMessage(),
Expand Down
7 changes: 5 additions & 2 deletions src/Models/ImportExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class ImportExport extends Model implements ImportExportInterface
self::COLUMN_STATUS => ImportExportStatus::class,
];

/**
* @var array|string[]
*/
protected array $dates = [
self::COLUMN_COMPLETED_AT,
];
Expand All @@ -68,7 +71,7 @@ public function user(): BelongsTo
return $this->belongsTo(config('backpack-async-import-export.user_model'));
}

protected static function boot()
protected static function boot(): void
{
parent::boot();
ImportExport::saving(function (ImportExport $export) {
Expand Down Expand Up @@ -128,7 +131,7 @@ public function getDownloadButton(): string
protected function isReady(): Attribute
{
return Attribute::make(
get: fn (): bool => ImportExportStatus::Successful === $this->{ImportExport::COLUMN_STATUS}
get: fn(): bool => ImportExportStatus::Successful === $this->{ImportExport::COLUMN_STATUS}
&& Storage::disk($this->disk)->exists($this->{self::COLUMN_FILENAME}),
);
}
Expand Down