VariantEditor   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 5
dl 0
loc 72
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configureOptions() 0 49 2
A prepareAttributesCollection() 0 19 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\Component\Form\Elements\Editor;
14
15
use Symfony\Component\OptionsResolver\Options;
16
use Symfony\Component\OptionsResolver\OptionsResolver;
17
use WellCommerce\Component\Form\Elements\AbstractField;
18
use WellCommerce\Component\Form\Elements\Attribute;
19
use WellCommerce\Component\Form\Elements\AttributeCollection;
20
use WellCommerce\Component\Form\Elements\ElementInterface;
21
22
/**
23
 * Class VariantEditor
24
 *
25
 * @author  Adam Piotrowski <[email protected]>
26
 */
27
class VariantEditor extends AbstractField implements ElementInterface
28
{
29
    public function configureOptions(OptionsResolver $resolver)
30
    {
31
        parent::configureOptions($resolver);
32
        
33
        $resolver->setRequired([
34
            'set',
35
            'attribute_group_field',
36
            'category_field',
37
            'photo_field',
38
            'vat_values',
39
            'allow_generate',
40
            'suffixes',
41
            'get_groups_route',
42
            'get_photos_route',
43
            'get_attributes_route',
44
        ]);
45
        
46
        $resolver->setDefaults([
47
            'set'                         => null,
48
            'allow_generate'              => true,
49
            'suffixes'                    => ['+', '-', '%', '='],
50
            'availability'                => [],
51
            'get_groups_route'            => 'admin.attribute_group.ajax.index',
52
            'get_photos_route'            => 'admin.media.grid',
53
            'get_attributes_route'        => 'admin.attribute.ajax.index',
54
            'get_attributes_values_route' => 'admin.attribute_value.ajax.index',
55
            'add_attribute_route'         => 'admin.attribute.ajax.add',
56
            'add_attribute_value_route'   => 'admin.attribute_value.ajax.add',
57
            'generate_cartesian_route'    => 'admin.attribute.ajax.generate',
58
        ]);
59
        
60
        $resolver->setAllowedTypes('allow_generate', 'bool');
61
        $resolver->setAllowedTypes('attribute_group_field', ElementInterface::class);
62
        $resolver->setAllowedTypes('vat_values', 'array');
63
        $resolver->setAllowedTypes('availability', 'array');
64
        $resolver->setAllowedTypes('category_field', ElementInterface::class);
65
        
66
        $fieldNormalizer = function (Options $options, $value) {
67
            if (!$value instanceof ElementInterface) {
68
                throw new \InvalidArgumentException('Passed field should implement "ElementInterface" and have accessible "getName" method.');
69
            }
70
            
71
            return $value->getName();
72
        };
73
        
74
        $resolver->setNormalizer('attribute_group_field', $fieldNormalizer);
75
        $resolver->setNormalizer('category_field', $fieldNormalizer);
76
        $resolver->setNormalizer('photo_field', $fieldNormalizer);
77
    }
78
    
79
    public function prepareAttributesCollection(AttributeCollection $collection)
80
    {
81
        parent::prepareAttributesCollection($collection);
82
        $collection->add(new Attribute('sGetGroupsRoute', $this->getOption('get_groups_route')));
83
        $collection->add(new Attribute('sGetPhotosRoute', $this->getOption('get_photos_route')));
84
        $collection->add(new Attribute('sGetAttributesRoute', $this->getOption('get_attributes_route')));
85
        $collection->add(new Attribute('sGetAttributesValuesRoute', $this->getOption('get_attributes_values_route')));
86
        $collection->add(new Attribute('sAddAttributeRoute', $this->getOption('add_attribute_route')));
87
        $collection->add(new Attribute('sAddAttributeValueRoute', $this->getOption('add_attribute_value_route')));
88
        $collection->add(new Attribute('sGenerateCartesianRoute', $this->getOption('generate_cartesian_route')));
89
        $collection->add(new Attribute('sCategoryField', $this->getOption('category_field')));
90
        $collection->add(new Attribute('sPhotoField', $this->getOption('photo_field')));
91
        $collection->add(new Attribute('bAllowGenerate', $this->getOption('allow_generate'), Attribute::TYPE_BOOLEAN));
92
        $collection->add(new Attribute('aoVatValues', $this->getOption('vat_values'), Attribute::TYPE_ARRAY));
93
        $collection->add(new Attribute('aoAvailability', $this->getOption('availability'), Attribute::TYPE_ARRAY));
94
        $collection->add(new Attribute('aoSuffixes', $this->getOption('suffixes'), Attribute::TYPE_ARRAY));
95
        $collection->add(new Attribute('sAttributeGroupField', $this->getOption('attribute_group_field')));
96
        $collection->add(new Attribute('sSet', $this->getOption('set')));
97
    }
98
}
99