1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer; |
4
|
|
|
|
5
|
|
|
use Pim\Bundle\CatalogBundle\Model\Product; |
6
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
7
|
|
|
use Pim\Bundle\CatalogBundle\Manager\ProductManager; |
8
|
|
|
|
9
|
|
|
class AssociationNormalizer implements NormalizerInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var ProductManager $productManager |
13
|
|
|
*/ |
14
|
|
|
protected $productManager; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param ProductManager $productManager |
18
|
|
|
*/ |
19
|
|
|
public function __construct(ProductManager $productManager) |
20
|
|
|
{ |
21
|
|
|
$this->productManager = $productManager; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param object $product |
26
|
|
|
* @param null $format |
27
|
|
|
* @param array $context |
28
|
|
|
* @return array|\Symfony\Component\Serializer\Normalizer\scalar |
29
|
|
|
*/ |
30
|
|
|
public function normalize($product, $format = null, array $context = []) |
31
|
|
|
{ |
32
|
|
|
$normalizedAssociations = []; |
33
|
|
|
$identifierAttributeCode = $this->productManager->getIdentifierAttribute( |
34
|
|
|
)->getCode(); |
35
|
|
|
$sku = $product->getValue( |
36
|
|
|
$this->productManager->getIdentifierAttribute()->getCode() |
37
|
|
|
)->getData(); |
38
|
|
|
|
39
|
|
|
$normalizedAssociations['sku'] = $sku; |
40
|
|
|
$normalizedAssociations['family'] = $product->getFamily()->getCode(); |
41
|
|
|
$normalizedAssociations['associations'] = []; |
42
|
|
View Code Duplication |
foreach ($product->getAssociations() as $association) { |
|
|
|
|
43
|
|
|
$associationCode = $association->getAssociationType()->getCode(); |
44
|
|
|
|
45
|
|
|
if ($association->getGroups()->count( |
46
|
|
|
) > 0 || $association->getProducts()->count() > 0 |
47
|
|
|
) { |
48
|
|
|
|
49
|
|
|
/**@var Product $product * */ |
50
|
|
|
$normalizedAssociations['associations'][$associationCode] = [ |
51
|
|
|
'type' => null, |
52
|
|
|
'groups' => [], |
53
|
|
|
'products' => [], |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
$normalizedAssociations['associations'][$associationCode]['type'] = $associationCode; |
57
|
|
|
foreach ($association->getGroups() as $group) { |
58
|
|
|
$normalizedAssociations['associations'][$associationCode]['groups'][] = $group->getCode( |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
foreach ($association->getProducts() as $product) { |
62
|
|
|
$normalizedAssociations['associations'][$associationCode]['products'][] = $product->getValue( |
63
|
|
|
$identifierAttributeCode |
64
|
|
|
)->getData(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $normalizedAssociations; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Checks whether the given class is supported for normalization by this normalizer |
74
|
|
|
* |
75
|
|
|
* @param mixed $data Data to normalize. |
76
|
|
|
* @param string $format The format being (de-)serialized from or into. |
77
|
|
|
* |
78
|
|
|
* @return boolean |
79
|
|
|
*/ |
80
|
|
|
public function supportsNormalization($data, $format = null) |
81
|
|
|
{ |
82
|
|
|
return $data instanceof Product; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.