Completed
Branch master (9d8d38)
by Łukasz
03:25
created

DetailedProductViewFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 8
dl 0
loc 95
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A create() 0 10 2
B createWithVariants() 0 23 4
A createAssociations() 0 10 2
1
<?php
2
3
namespace Sylius\ShopApiPlugin\Factory;
4
5
use Sylius\Component\Core\Model\ChannelInterface;
6
use Sylius\Component\Core\Model\ProductImageInterface;
7
use Sylius\Component\Core\Model\ProductInterface;
8
use Sylius\Component\Core\Model\ProductVariantInterface;
9
use Sylius\Component\Core\Model\TaxonInterface;
10
use Sylius\Component\Product\Model\ProductAssociationInterface;
11
use Sylius\Component\Product\Model\ProductAssociationTypeInterface;
12
use Sylius\ShopApiPlugin\View\ProductVariantView;
13
use Sylius\ShopApiPlugin\View\ProductView;
14
15
final class DetailedProductViewFactory implements ProductViewFactoryInterface
16
{
17
    /**
18
     * @var ImageViewFactoryInterface
19
     */
20
    private $imageViewFactory;
21
22
    /**
23
     * @var ProductViewFactoryInterface
24
     */
25
    private $productViewFactory;
26
27
    /**
28
     * @var ProductVariantViewFactoryInterface
29
     */
30
    private $variantViewFactory;
31
32
    /**
33
     * @param ImageViewFactoryInterface $imageViewFactory
34
     * @param ProductViewFactoryInterface $productViewFactory
35
     * @param ProductVariantViewFactoryInterface $variantViewFactory
36
     */
37
    public function __construct(
38
        ImageViewFactoryInterface $imageViewFactory,
39
        ProductViewFactoryInterface $productViewFactory,
40
        ProductVariantViewFactoryInterface $variantViewFactory
41
    ) {
42
        $this->imageViewFactory = $imageViewFactory;
43
        $this->variantViewFactory = $variantViewFactory;
44
        $this->productViewFactory = $productViewFactory;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function create(ProductInterface $product, ChannelInterface $channel, $locale)
51
    {
52
        $productView = $this->createWithVariants($product, $channel, $locale);
53
54
        foreach ($product->getAssociations() as $association) {
55
            $productView->associations[$association->getType()->getCode()] = $this->createAssociations($association, $channel, $locale);
56
        }
57
58
        return $productView;
59
    }
60
61
    /**
62
     * @param ProductInterface $product
63
     * @param ChannelInterface $channel
64
     * @param string $locale
65
     *
66
     * @return ProductView
67
     */
68
    private function createWithVariants(ProductInterface $product, ChannelInterface $channel, $locale)
69
    {
70
        $productView = $this->productViewFactory->create($product, $channel, $locale);
71
72
        /** @var ProductVariantInterface $variant */
73
        foreach ($product->getVariants() as $variant) {
74
            $productView->variants[$variant->getCode()] = $this->variantViewFactory->create($variant, $channel, $locale);
75
        }
76
77
        /** @var ProductImageInterface $image */
78
        foreach ($product->getImages() as $image) {
79
            $imageView = $this->imageViewFactory->create($image);
80
81
            foreach ($image->getProductVariants() as $productVariant) {
82
                /** @var ProductVariantView $variantView */
83
                $variantView = $productView->variants[$productVariant->getCode()];
84
85
                $variantView->images[] = $imageView;
86
            }
87
        }
88
89
        return $productView;
90
    }
91
92
    /**
93
     * @param ProductAssociationInterface $association
94
     * @param ChannelInterface $channel
95
     * @param string $locale
96
     *
97
     * @return array
98
     */
99
    private function createAssociations(ProductAssociationInterface $association, ChannelInterface $channel, $locale)
100
    {
101
        $associatedProducts = [];
102
103
        foreach ($association->getAssociatedProducts() as $associatedProduct) {
104
            $associatedProducts[] = $this->createWithVariants($associatedProduct, $channel, $locale);
105
        }
106
107
        return $associatedProducts;
108
    }
109
}
110