|
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\Category; |
|
9
|
|
|
use Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer\Exception\NormalizeException; |
|
10
|
|
|
use Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer\CategoryNormalizer; |
|
11
|
|
|
use Actualys\Bundle\DrupalCommerceConnectorBundle\Item\DrupalItemStep; |
|
12
|
|
|
|
|
13
|
|
|
class CategoryProcessor extends DrupalItemStep implements ItemProcessorInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var CategoryNormalizer |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $categoryNormalizer; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $globalContext = array(); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param CategoryNormalizer $categoryNormalizer The entity manager |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(CategoryNormalizer $categoryNormalizer) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->categoryNormalizer = $categoryNormalizer; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param mixed $categories |
|
35
|
|
|
* @return array|mixed |
|
36
|
|
|
* @throws InvalidItemException |
|
37
|
|
|
*/ |
|
38
|
|
|
public function process($categories) |
|
39
|
|
|
{ |
|
40
|
|
|
$result = []; |
|
41
|
|
|
foreach ($categories as $category) { |
|
42
|
|
|
$result[$category->getCode()] = $this->normalizeCategory( |
|
43
|
|
|
$category, |
|
44
|
|
|
$this->globalContext |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $result; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param Category $category |
|
53
|
|
|
* @param array $context |
|
54
|
|
|
* @return array|\Symfony\Component\Serializer\Normalizer\scalar |
|
55
|
|
|
* @throws InvalidItemException |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function normalizeCategory(Category $category, array $context) |
|
58
|
|
|
{ |
|
59
|
|
|
try { |
|
60
|
|
|
$processedItem = $this->categoryNormalizer->normalize( |
|
61
|
|
|
$category, |
|
62
|
|
|
$context |
|
63
|
|
|
); |
|
64
|
|
|
} catch (NormalizeException $e) { |
|
65
|
|
|
throw new InvalidItemException($e->getMessage(), [$category]); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $processedItem; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function getConfigurationFields() |
|
72
|
|
|
{ |
|
73
|
|
|
return parent::getConfigurationFields(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param StepExecution $stepExecution |
|
78
|
|
|
*/ |
|
79
|
|
|
public function setStepExecution(StepExecution $stepExecution) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->stepExecution = $stepExecution; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|