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\Loevgaard\BrandViewFactoryInterface; |
10
|
|
|
use Setono\SyliusLagersystemPlugin\View\Product\ProductVariantView; |
11
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
12
|
|
|
use Sylius\Component\Core\Model\ProductVariantInterface; |
13
|
|
|
use Webmozart\Assert\Assert; |
14
|
|
|
|
15
|
|
|
class ProductVariantViewFactory implements ProductVariantViewFactoryInterface |
16
|
|
|
{ |
17
|
|
|
/** @var BrandViewFactoryInterface */ |
18
|
|
|
private $brandViewFactory; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
private $productVariantViewClass; |
22
|
|
|
|
23
|
|
|
public function __construct( |
24
|
|
|
BrandViewFactoryInterface $brandViewFactory, |
25
|
|
|
string $productVariantViewClass |
26
|
|
|
) { |
27
|
|
|
$this->brandViewFactory = $brandViewFactory; |
28
|
|
|
$this->productVariantViewClass = $productVariantViewClass; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function create(ProductVariantInterface $variant, string $localeCode): ProductVariantView |
32
|
|
|
{ |
33
|
|
|
$translation = $variant->getTranslation($localeCode); |
34
|
|
|
|
35
|
|
|
$weight = $variant->getShippingWeight(); |
36
|
|
|
if (null !== $weight) { |
37
|
|
|
$weight = (int) ceil(1000 * $variant->getShippingWeight()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($variant->isTracked()) { |
41
|
|
|
$onHand = $variant->getOnHand(); |
42
|
|
|
} else { |
43
|
|
|
$onHand = 1; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** @var ProductVariantView $variantView */ |
47
|
|
|
$variantView = new $this->productVariantViewClass(); |
48
|
|
|
$variantView->id = $variant->getId(); |
49
|
|
|
$variantView->code = $variant->getCode(); |
50
|
|
|
$variantView->name = $translation->getName(); |
|
|
|
|
51
|
|
|
$variantView->weight = $weight; |
52
|
|
|
$variantView->onHand = $onHand; |
53
|
|
|
|
54
|
|
|
/** @var ProductInterface|BrandAwareInterface|null $product */ |
55
|
|
|
$product = $variant->getProduct(); |
56
|
|
|
Assert::notNull($product); |
57
|
|
|
|
58
|
|
|
if ($product instanceof BrandAwareInterface) { |
59
|
|
|
$brand = $product->getBrand(); |
60
|
|
|
if (null !== $brand) { |
61
|
|
|
$variantView->brand = $this->brandViewFactory->create($brand); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($variant instanceof BarcodeAwareInterface) { |
66
|
|
|
$variantView->barcode = $variant->getBarcode(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $variantView; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|