initial commit
This commit is contained in:
54
app/Services/Command/CreateContactCommand.php
Normal file
54
app/Services/Command/CreateContactCommand.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Command;
|
||||
|
||||
use App\Data\Command\BatchResult;
|
||||
use App\Data\Contact;
|
||||
use App\Data\Intent\Contact\CreateContactIntent;
|
||||
use App\Data\InvariantException;
|
||||
use App\Services\Mapper\ContactMapper;
|
||||
use App\Services\Storage\ContactStorageProvider;
|
||||
|
||||
class CreateContactCommand
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ContactMapper $contactMapper,
|
||||
private readonly ContactStorageProvider $contactStorageProvider,
|
||||
) {
|
||||
}
|
||||
|
||||
public function execute(CreateContactIntent $command): ?Contact
|
||||
{
|
||||
$entity = $this->contactMapper->tryToCreateFromIntent($command);
|
||||
|
||||
return $this->contactStorageProvider->create($entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<CreateContactIntent> $actualBatch
|
||||
* @return BatchResult
|
||||
*/
|
||||
public function multipleExecute(array $actualBatch): BatchResult
|
||||
{
|
||||
$contacts = [];
|
||||
|
||||
foreach ($actualBatch as $intent) {
|
||||
try {
|
||||
$entity = $this->contactMapper->tryToCreateFromIntent($intent);
|
||||
$contacts[] = $entity;
|
||||
} catch (InvariantException) {
|
||||
// Do nothing -> mark later as failed
|
||||
}
|
||||
}
|
||||
|
||||
$result = $this->contactStorageProvider->batchCreate($contacts);
|
||||
|
||||
return new BatchResult(
|
||||
count($actualBatch) - count($contacts),
|
||||
count($contacts) - $result,
|
||||
$result,
|
||||
);
|
||||
}
|
||||
}
|
||||
21
app/Services/Command/DeleteContactCommand.php
Normal file
21
app/Services/Command/DeleteContactCommand.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Command;
|
||||
|
||||
use App\Data\Intent\Contact\DeleteContactIntent;
|
||||
use App\Services\Storage\ContactStorageProvider;
|
||||
|
||||
class DeleteContactCommand
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ContactStorageProvider $contactStorageProvider,
|
||||
) {
|
||||
}
|
||||
|
||||
public function execute(DeleteContactIntent $command): bool
|
||||
{
|
||||
return $this->contactStorageProvider->delete($command->uuid);
|
||||
}
|
||||
}
|
||||
25
app/Services/Command/ImportContactsCommand.php
Normal file
25
app/Services/Command/ImportContactsCommand.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Command;
|
||||
|
||||
use App\Data\Intent\ProcessImport\ImportContactsIntent;
|
||||
use App\Jobs\ProcessImport;
|
||||
use App\Services\ProcessImportMonitor\ProcessImportIdentifiable;
|
||||
use App\Services\ProcessImportMonitor\ProcessImportMonitor;
|
||||
|
||||
class ImportContactsCommand
|
||||
{
|
||||
public function __construct(private readonly ProcessImportMonitor $importMonitor)
|
||||
{
|
||||
}
|
||||
|
||||
public function execute(ImportContactsIntent $command): ProcessImportIdentifiable
|
||||
{
|
||||
$import = $this->importMonitor->newImport($command->path);
|
||||
ProcessImport::dispatch($command->path, $import);
|
||||
|
||||
return $import;
|
||||
}
|
||||
}
|
||||
26
app/Services/Command/UpdateContactCommand.php
Normal file
26
app/Services/Command/UpdateContactCommand.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Command;
|
||||
|
||||
use App\Data\Contact;
|
||||
use App\Data\Intent\Contact\UpdateContactIntent;
|
||||
use App\Services\Mapper\ContactMapper;
|
||||
use App\Services\Storage\ContactStorageProvider;
|
||||
|
||||
class UpdateContactCommand
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ContactMapper $contactMapper,
|
||||
private readonly ContactStorageProvider $contactStorageProvider,
|
||||
) {
|
||||
}
|
||||
|
||||
public function execute(UpdateContactIntent $command): ?Contact
|
||||
{
|
||||
$entity = $this->contactMapper->tryToCreateFromIntent($command);
|
||||
|
||||
return $this->contactStorageProvider->update($entity);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user