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

VariantCollectionToArrayTransformer::transform()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 17
nc 2
nop 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\Form\DataTransformer;
14
15
use Doctrine\Common\Collections\Collection;
16
use Symfony\Component\PropertyAccess\PropertyPathInterface;
17
use WellCommerce\Bundle\AppBundle\Entity\Media;
18
use WellCommerce\Bundle\CatalogBundle\Entity\Availability;
19
use WellCommerce\Bundle\CatalogBundle\Entity\Product;
20
use WellCommerce\Bundle\CatalogBundle\Entity\Variant;
21
use WellCommerce\Bundle\CatalogBundle\Entity\VariantOption;
22
use WellCommerce\Bundle\CatalogBundle\Manager\VariantManager;
23
use WellCommerce\Bundle\CoreBundle\Form\DataTransformer\CollectionToArrayTransformer;
24
25
/**
26
 * Class VariantCollectionToArrayTransformer
27
 *
28
 * @author  Adam Piotrowski <[email protected]>
29
 */
30
class VariantCollectionToArrayTransformer extends CollectionToArrayTransformer
31
{
32
    /**
33
     * @var VariantManager
34
     */
35
    protected $variantManager;
36
    
37
    /**
38
     * @param VariantManager $variantManager
39
     */
40
    public function setVariantManager(VariantManager $variantManager)
41
    {
42
        $this->variantManager = $variantManager;
43
    }
44
    
45
    public function transform($modelData)
46
    {
47
        $values = [];
48
        
49
        if ($modelData instanceof Collection) {
50
            $modelData->map(function (Variant $variant) use (&$values) {
51
                $values[] = [
52
                    'id'           => $variant->getId(),
53
                    'suffix'       => $variant->getModifierType(),
54
                    'modifier'     => $variant->getModifierValue(),
55
                    'stock'        => $variant->getStock(),
56
                    'symbol'       => $variant->getSymbol(),
57
                    'hierarchy'    => $variant->getHierarchy(),
58
                    'status'       => $variant->isEnabled(),
59
                    'weight'       => $variant->getWeight(),
60
                    'availability' => $this->transformAvailability($variant->getAvailability()),
61
                    'attributes'   => $this->transformOptions($variant->getOptions()),
62
                    'photo'        => $this->transformPhoto($variant->getPhoto()),
63
                ];
64
            });
65
        }
66
        
67
        return $values;
68
    }
69
    
70
    public function reverseTransform($modelData, PropertyPathInterface $propertyPath, $values)
71
    {
72
        if ($modelData instanceof Product && null !== $values) {
73
            $collection = $this->variantManager->getAttributesCollectionForProduct($modelData, $values);
74
            $modelData->setVariants($collection);
75
        }
76
    }
77
    
78
    private function transformAvailability(Availability $availability = null)
79
    {
80
        if (null !== $availability) {
81
            return $availability->getId();
82
        }
83
        
84
        return null;
85
    }
86
    
87
    private function transformOptions(Collection $collection = null): array
88
    {
89
        if (null === $collection) {
90
            return [];
91
        }
92
        
93
        $values = [];
94
        $collection->map(function (VariantOption $variantOption) use (&$values) {
95
            $values[$variantOption->getAttribute()->getId()] = $variantOption->getAttributeValue()->getId();
96
        });
97
        
98
        return $values;
99
    }
100
    
101
    private function transformPhoto(Media $photo = null)
102
    {
103
        if (null !== $photo) {
104
            return $photo->getId();
105
        }
106
        
107
        return null;
108
    }
109
}
110