| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Setono\SyliusLagersystemPlugin\Factory\Product; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Loevgaard\SyliusBarcodePlugin\Model\BarcodeAwareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Setono\SyliusLagersystemPlugin\Factory\Image\ImageViewFactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Setono\SyliusLagersystemPlugin\View\Product\ProductVariantView; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Sylius\Component\Core\Model\ProductImageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 ImageViewFactoryInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $imageViewFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected $productVariantViewClass; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         ImageViewFactoryInterface $imageViewFactory, | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         string $productVariantViewClass | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         $this->imageViewFactory = $imageViewFactory; | 
            
                                                                        
                            
            
                                    
            
            
                | 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|null $product */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $product = $variant->getProduct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         Assert::notNull($product); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         if ($variant instanceof BarcodeAwareInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $variantView->barcode = $variant->getBarcode(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         /** @var ProductImageInterface $image */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         foreach ($product->getImages() as $image) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $imageView = $this->imageViewFactory->create($image); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             foreach ($image->getProductVariants() as $imagesVariant) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 if ($imagesVariant !== $variant) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 $variantView->images[] = $imageView; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return $variantView; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  |