Skip to content

Commit 42ecb83

Browse files
committed
Rename duplicate parameters and attachments when importing from an info provider
This fixes issue #840
1 parent 2d3d05e commit 42ecb83

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

src/Services/InfoProviderSystem/DTOtoEntityConverter.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,21 @@ public function convertPart(PartDetailDTO $dto, Part $entity = new Part()): Part
174174
//Set the provider reference on the part
175175
$entity->setProviderReference(InfoProviderReference::fromPartDTO($dto));
176176

177+
$param_groups = [];
178+
177179
//Add parameters
178180
foreach ($dto->parameters ?? [] as $parameter) {
179-
$entity->addParameter($this->convertParameter($parameter));
181+
$new_param = $this->convertParameter($parameter);
182+
183+
$key = $new_param->getName() . '##' . $new_param->getGroup();
184+
//If there is already an parameter with the same name and group, rename the new parameter, by suffixing a number
185+
if (count($param_groups[$key] ?? []) > 0) {
186+
$new_param->setName($new_param->getName() . ' (' . (count($param_groups[$key]) + 1) . ')');
187+
}
188+
189+
$param_groups[$key][] = $new_param;
190+
191+
$entity->addParameter($new_param);
180192
}
181193

182194
//Add preview image
@@ -192,6 +204,8 @@ public function convertPart(PartDetailDTO $dto, Part $entity = new Part()): Part
192204
$entity->setMasterPictureAttachment($preview_image);
193205
}
194206

207+
$attachments_grouped = [];
208+
195209
//Add other images
196210
$images = $this->files_unique($dto->images ?? []);
197211
foreach ($images as $image) {
@@ -200,14 +214,29 @@ public function convertPart(PartDetailDTO $dto, Part $entity = new Part()): Part
200214
continue;
201215
}
202216

203-
$entity->addAttachment($this->convertFile($image, $image_type));
217+
$attachment = $this->convertFile($image, $image_type);
218+
219+
$attachments_grouped[$attachment->getName()][] = $attachment;
220+
if (count($attachments_grouped[$attachment->getName()] ?? []) > 1) {
221+
$attachment->setName($attachment->getName() . ' (' . (count($attachments_grouped[$attachment->getName()]) + 1) . ')');
222+
}
223+
224+
225+
$entity->addAttachment($attachment);
204226
}
205227

206228
//Add datasheets
207229
$datasheet_type = $this->getDatasheetType();
208230
$datasheets = $this->files_unique($dto->datasheets ?? []);
209231
foreach ($datasheets as $datasheet) {
210-
$entity->addAttachment($this->convertFile($datasheet, $datasheet_type));
232+
$attachment = $this->convertFile($datasheet, $datasheet_type);
233+
234+
$attachments_grouped[$attachment->getName()][] = $attachment;
235+
if (count($attachments_grouped[$attachment->getName()] ?? []) > 1) {
236+
$attachment->setName($attachment->getName() . ' (' . (count($attachments_grouped[$attachment->getName()])) . ')');
237+
}
238+
239+
$entity->addAttachment($attachment);
211240
}
212241

213242
//Add orderdetails and prices

tests/Services/InfoProviderSystem/DTOtoEntityConverterTest.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use App\Services\InfoProviderSystem\DTOs\PriceDTO;
3131
use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO;
3232
use App\Services\InfoProviderSystem\DTOtoEntityConverter;
33+
use PhpParser\Node\Param;
3334
use PHPUnit\Framework\TestCase;
3435
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
3536

@@ -150,9 +151,9 @@ public function testConvertFileWithoutName(): void
150151

151152
public function testConvertPart(): void
152153
{
153-
$parameters = [new ParameterDTO('Test', 'Test')];
154-
$datasheets = [new FileDTO('https://invalid.invalid/file.pdf'), new FileDTO('https://invalid.invalid/file.pdf', name: 'TestFile')];
155-
$images = [new FileDTO('https://invalid.invalid/image.png'), new FileDTO('https://invalid.invalid/image2.png', name: 'TestImage2'), new FileDTO('https://invalid.invalid/image2.png')];
154+
$parameters = [new ParameterDTO('Test', 'Test'), new ParameterDTO('Duplicate', 'Test'), new ParameterDTO('Test', 'test', group: "Other"), new ParameterDTO('Duplicate', 'ds')];
155+
$datasheets = [new FileDTO('https://invalid.invalid/file.pdf'), new FileDTO('https://invalid.invalid/file.pdf', name: 'TestFile'), new FileDTO('https://invalid.invalid/file2.pdf', name: 'Duplicate'), new FileDTO('https://invalid.invalid/file3.pdf', name: 'Duplicate')];
156+
$images = [new FileDTO('https://invalid.invalid/image.png'), new FileDTO('https://invalid.invalid/image2.png', name: 'TestImage2'), new FileDTO('https://invalid.invalid/image3.png', name: "Duplicate")];
156157
$shopping_infos = [new PurchaseInfoDTO('TestDistributor', 'TestOrderNumber', [new PriceDTO(1, "10.0", 'EUR')])];
157158

158159
$dto = new PartDetailDTO(
@@ -182,15 +183,31 @@ public function testConvertPart(): void
182183
$this->assertCount(count($parameters), $entity->getParameters());
183184
$this->assertCount(count($shopping_infos), $entity->getOrderdetails());
184185

186+
//Test that duplicate parameters get renamed:
187+
$this->assertSame('Test', $entity->getParameters()[0]->getName());
188+
$this->assertSame('Duplicate', $entity->getParameters()[1]->getName());
189+
$this->assertSame('Test', $entity->getParameters()[2]->getName());
190+
$this->assertSame('Duplicate (2)', $entity->getParameters()[3]->getName());
191+
185192
//Datasheets and images are stored as attachments and the duplicates, should be filtered out
186-
$this->assertCount(3, $entity->getAttachments());
193+
$this->assertCount(6, $entity->getAttachments());
187194
//The attachments should have the name of the named duplicate file
188195
$image1 = $entity->getAttachments()[0];
189196
$this->assertSame('Main image', $image1->getName());
190197

191198
$image1 = $entity->getAttachments()[1];
199+
$this->assertSame('TestImage2', $image1->getName());
192200

193201
$datasheet = $entity->getAttachments()[2];
202+
$this->assertSame('Duplicate', $datasheet->getName());
203+
204+
$datasheet = $entity->getAttachments()[3];
194205
$this->assertSame('TestFile', $datasheet->getName());
206+
207+
$datasheet = $entity->getAttachments()[4];
208+
$this->assertSame('Duplicate (2)', $datasheet->getName());
209+
210+
$datasheet = $entity->getAttachments()[5];
211+
$this->assertSame('Duplicate (3)', $datasheet->getName());
195212
}
196213
}

0 commit comments

Comments
 (0)