Passed
Pull Request — master (#1)
by Igor
04:23
created

ProductVariantViewFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 49
rs 10
c 1
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A create() 0 33 5
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
        /** @var ProductVariantView $variantView */
41
        $variantView = new $this->productVariantViewClass();
42
        $variantView->id = $variant->getId();
43
        $variantView->code = $variant->getCode();
44
        $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

44
        /** @scrutinizer ignore-call */ 
45
        $variantView->name = $translation->getName();
Loading history...
45
        $variantView->weight = $weight;
46
        $variantView->onHand = $variant->getOnHand();
47
48
        /** @var ProductInterface|BrandAwareInterface|null $product */
49
        $product = $variant->getProduct();
50
        Assert::notNull($product);
51
52
        if ($product instanceof BrandAwareInterface) {
53
            $brand = $product->getBrand();
54
            if (null !== $brand) {
55
                $variantView->brand = $this->brandViewFactory->create($brand);
56
            }
57
        }
58
59
        if ($variant instanceof BarcodeAwareInterface) {
60
            $variantView->barcode = $variant->getBarcode();
61
        }
62
63
        return $variantView;
64
    }
65
}
66