Completed
Push — master ( 2c1f3d...7a9a35 )
by Adam
05:47
created

VariantHelper   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 11
dl 0
loc 123
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getVariants() 0 12 2
A getAttributes() 0 12 2
A extractAttributesData() 0 9 1
A extractVariantData() 0 20 1
A getPhoto() 0 8 2
A getVariantOptionsKey() 0 15 1
A getVariantOptions() 0 12 1
A sortOptions() 0 4 1
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 * 
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 * 
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\CatalogBundle\Helper;
14
15
use WellCommerce\Bundle\AppBundle\Entity\Media;
16
use WellCommerce\Bundle\AppBundle\Helper\CurrencyHelperInterface;
17
use WellCommerce\Bundle\CatalogBundle\Entity\Product;
18
use WellCommerce\Bundle\CatalogBundle\Entity\Variant;
19
use WellCommerce\Bundle\CatalogBundle\Entity\VariantOption;
20
21
/**
22
 * Class VariantHelper
23
 *
24
 * @author  Adam Piotrowski <[email protected]>
25
 */
26
class VariantHelper implements VariantHelperInterface
27
{
28
    /**
29
     * @var CurrencyHelperInterface
30
     */
31
    protected $currencyHelper;
32
    
33
    /**
34
     * VariantHelper constructor.
35
     *
36
     * @param CurrencyHelperInterface $currencyHelper
37
     */
38
    public function __construct(CurrencyHelperInterface $currencyHelper)
39
    {
40
        $this->currencyHelper = $currencyHelper;
41
    }
42
    
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getVariants(Product $product): array
47
    {
48
        $variants = [];
49
        
50
        $product->getVariants()->map(function (Variant $variant) use (&$variants) {
51
            if ($variant->isEnabled()) {
52
                $this->extractVariantData($variant, $variants);
53
            }
54
        });
55
        
56
        return $variants;
57
    }
58
    
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getAttributes(Product $product): array
63
    {
64
        $attributes = [];
65
        
66
        $product->getVariants()->map(function (Variant $variant) use (&$attributes) {
67
            if ($variant->isEnabled()) {
68
                $this->extractAttributesData($variant, $attributes);
69
            }
70
        });
71
        
72
        return $attributes;
73
    }
74
    
75
    protected function extractAttributesData(Variant $variant, &$attributes)
76
    {
77
        $variant->getOptions()->map(function (VariantOption $variantOption) use (&$attributes) {
78
            $attribute                                                           = $variantOption->getAttribute();
79
            $attributeValue                                                      = $variantOption->getAttributeValue();
80
            $attributes[$attribute->getId()]['name']                             = $attribute->translate()->getName();
81
            $attributes[$attribute->getId()]['values'][$attributeValue->getId()] = $attributeValue->translate()->getName();
82
        });
83
    }
84
    
85
    protected function extractVariantData(Variant $variant, array &$variants)
86
    {
87
        $sellPrice    = $variant->getSellPrice();
88
        $baseCurrency = $sellPrice->getCurrency();
89
        $key          = $this->getVariantOptionsKey($variant);
90
        
91
        $variants[$key] = [
92
            'id'                 => $variant->getId(),
93
            'finalPriceGross'    => $this->currencyHelper->convertAndFormat($sellPrice->getFinalGrossAmount(), $baseCurrency),
94
            'sellPriceGross'     => $this->currencyHelper->convertAndFormat($sellPrice->getGrossAmount(), $baseCurrency),
95
            'discountPriceGross' => $this->currencyHelper->convertAndFormat($sellPrice->getDiscountedGrossAmount(), $baseCurrency),
96
            'finalPriceNet'      => $this->currencyHelper->convertAndFormat($sellPrice->getFinalNetAmount(), $baseCurrency),
97
            'sellPriceNet'       => $this->currencyHelper->convertAndFormat($sellPrice->getNetAmount(), $baseCurrency),
98
            'discountPriceNet'   => $this->currencyHelper->convertAndFormat($sellPrice->getDiscountedNetAmount(), $baseCurrency),
99
            'stock'              => $variant->getStock(),
100
            'symbol'             => $variant->getSymbol(),
101
            'options'            => $this->getVariantOptions($variant),
102
            'photo'              => $this->getPhoto($variant->getPhoto()),
103
        ];
104
    }
105
    
106
    protected function getPhoto(Media $media = null): string
107
    {
108
        if ($media instanceof Media) {
109
            return $media->getPath();
110
        }
111
        
112
        return '';
113
    }
114
    
115
    protected function getVariantOptionsKey(Variant $variant): string
116
    {
117
        $options = [];
118
        
119
        $variant->getOptions()->map(function (VariantOption $variantOption) use (&$options) {
120
            $attribute      = $variantOption->getAttribute();
121
            $attributeValue = $variantOption->getAttributeValue();
122
            $key            = sprintf('%s:%s', $attribute->getId(), $attributeValue->getId());
123
            $options[$key]  = $key;
124
        });
125
        
126
        ksort($options);
127
        
128
        return implode(',', $options);
129
    }
130
    
131
    public function getVariantOptions(Variant $variant): array
132
    {
133
        $options = [];
134
        
135
        $variant->getOptions()->map(function (VariantOption $variantOption) use (&$options) {
136
            $attribute                                   = $variantOption->getAttribute();
137
            $attributeValue                              = $variantOption->getAttributeValue();
138
            $options[$attribute->translate()->getName()] = $attributeValue->translate()->getName();
139
        });
140
        
141
        return $options;
142
    }
143
    
144
    public function sortOptions(array &$options)
145
    {
146
        ksort($options);
147
    }
148
}
149