Added very basic controller to merge info provider data into the part

This commit is contained in:
Jan Böhmer 2023-11-19 23:47:46 +01:00
parent 01784a9d1f
commit 87626589a3
3 changed files with 31 additions and 12 deletions

View file

@ -36,6 +36,7 @@ use App\Exceptions\AttachmentDownloadException;
use App\Form\Part\PartBaseType; use App\Form\Part\PartBaseType;
use App\Services\Attachments\AttachmentSubmitHandler; use App\Services\Attachments\AttachmentSubmitHandler;
use App\Services\Attachments\PartPreviewGenerator; use App\Services\Attachments\PartPreviewGenerator;
use App\Services\EntityMergers\Mergers\PartMerger;
use App\Services\InfoProviderSystem\PartInfoRetriever; use App\Services\InfoProviderSystem\PartInfoRetriever;
use App\Services\LogSystem\EventCommentHelper; use App\Services\LogSystem\EventCommentHelper;
use App\Services\LogSystem\HistoryHelper; use App\Services\LogSystem\HistoryHelper;
@ -233,6 +234,24 @@ class PartController extends AbstractController
]); ]);
} }
#[Route(path: '/{id}/from_info_provider/{providerKey}/{providerId}/update', requirements: ['providerId' => '.+'])]
public function updateFromInfoProvider(Part $part, Request $request, string $providerKey, string $providerId,
PartInfoRetriever $infoRetriever, PartMerger $partMerger): Response
{
$this->denyAccessUnlessGranted('edit', $part);
$this->denyAccessUnlessGranted('@info_providers.create_parts');
$dto = $infoRetriever->getDetails($providerKey, $providerId);
$provider_part = $infoRetriever->dtoToPart($dto);
$part = $partMerger->merge($part, $provider_part);
return $this->renderPartForm('edit', $request, $part, [
'info_provider_dto' => $dto,
]);
}
/** /**
* This function provides a common implementation for methods, which use the part form. * This function provides a common implementation for methods, which use the part form.
* @param Request $request * @param Request $request

View file

@ -63,6 +63,8 @@ trait EntityMergerHelperTrait
//Set the value //Set the value
$this->property_accessor->setValue($target, $field, $value); $this->property_accessor->setValue($target, $field, $value);
return $target;
} }
/** /**
@ -75,7 +77,7 @@ trait EntityMergerHelperTrait
*/ */
protected function useOtherValueIfNotNull(object $target, object $other, string $field): object protected function useOtherValueIfNotNull(object $target, object $other, string $field): object
{ {
$this->useCallback( return $this->useCallback(
function ($target_value, $other_value) { function ($target_value, $other_value) {
return $target_value ?? $other_value; return $target_value ?? $other_value;
}, },
@ -84,7 +86,6 @@ trait EntityMergerHelperTrait
$field $field
); );
return $target;
} }
/** /**
@ -97,7 +98,7 @@ trait EntityMergerHelperTrait
*/ */
protected function useOtherValueIfNotEmtpy(object $target, object $other, string $field): object protected function useOtherValueIfNotEmtpy(object $target, object $other, string $field): object
{ {
$this->useCallback( return $this->useCallback(
function ($target_value, $other_value) { function ($target_value, $other_value) {
return empty($target_value) ? $other_value : $target_value; return empty($target_value) ? $other_value : $target_value;
}, },
@ -105,8 +106,6 @@ trait EntityMergerHelperTrait
$other, $other,
$field $field
); );
return $target;
} }
/** /**
@ -119,7 +118,7 @@ trait EntityMergerHelperTrait
*/ */
protected function useLargerValue(object $target, object $other, string $field): object protected function useLargerValue(object $target, object $other, string $field): object
{ {
$this->useCallback( return $this->useCallback(
function ($target_value, $other_value) { function ($target_value, $other_value) {
return max($target_value, $other_value); return max($target_value, $other_value);
}, },
@ -127,8 +126,6 @@ trait EntityMergerHelperTrait
$other, $other,
$field $field
); );
return $target;
} }
/** /**
@ -141,7 +138,7 @@ trait EntityMergerHelperTrait
*/ */
protected function useSmallerValue(object $target, object $other, string $field): object protected function useSmallerValue(object $target, object $other, string $field): object
{ {
$this->useCallback( return $this->useCallback(
function ($target_value, $other_value) { function ($target_value, $other_value) {
return min($target_value, $other_value); return min($target_value, $other_value);
}, },
@ -149,8 +146,6 @@ trait EntityMergerHelperTrait
$other, $other,
$field $field
); );
return $target;
} }
/** /**

View file

@ -37,14 +37,19 @@ class PartMerger implements EntityMergerInterface
return $target instanceof Part && $other instanceof Part; return $target instanceof Part && $other instanceof Part;
} }
public function merge(object $target, object $other, array $context = []): object public function merge(object $target, object $other, array $context = []): Part
{ {
if (!$target instanceof Part || !$other instanceof Part) { if (!$target instanceof Part || !$other instanceof Part) {
throw new \InvalidArgumentException('The target and the other entity must be instances of Part'); throw new \InvalidArgumentException('The target and the other entity must be instances of Part');
} }
//Merge the fields //Merge the fields
$this->useOtherValueIfNotNull($target, $other, 'manufacturer');
$this->mergeCollections($target, $other, 'partLots'); $this->mergeCollections($target, $other, 'partLots');
$this->mergeCollections($target, $other, 'attachments');
$this->mergeCollections($target, $other, 'orderdetails');
$this->mergeCollections($target, $other, 'parameters');
return $target; return $target;
} }