|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Actualys\Bundle\DrupalCommerceConnectorBundle\Processor; |
|
4
|
|
|
|
|
5
|
|
|
use Akeneo\Bundle\BatchBundle\Entity\StepExecution; |
|
6
|
|
|
use Akeneo\Bundle\BatchBundle\Item\InvalidItemException; |
|
7
|
|
|
use Akeneo\Bundle\BatchBundle\Item\ItemProcessorInterface; |
|
8
|
|
|
use Pim\Bundle\CatalogBundle\Manager\ProductManager; |
|
9
|
|
|
use Pim\Bundle\CatalogBundle\Manager\ChannelManager; |
|
10
|
|
|
use Pim\Bundle\CatalogBundle\Entity\Channel; |
|
11
|
|
|
use Pim\Bundle\CatalogBundle\Model\ProductInterface; |
|
12
|
|
|
use Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer\Exception\NormalizeException; |
|
13
|
|
|
use Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer\ProductNormalizer; |
|
14
|
|
|
use Actualys\Bundle\DrupalCommerceConnectorBundle\Item\DrupalItemStep; |
|
15
|
|
|
|
|
16
|
|
|
class ProductProcessor extends DrupalItemStep implements ItemProcessorInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var StepExecution */ |
|
19
|
|
|
protected $stepExecution; |
|
20
|
|
|
|
|
21
|
|
|
/** @var ProductManager */ |
|
22
|
|
|
protected $productManager; |
|
23
|
|
|
|
|
24
|
|
|
/** @var ChannelManager */ |
|
25
|
|
|
protected $channelManager; |
|
26
|
|
|
|
|
27
|
|
|
/** @var array */ |
|
28
|
|
|
protected $globalContext; |
|
29
|
|
|
|
|
30
|
|
|
/** @var ProductNormalizer */ |
|
31
|
|
|
protected $productNormalizer; |
|
32
|
|
|
|
|
33
|
|
|
/** @var Channel $channel */ |
|
34
|
|
|
protected $channel; |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param ProductManager $productManager |
|
39
|
|
|
* @param ChannelManager $channelManager |
|
40
|
|
|
* @param ProductNormalizer $productNormalizer |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct( |
|
43
|
|
|
ProductManager $productManager, |
|
44
|
|
|
ChannelManager $channelManager, |
|
45
|
|
|
ProductNormalizer $productNormalizer |
|
46
|
|
|
) { |
|
47
|
|
|
$this->productManager = $productManager; |
|
48
|
|
|
$this->channelManager = $channelManager; |
|
49
|
|
|
$this->productNormalizer = $productNormalizer; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param mixed $product |
|
54
|
|
|
* @return array|mixed |
|
55
|
|
|
* @throws InvalidItemException |
|
56
|
|
|
*/ |
|
57
|
|
|
public function process($product) |
|
58
|
|
|
{ |
|
59
|
|
|
/** @var Channel $channel */ |
|
60
|
|
|
$channel = $this->channelManager->getChannelByCode($this->channel); |
|
61
|
|
|
|
|
62
|
|
|
return $this->normalizeProduct($product, $channel); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Normalize the given product |
|
67
|
|
|
* |
|
68
|
|
|
* @param ProductInterface $product [description] |
|
69
|
|
|
* @param Channel $channel |
|
70
|
|
|
* |
|
71
|
|
|
* @throws InvalidItemException If a normalization error occurs |
|
72
|
|
|
* |
|
73
|
|
|
* @return array processed item |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function normalizeProduct( |
|
76
|
|
|
ProductInterface $product, |
|
77
|
|
|
Channel $channel |
|
78
|
|
|
) { |
|
79
|
|
|
try { |
|
80
|
|
|
$processedItem = $this->productNormalizer->normalize( |
|
81
|
|
|
$product, |
|
82
|
|
|
null, |
|
83
|
|
|
[ |
|
84
|
|
|
'channel' => $channel, |
|
85
|
|
|
'configuration' => $this->getConfiguration() |
|
86
|
|
|
] |
|
87
|
|
|
); |
|
88
|
|
|
} catch (NormalizeException $e) { |
|
89
|
|
|
throw new InvalidItemException( |
|
90
|
|
|
$e->getMessage(), |
|
91
|
|
|
[ |
|
92
|
|
|
'id' => $product->getId( |
|
93
|
|
|
), |
|
94
|
|
|
$product->getIdentifier()->getAttribute()->getCode( |
|
95
|
|
|
) => $product->getIdentifier( |
|
96
|
|
|
)->getData(), |
|
97
|
|
|
'label' => $product->getLabel( |
|
98
|
|
|
), |
|
99
|
|
|
'family' => $product->getFamily( |
|
100
|
|
|
)->getCode() |
|
101
|
|
|
] |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $processedItem; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get fields for the twig |
|
110
|
|
|
* |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getConfigurationFields() |
|
114
|
|
|
{ |
|
115
|
|
|
return array_merge( |
|
|
|
|
|
|
116
|
|
|
parent::getConfigurationFields(), |
|
117
|
|
|
[ |
|
118
|
|
|
'channel' => [ |
|
119
|
|
|
'type' => 'choice', |
|
120
|
|
|
'options' => [ |
|
121
|
|
|
'choices' => $this->channelManager->getChannelChoices(), |
|
122
|
|
|
'required' => true, |
|
123
|
|
|
'help' => 'actualys_drupal_commerce_connector.export.channel.help', |
|
124
|
|
|
'label' => 'actualys_drupal_commerce_connector.export.channel.label', |
|
125
|
|
|
], |
|
126
|
|
|
], |
|
127
|
|
|
] |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return Channel |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getChannel() |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->channel; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param Channel $channel |
|
141
|
|
|
*/ |
|
142
|
|
|
public function setChannel($channel) |
|
143
|
|
|
{ |
|
144
|
|
|
$this->channel = $channel; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.