ProductVariantViewFactory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 61
rs 10
c 1
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B create() 0 45 7
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();
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Sylius\Component\Resourc...el\TranslationInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Sylius\Component\Resourc...del\AbstractTranslation or Sylius\Component\Product...lueTranslationInterface or spec\Sylius\Component\Re...del\ConcreteTranslation or Sylius\Component\Product...tOptionValueTranslation or AppBundle\Entity\BookTranslation or Sylius\Component\Product...tOptionValueTranslation. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        /** @scrutinizer ignore-call */ 
51
        $variantView->name = $translation->getName();
Loading history...
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