initial commit
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\ProcessImportMonitor;
|
||||
|
||||
use Ramsey\Uuid\Rfc4122\UuidV7;
|
||||
|
||||
class DatabaseProcessImportMonitor implements ProcessImportMonitor
|
||||
{
|
||||
public function newImport(string $file): ProcessImportState&ProcessImportIdentifiable
|
||||
{
|
||||
return new DatabaseProcessImportState(UuidV7::uuid7(), $file);
|
||||
}
|
||||
}
|
||||
115
app/Services/ProcessImportMonitor/DatabaseProcessImportState.php
Normal file
115
app/Services/ProcessImportMonitor/DatabaseProcessImportState.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\ProcessImportMonitor;
|
||||
|
||||
use App\Enums\ContactImportStateEnum;
|
||||
use App\Models\ContactImport;
|
||||
use Carbon\Carbon;
|
||||
use Ramsey\Uuid\UuidInterface;
|
||||
|
||||
class DatabaseProcessImportState implements ProcessImportIdentifiable, ProcessImportState
|
||||
{
|
||||
public function __construct(
|
||||
public readonly UuidInterface $uuid,
|
||||
string $file
|
||||
) {
|
||||
ContactImport::query()->create(
|
||||
[
|
||||
'id' => $this->uuid()->toString(),
|
||||
'state' => ContactImportStateEnum::Pending,
|
||||
'file' => $file,
|
||||
'queue_at' => Carbon::now(),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
public function uuid(): UuidInterface
|
||||
{
|
||||
return $this->uuid;
|
||||
}
|
||||
|
||||
public function start(): void
|
||||
{
|
||||
ContactImport::query()->where(
|
||||
[
|
||||
'id' => $this->uuid()->toString(),
|
||||
]
|
||||
)->update(
|
||||
[
|
||||
'started_at' => now(),
|
||||
'state' => ContactImportStateEnum::Running,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
public function fail(): void
|
||||
{
|
||||
ContactImport::query()->where(
|
||||
[
|
||||
'id' => $this->uuid()->toString(),
|
||||
],
|
||||
)->update(
|
||||
[
|
||||
'finished_at' => now(),
|
||||
'state' => ContactImportStateEnum::Fail,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function finish(): void
|
||||
{
|
||||
ContactImport::query()->where(
|
||||
[
|
||||
'id' => $this->uuid()->toString(),
|
||||
],
|
||||
)->update(
|
||||
[
|
||||
'finished_at' => now(),
|
||||
'state' => ContactImportStateEnum::Done,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function contactDuplicate(int $processed = 1): void
|
||||
{
|
||||
ContactImport::query()->where(
|
||||
[
|
||||
'id' => $this->uuid()->toString(),
|
||||
],
|
||||
)->incrementEach(
|
||||
[
|
||||
'total_processed' => $processed,
|
||||
'duplicates' => $processed,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function contactFail(int $processed = 1): void
|
||||
{
|
||||
ContactImport::query()->where(
|
||||
[
|
||||
'id' => $this->uuid()->toString(),
|
||||
],
|
||||
)->incrementEach(
|
||||
[
|
||||
'total_processed' => $processed,
|
||||
'errors' => $processed,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function contactSuccess(int $processed = 1): void
|
||||
{
|
||||
ContactImport::query()->where(
|
||||
[
|
||||
'id' => $this->uuid()->toString(),
|
||||
],
|
||||
)->incrementEach(
|
||||
[
|
||||
'total_processed' => $processed,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\ProcessImportMonitor;
|
||||
|
||||
use Ramsey\Uuid\UuidInterface;
|
||||
|
||||
interface ProcessImportIdentifiable
|
||||
{
|
||||
public UuidInterface $uuid { get; }
|
||||
}
|
||||
10
app/Services/ProcessImportMonitor/ProcessImportMonitor.php
Normal file
10
app/Services/ProcessImportMonitor/ProcessImportMonitor.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\ProcessImportMonitor;
|
||||
|
||||
interface ProcessImportMonitor
|
||||
{
|
||||
public function newImport(string $file): ProcessImportState&ProcessImportIdentifiable;
|
||||
}
|
||||
29
app/Services/ProcessImportMonitor/ProcessImportState.php
Normal file
29
app/Services/ProcessImportMonitor/ProcessImportState.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\ProcessImportMonitor;
|
||||
|
||||
use Ramsey\Uuid\UuidInterface;
|
||||
|
||||
/**
|
||||
* Handling actual state of import
|
||||
*
|
||||
* Never use not-serializable values in implementations!
|
||||
*/
|
||||
interface ProcessImportState
|
||||
{
|
||||
public function uuid(): UuidInterface;
|
||||
|
||||
public function start(): void;
|
||||
|
||||
public function finish(): void;
|
||||
|
||||
public function fail(): void;
|
||||
|
||||
public function contactFail(int $processed = 1): void;
|
||||
|
||||
public function contactDuplicate(int $processed = 1): void;
|
||||
|
||||
public function contactSuccess(int $processed = 1): void;
|
||||
}
|
||||
Reference in New Issue
Block a user