These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | namespace WellCommerce\Bundle\OrderBundle\Form\Front; |
||
13 | |||
14 | use WellCommerce\Bundle\CoreBundle\Form\AbstractFormBuilder; |
||
15 | use WellCommerce\Bundle\OrderBundle\Context\OrderContext; |
||
16 | use WellCommerce\Bundle\OrderBundle\Entity\Order; |
||
17 | use WellCommerce\Bundle\OrderBundle\Entity\PaymentMethod; |
||
18 | use WellCommerce\Bundle\OrderBundle\Entity\ShippingMethod; |
||
19 | use WellCommerce\Bundle\OrderBundle\Entity\ShippingMethodCost; |
||
20 | use WellCommerce\Bundle\OrderBundle\Provider\Front\OrderProviderInterface; |
||
21 | use WellCommerce\Bundle\OrderBundle\Provider\ShippingMethodOptionsProviderInterface; |
||
22 | use WellCommerce\Bundle\OrderBundle\Provider\ShippingMethodProviderInterface; |
||
23 | use WellCommerce\Component\Form\Elements\ElementInterface; |
||
24 | use WellCommerce\Component\Form\Elements\FormInterface; |
||
25 | use WellCommerce\Component\Form\Elements\Optioned\RadioGroup; |
||
26 | |||
27 | /** |
||
28 | * Class CartFormBuilder |
||
29 | * |
||
30 | * @author Adam Piotrowski <[email protected]> |
||
31 | */ |
||
32 | class CartFormBuilder extends AbstractFormBuilder |
||
33 | { |
||
34 | public function getAlias(): string |
||
35 | { |
||
36 | return 'front.cart'; |
||
37 | } |
||
38 | |||
39 | public function buildForm(FormInterface $form) |
||
40 | { |
||
41 | $order = $this->getOrderProvider()->getCurrentOrder(); |
||
42 | |||
43 | $shippingAddress = $form->addChild($this->getElement('nested_fieldset', [ |
||
44 | 'name' => 'shippingAddress', |
||
45 | 'label' => 'client.heading.shipping_address', |
||
46 | ])); |
||
47 | |||
48 | $shippingAddress->addChild($this->getElement('select', [ |
||
49 | 'name' => 'shippingAddress.country', |
||
50 | 'label' => 'client.label.address.country', |
||
51 | 'options' => $this->get('country.repository')->all(), |
||
52 | 'default' => $this->getShopStorage()->getCurrentShop()->getDefaultCountry(), |
||
53 | ])); |
||
54 | |||
55 | $shippingMethods = $form->addChild($this->getElement('radio_group', [ |
||
56 | 'name' => 'shippingMethod', |
||
57 | 'label' => 'order.label.shipping_method', |
||
58 | 'transformer' => $this->getRepositoryTransformer('entity', $this->get('shipping_method.repository')), |
||
59 | ])); |
||
60 | |||
61 | $this->addShippingMethods($order, $shippingMethods); |
||
62 | |||
63 | $this->addShippingOptions($order, $form); |
||
64 | |||
65 | $paymentMethods = $form->addChild($this->getElement('radio_group', [ |
||
66 | 'name' => 'paymentMethod', |
||
67 | 'label' => 'order.label.payment_method', |
||
68 | 'transformer' => $this->getRepositoryTransformer('entity', $this->get('payment_method.repository')), |
||
69 | ])); |
||
70 | |||
71 | $this->addPaymentMethods($order, $paymentMethods); |
||
72 | |||
73 | $form->addFilter($this->getFilter('no_code')); |
||
74 | $form->addFilter($this->getFilter('trim')); |
||
75 | $form->addFilter($this->getFilter('secure')); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Adds shipping options if available for order's shipping method |
||
80 | * |
||
81 | * @param Order $order |
||
82 | * @param FormInterface $form |
||
83 | */ |
||
84 | private function addShippingOptions(Order $order, FormInterface $form) |
||
85 | { |
||
86 | if ($order->getShippingMethod() instanceof ShippingMethod) { |
||
87 | $provider = $this->getOptionsProvider($order->getShippingMethod()); |
||
88 | if ($provider instanceof ShippingMethodOptionsProviderInterface) { |
||
89 | $form->addChild($this->getElement('select', [ |
||
90 | 'name' => 'shippingMethodOption', |
||
91 | 'label' => 'order.label.shipping_method', |
||
92 | 'options' => $provider->getShippingOptions(), |
||
93 | ])); |
||
94 | } |
||
95 | } |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Adds shipping method options to select |
||
100 | * |
||
101 | * @param Order $order |
||
102 | * @param ElementInterface|RadioGroup $radioGroup |
||
103 | */ |
||
104 | private function addShippingMethods(Order $order, ElementInterface $radioGroup) |
||
105 | { |
||
106 | $collection = $this->getShippingMethodProvider()->getCosts(new OrderContext($order)); |
||
107 | |||
108 | $collection->map(function (ShippingMethodCost $shippingMethodCost) use ($radioGroup) { |
||
109 | $shippingMethod = $shippingMethodCost->getShippingMethod(); |
||
110 | $baseCurrency = $shippingMethod->getCurrency()->getCode(); |
||
111 | $grossAmount = $shippingMethodCost->getCost()->getGrossAmount(); |
||
112 | |||
113 | $label = [ |
||
114 | 'name' => $shippingMethod->translate()->getName(), |
||
115 | 'comment' => $this->getCurrencyHelper()->convertAndFormat($grossAmount, $baseCurrency), |
||
116 | ]; |
||
117 | |||
118 | $radioGroup->addOptionToSelect($shippingMethod->getId(), $label); |
||
119 | }); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Adds payment method options to select |
||
124 | * |
||
125 | * @param Order $order |
||
126 | * @param ElementInterface|RadioGroup $radioGroup |
||
127 | */ |
||
128 | private function addPaymentMethods(Order $order, ElementInterface $radioGroup) |
||
0 ignored issues
–
show
|
|||
129 | { |
||
130 | $order = $this->getOrderProvider()->getCurrentOrder(); |
||
131 | |||
132 | if ($order->getShippingMethod() instanceof ShippingMethod) { |
||
133 | $collection = $order->getShippingMethod()->getPaymentMethods(); |
||
134 | |||
135 | $collection->map(function (PaymentMethod $paymentMethod) use ($radioGroup) { |
||
136 | if ($paymentMethod->isEnabled()) { |
||
137 | $radioGroup->addOptionToSelect($paymentMethod->getId(), $paymentMethod->translate()->getName()); |
||
138 | } |
||
139 | }); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | private function getShippingMethodProvider(): ShippingMethodProviderInterface |
||
144 | { |
||
145 | return $this->get('shipping_method.provider'); |
||
146 | } |
||
147 | |||
148 | private function getOrderProvider(): OrderProviderInterface |
||
149 | { |
||
150 | return $this->get('order.provider.front'); |
||
151 | } |
||
152 | |||
153 | private function getOptionsProvider(ShippingMethod $method) |
||
154 | { |
||
155 | $provider = $method->getOptionsProvider(); |
||
156 | $collection = $this->get('shipping_method.options_provider.collection'); |
||
157 | |||
158 | if ($collection->containsKey($provider)) { |
||
159 | return $collection->get($provider); |
||
160 | } |
||
161 | |||
162 | return null; |
||
163 | } |
||
164 | } |
||
165 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.