/
ProductVariantMapper.php
38 lines (29 loc) · 1.1 KB
/
ProductVariantMapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
declare(strict_types=1);
namespace Setono\SyliusMiintoPlugin\Mapper;
use Setono\SyliusMiintoPlugin\Exception\NoMappingFoundException;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
final class ProductVariantMapper implements ProductVariantMapperInterface
{
/** @var ProductVariantRepositoryInterface */
private $productVariantRepository;
/** @var string */
private $gtinField;
public function __construct(ProductVariantRepositoryInterface $productVariantRepository, string $gtinField)
{
$this->productVariantRepository = $productVariantRepository;
$this->gtinField = $gtinField;
}
public function map(array $item): ProductVariantInterface
{
/** @var ProductVariantInterface|null $productVariant */
$productVariant = $this->productVariantRepository->findOneBy([
$this->gtinField => $item['gtin'],
]);
if (null === $productVariant) {
throw new NoMappingFoundException($item);
}
return $productVariant;
}
}