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\Bundle\OrderBundle\Form\DataTransformer; |
14
|
|
|
|
15
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
16
|
|
|
use Doctrine\Common\Collections\Collection; |
17
|
|
|
use Symfony\Component\PropertyAccess\PropertyPathInterface; |
18
|
|
|
use WellCommerce\Bundle\CatalogBundle\Entity\ProductPhoto; |
19
|
|
|
use WellCommerce\Bundle\CoreBundle\Form\DataTransformer\CollectionToArrayTransformer; |
20
|
|
|
use WellCommerce\Bundle\CoreBundle\Helper\Image\ImageHelperInterface; |
21
|
|
|
use WellCommerce\Bundle\OrderBundle\Entity\Order; |
22
|
|
|
use WellCommerce\Bundle\OrderBundle\Entity\OrderProduct; |
23
|
|
|
use WellCommerce\Bundle\OrderBundle\Manager\OrderProductManagerInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class OrderProductCollectionToArrayTransformer |
27
|
|
|
* |
28
|
|
|
* @author Adam Piotrowski <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
final class OrderProductCollectionToArrayTransformer extends CollectionToArrayTransformer |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var OrderProductManagerInterface |
34
|
|
|
*/ |
35
|
|
|
private $manager; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ImageHelperInterface |
39
|
|
|
*/ |
40
|
|
|
private $imageHelper; |
41
|
|
|
|
42
|
|
|
public function setOrderProductManager(OrderProductManagerInterface $manager) |
43
|
|
|
{ |
44
|
|
|
$this->manager = $manager; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function setImageHelper(ImageHelperInterface $imageHelper) |
48
|
|
|
{ |
49
|
|
|
$this->imageHelper = $imageHelper; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function transform($modelData) |
56
|
|
|
{ |
57
|
|
|
$values = []; |
58
|
|
|
|
59
|
|
|
if ($modelData instanceof Collection) { |
60
|
|
|
$modelData->map(function (OrderProduct $orderProduct) use (&$values) { |
61
|
|
|
|
62
|
|
|
$variantId = $orderProduct->hasVariant() ? $orderProduct->getVariant()->getId() : 0; |
63
|
|
|
$photo = $orderProduct->getProduct()->getPhoto(); |
64
|
|
|
$photoPath = $photo instanceof ProductPhoto ? $photo->getPath() : ''; |
65
|
|
|
$symbol = $orderProduct->hasVariant() ? $orderProduct->getVariant()->getSymbol() : $orderProduct->getProduct()->getSku(); |
66
|
|
|
|
67
|
|
|
$values[] = [ |
68
|
|
|
'id' => $orderProduct->getId(), |
69
|
|
|
'product_id' => $orderProduct->getProduct()->getId(), |
70
|
|
|
'product_name' => $orderProduct->getProduct()->translate()->getName(), |
|
|
|
|
71
|
|
|
'gross_amount' => $orderProduct->getSellPrice()->getGrossAmount(), |
72
|
|
|
'quantity' => $orderProduct->getQuantity(), |
73
|
|
|
'ean' => $symbol, |
74
|
|
|
'previousquantity' => $orderProduct->getQuantity(), |
75
|
|
|
'trackstock' => $orderProduct->getProduct()->getTrackStock(), |
76
|
|
|
'tax_rate' => $orderProduct->getSellPrice()->getTaxRate(), |
77
|
|
|
'tax_value' => $orderProduct->getSellPrice()->getTaxAmount(), |
78
|
|
|
'previousvariant' => $variantId, |
79
|
|
|
'variant' => $variantId, |
80
|
|
|
'variant_options' => $this->getVariantOptions($orderProduct), |
81
|
|
|
'weight' => $orderProduct->getWeight(), |
82
|
|
|
'stock' => $orderProduct->getProduct()->getStock(), |
83
|
|
|
'thumb' => null !== $photoPath ? $this->imageHelper->getImage($photoPath, 'medium') : '', |
84
|
|
|
]; |
85
|
|
|
}); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $values; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function getVariantOptions(OrderProduct $orderProduct) |
92
|
|
|
{ |
93
|
|
|
if ($orderProduct->hasVariant()) { |
94
|
|
|
$options = []; |
95
|
|
|
foreach ($orderProduct->getOptions() as $name => $option) { |
96
|
|
|
$options[] = $option; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return implode(', ', $options); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return ''; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function reverseTransform($modelData, PropertyPathInterface $propertyPath, $values) |
106
|
|
|
{ |
107
|
|
|
$collection = new ArrayCollection(); |
108
|
|
|
if ($modelData instanceof Order) { |
109
|
|
|
foreach ($values as $product) { |
110
|
|
|
$orderProduct = $this->manager->addUpdateOrderProduct($product, $modelData); |
111
|
|
|
$collection->add($orderProduct); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->setProducts($modelData, $collection); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function setProducts(Order $order, Collection $products) |
119
|
|
|
{ |
120
|
|
|
$orderProducts = $order->getProducts(); |
121
|
|
|
|
122
|
|
|
$orderProducts->map(function (OrderProduct $orderProduct) use ($products, $orderProducts) { |
123
|
|
|
if (false === $products->contains($orderProduct)) { |
124
|
|
|
$orderProducts->removeElement($orderProduct); |
125
|
|
|
} |
126
|
|
|
}); |
127
|
|
|
|
128
|
|
|
$order->setProducts($products); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.