AttributeOptionProcessor::process()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 3
eloc 11
nc 4
nop 1
1
<?php
2
3
namespace Actualys\Bundle\DrupalCommerceConnectorBundle\Processor;
4
5
use Akeneo\Bundle\BatchBundle\Item\InvalidItemException;
6
use Akeneo\Bundle\BatchBundle\Entity\StepExecution;
7
use Akeneo\Bundle\BatchBundle\Item\ItemProcessorInterface;
8
use Pim\Bundle\CatalogBundle\Entity\AttributeOption;
9
use Pim\Bundle\CatalogBundle\Entity\Attribute;
10
use Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer\Exception\NormalizeException;
11
use Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer\AttributeOptionNormalizer;
12
use Actualys\Bundle\DrupalCommerceConnectorBundle\Item\DrupalItemStep;
13
14
class AttributeOptionProcessor extends DrupalItemStep implements ItemProcessorInterface
15
{
16
    /**
17
     * @var AttributeOptionNormalizer
18
     */
19
    protected $attributeOptionNormalizer;
20
21
    /**
22
     * @var array
23
     */
24
    protected $globalContext = array();
25
26
    /**
27
     * @param AttributeOptionNormalizer $attributeOptionNormalizer The entity manager
28
     */
29
    public function __construct(
30
      AttributeOptionNormalizer $attributeOptionNormalizer
31
    ) {
32
        $this->attributeOptionNormalizer = $attributeOptionNormalizer;
33
    }
34
35
    /**
36
     * @param  Attribute            $attribute
37
     * @return array|mixed
38
     * @throws InvalidItemException
39
     */
40
    public function process($attribute)
41
    {
42
        $result = [
43
          'code'    => $attribute->getCode(),
44
          'labels'  => array(),
45
          'options' => array(),
46
        ];
47
48
        foreach ($attribute->getTranslations() as $trans) {
49
            $result['labels'][$trans->getLocale()] = $trans->getLabel();
50
        }
51
        foreach ($attribute->getOptions() as $attributeOption) {
52
            $result['options'][$attributeOption->getCode(
53
            )] = $this->normalizeOption($attributeOption, $this->globalContext);
54
        }
55
56
        return $result;
57
    }
58
59
    /**
60
     * @param  AttributeOption      $attributeOptionNormalizer
61
     * @param  array                $context
62
     * @return mixed
63
     * @throws InvalidItemException
64
     */
65
    protected function normalizeOption(
66
      AttributeOption $attributeOptionNormalizer,
67
      array $context
68
    ) {
69
        try {
70
            $processedItem = $this->attributeOptionNormalizer->normalize(
71
              $attributeOptionNormalizer,
72
              $context
73
            );
74
        } catch (NormalizeException $e) {
75
            throw new InvalidItemException(
76
              $e->getMessage(),
77
              [$attributeOptionNormalizer]
78
            );
79
        }
80
81
        return $processedItem;
82
    }
83
84
    public function getConfigurationFields()
85
    {
86
        return parent::getConfigurationFields();
87
    }
88
89
    /**
90
     * @param StepExecution $stepExecution
91
     */
92
    public function setStepExecution(StepExecution $stepExecution)
93
    {
94
        $this->stepExecution = $stepExecution;
95
    }
96
}
97