1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Setono\SyliusLagersystemPlugin\Factory\Product; |
6
|
|
|
|
7
|
|
|
use Loevgaard\SyliusBarcodePlugin\Model\BarcodeAwareInterface; |
8
|
|
|
use Loevgaard\SyliusBrandPlugin\Model\BrandAwareInterface; |
9
|
|
|
use Setono\SyliusLagersystemPlugin\Factory\Image\ImageViewFactoryInterface; |
10
|
|
|
use Setono\SyliusLagersystemPlugin\Factory\Loevgaard\BrandViewFactoryInterface; |
11
|
|
|
use Setono\SyliusLagersystemPlugin\View\Product\ProductVariantView; |
12
|
|
|
use Sylius\Component\Core\Model\ProductImageInterface; |
13
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
14
|
|
|
use Sylius\Component\Core\Model\ProductVariantInterface; |
15
|
|
|
use Webmozart\Assert\Assert; |
16
|
|
|
|
17
|
|
|
class ProductVariantViewFactory implements ProductVariantViewFactoryInterface |
18
|
|
|
{ |
19
|
|
|
/** @var BrandViewFactoryInterface */ |
20
|
|
|
protected $brandViewFactory; |
21
|
|
|
|
22
|
|
|
/** @var ImageViewFactoryInterface */ |
23
|
|
|
protected $imageViewFactory; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $productVariantViewClass; |
27
|
|
|
|
28
|
|
|
public function __construct( |
29
|
|
|
BrandViewFactoryInterface $brandViewFactory, |
30
|
|
|
ImageViewFactoryInterface $imageViewFactory, |
31
|
|
|
string $productVariantViewClass |
32
|
|
|
) { |
33
|
|
|
$this->brandViewFactory = $brandViewFactory; |
34
|
|
|
$this->imageViewFactory = $imageViewFactory; |
35
|
|
|
$this->productVariantViewClass = $productVariantViewClass; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function create(ProductVariantInterface $variant, string $localeCode): ProductVariantView |
39
|
|
|
{ |
40
|
|
|
$translation = $variant->getTranslation($localeCode); |
41
|
|
|
|
42
|
|
|
$weight = $variant->getShippingWeight(); |
43
|
|
|
if (null !== $weight) { |
44
|
|
|
$weight = (int) ceil(1000 * $variant->getShippingWeight()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($variant->isTracked()) { |
48
|
|
|
$onHand = $variant->getOnHand(); |
49
|
|
|
} else { |
50
|
|
|
$onHand = 1; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** @var ProductVariantView $variantView */ |
54
|
|
|
$variantView = new $this->productVariantViewClass(); |
55
|
|
|
$variantView->id = $variant->getId(); |
56
|
|
|
$variantView->code = $variant->getCode(); |
57
|
|
|
$variantView->name = $translation->getName(); |
|
|
|
|
58
|
|
|
$variantView->weight = $weight; |
59
|
|
|
$variantView->onHand = $onHand; |
60
|
|
|
|
61
|
|
|
/** @var ProductInterface|null $product */ |
62
|
|
|
$product = $variant->getProduct(); |
63
|
|
|
Assert::notNull($product); |
64
|
|
|
|
65
|
|
|
if ($product instanceof BrandAwareInterface) { |
66
|
|
|
$brand = $product->getBrand(); |
67
|
|
|
if (null !== $brand) { |
68
|
|
|
$variantView->brand = $this->brandViewFactory->create($brand); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($variant instanceof BarcodeAwareInterface) { |
73
|
|
|
$variantView->barcode = $variant->getBarcode(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** @var ProductImageInterface $image */ |
77
|
|
|
foreach ($product->getImages() as $image) { |
78
|
|
|
$imageView = $this->imageViewFactory->create($image); |
79
|
|
|
|
80
|
|
|
foreach ($image->getProductVariants() as $imagesVariant) { |
81
|
|
|
if ($imagesVariant !== $variant) { |
82
|
|
|
continue; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$variantView->images[] = $imageView; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $variantView; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|