Passed
Pull Request — master (#1)
by Igor
05:02
created

ProductVariantViewFactory::create()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 23
c 1
b 0
f 0
nc 24
nop 2
dl 0
loc 39
rs 8.9297
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();
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|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