Completed
Push — master ( 27ca42...afe955 )
by Kamil
03:24
created

ProductViewFactory::create()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 3
eloc 14
nc 4
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\ShopApiPlugin\Factory;
6
7
use Sylius\Component\Core\Model\ChannelInterface;
8
use Sylius\Component\Core\Model\ProductImageInterface;
9
use Sylius\Component\Core\Model\ProductInterface;
10
use Sylius\Component\Core\Model\ProductTranslationInterface;
11
use Sylius\Component\Core\Model\TaxonInterface;
12
use Sylius\ShopApiPlugin\View\ProductView;
13
use Sylius\ShopApiPlugin\View\TaxonView;
14
15
final class ProductViewFactory implements ProductViewFactoryInterface
16
{
17
    /**
18
     * @var ImageViewFactoryInterface
19
     */
20
    private $imageViewFactory;
21
22
    /**
23
     * @var ProductAttributeValuesViewFactoryInterface
24
     */
25
    private $attributeValuesViewFactory;
26
27
    /**
28
     * @var TaxonViewFactoryInterface
29
     */
30
    private $taxonViewFactory;
31
32
    /**
33
     * @var string
34
     */
35
    private $fallback;
36
37
    /**
38
     * @param ImageViewFactoryInterface $imageViewFactory
39
     * @param ProductAttributeValuesViewFactoryInterface $attributeValuesViewFactory
40
     * @param TaxonViewFactoryInterface $taxonViewFactory
41
     * @param string $fallback
42
     */
43
    public function __construct(
44
        ImageViewFactoryInterface $imageViewFactory,
45
        ProductAttributeValuesViewFactoryInterface $attributeValuesViewFactory,
46
        TaxonViewFactoryInterface $taxonViewFactory,
47
        $fallback
48
    ) {
49
        $this->imageViewFactory = $imageViewFactory;
50
        $this->attributeValuesViewFactory = $attributeValuesViewFactory;
51
        $this->taxonViewFactory = $taxonViewFactory;
52
        $this->fallback = $fallback;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function create(ProductInterface $product, ChannelInterface $channel, string $locale): ProductView
59
    {
60
        $productView = new ProductView();
61
        $productView->code = $product->getCode();
62
        $productView->averageRating = $product->getAverageRating();
0 ignored issues
show
Documentation Bug introduced by
The property $averageRating was declared of type string, but $product->getAverageRating() is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
63
64
        /** @var ProductTranslationInterface $translation */
65
        $translation = $product->getTranslation($locale);
66
        $productView->name = $translation->getName();
67
        $productView->slug = $translation->getSlug();
68
69
        /** @var ProductImageInterface $image */
70
        foreach ($product->getImages() as $image) {
71
            $imageView = $this->imageViewFactory->create($image);
72
            $productView->images[] = $imageView;
73
        }
74
75
        /** @var TaxonInterface $taxon */
76
        foreach ($product->getTaxons() as $taxon) {
77
            $productView->taxons[$taxon->getCode()] = $this->getTaxonWithAncestors($taxon, $locale);
78
        }
79
80
        $productView->attributes = $this->attributeValuesViewFactory->create($product->getAttributesByLocale($locale, $this->fallback));
0 ignored issues
show
Bug introduced by
It seems like $product->getAttributesB...ocale, $this->fallback) targeting Sylius\Component\Attribu...getAttributesByLocale() can also be of type object<Doctrine\Common\Collections\Collection>; however, Sylius\ShopApiPlugin\Fac...toryInterface::create() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
81
82
        return $productView;
83
    }
84
85
    private function getTaxonWithAncestors(TaxonInterface $taxon, $locale): TaxonView
86
    {
87
        $currentTaxonView = $this->taxonViewFactory->create($taxon, $locale);
88
89
        if (null === $taxon->getParent()) {
90
            return $currentTaxonView;
91
        }
92
93
        $taxonView = $this->getTaxonWithAncestors($taxon->getParent(), $locale);
0 ignored issues
show
Compatibility introduced by
$taxon->getParent() of type object<Sylius\Component\...y\Model\TaxonInterface> is not a sub-type of object<Sylius\Component\...e\Model\TaxonInterface>. It seems like you assume a child interface of the interface Sylius\Component\Taxonomy\Model\TaxonInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
94
        $taxonView->children[] = $currentTaxonView;
95
96
        return $taxonView;
97
    }
98
}
99