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

ProductVariantViewFactory::create()   B

Complexity

Conditions 9
Paths 96

Size

Total Lines 52
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 29
nc 96
nop 2
dl 0
loc 52
rs 8.0555
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Image\ImageViewFactoryInterface;
10
use Setono\SyliusLagersystemPlugin\Factory\Loevgaard\BrandViewFactoryInterface;
11
use Setono\SyliusLagersystemPlugin\View\Product\ProductVariantView;
12
use Sylius\Component\Core\Model\ProductImageInterface;
13
use Sylius\Component\Core\Model\ProductInterface;
14
use Sylius\Component\Core\Model\ProductVariantInterface;
15
use Webmozart\Assert\Assert;
16
17
class ProductVariantViewFactory implements ProductVariantViewFactoryInterface
18
{
19
    /** @var BrandViewFactoryInterface */
20
    protected $brandViewFactory;
21
22
    /** @var ImageViewFactoryInterface */
23
    protected $imageViewFactory;
24
25
    /** @var string */
26
    protected $productVariantViewClass;
27
28
    public function __construct(
29
        BrandViewFactoryInterface $brandViewFactory,
30
        ImageViewFactoryInterface $imageViewFactory,
31
        string $productVariantViewClass
32
    ) {
33
        $this->brandViewFactory = $brandViewFactory;
34
        $this->imageViewFactory = $imageViewFactory;
35
        $this->productVariantViewClass = $productVariantViewClass;
36
    }
37
38
    public function create(ProductVariantInterface $variant, string $localeCode): ProductVariantView
39
    {
40
        $translation = $variant->getTranslation($localeCode);
41
42
        $weight = $variant->getShippingWeight();
43
        if (null !== $weight) {
44
            $weight = (int) ceil(1000 * $variant->getShippingWeight());
45
        }
46
47
        if ($variant->isTracked()) {
48
            $onHand = $variant->getOnHand();
49
        } else {
50
            $onHand = 1;
51
        }
52
53
        /** @var ProductVariantView $variantView */
54
        $variantView = new $this->productVariantViewClass();
55
        $variantView->id = $variant->getId();
56
        $variantView->code = $variant->getCode();
57
        $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

57
        /** @scrutinizer ignore-call */ 
58
        $variantView->name = $translation->getName();
Loading history...
58
        $variantView->weight = $weight;
59
        $variantView->onHand = $onHand;
60
61
        /** @var ProductInterface|null $product */
62
        $product = $variant->getProduct();
63
        Assert::notNull($product);
64
65
        if ($product instanceof BrandAwareInterface) {
66
            $brand = $product->getBrand();
67
            if (null !== $brand) {
68
                $variantView->brand = $this->brandViewFactory->create($brand);
69
            }
70
        }
71
72
        if ($variant instanceof BarcodeAwareInterface) {
73
            $variantView->barcode = $variant->getBarcode();
74
        }
75
76
        /** @var ProductImageInterface $image */
77
        foreach ($product->getImages() as $image) {
78
            $imageView = $this->imageViewFactory->create($image);
79
80
            foreach ($image->getProductVariants() as $imagesVariant) {
81
                if ($imagesVariant !== $variant) {
82
                    continue;
83
                }
84
85
                $variantView->images[] = $imageView;
86
            }
87
        }
88
89
        return $variantView;
90
    }
91
}
92