Skip to content

Commit 90354a6

Browse files
committed
Issue #2981944 by bojanz: Re-organize the base offer plugin classes
1 parent 14ce9da commit 90354a6

12 files changed

+130
-113
lines changed

modules/promotion/src/Plugin/Commerce/PromotionOffer/FixedAmountOffBase.php renamed to modules/promotion/src/Plugin/Commerce/PromotionOffer/FixedAmountOffTrait.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
use Drupal\Core\Form\FormStateInterface;
77

88
/**
9-
* Provides the base class for fixed amount off offers.
9+
* Provides common configuration for fixed amount off offers.
1010
*/
11-
abstract class FixedAmountOffBase extends PromotionOfferBase {
11+
trait FixedAmountOffTrait {
1212

1313
/**
1414
* {@inheritdoc}
@@ -47,8 +47,10 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
4747
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
4848
parent::submitConfigurationForm($form, $form_state);
4949

50-
$values = $form_state->getValue($form['#parents']);
51-
$this->configuration['amount'] = $values['amount'];
50+
if (!$form_state->getErrors()) {
51+
$values = $form_state->getValue($form['#parents']);
52+
$this->configuration['amount'] = $values['amount'];
53+
}
5254
}
5355

5456
/**

modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderFixedAmountOff.php

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
namespace Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer;
44

55
use Drupal\commerce_order\Adjustment;
6-
use Drupal\commerce_order\PriceSplitterInterface;
7-
use Drupal\commerce_price\RounderInterface;
86
use Drupal\commerce_promotion\Entity\PromotionInterface;
97
use Drupal\Core\Entity\EntityInterface;
10-
use Symfony\Component\DependencyInjection\ContainerInterface;
118

129
/**
1310
* Provides the fixed amount off offer for orders.
@@ -20,47 +17,9 @@
2017
* entity_type = "commerce_order",
2118
* )
2219
*/
23-
class OrderFixedAmountOff extends FixedAmountOffBase {
20+
class OrderFixedAmountOff extends OrderPromotionOfferBase {
2421

25-
/**
26-
* The price splitter.
27-
*
28-
* @var \Drupal\commerce_order\PriceSplitterInterface
29-
*/
30-
protected $splitter;
31-
32-
/**
33-
* Constructs a new OrderFixedAmountOff object.
34-
*
35-
* @param array $configuration
36-
* A configuration array containing information about the plugin instance.
37-
* @param string $plugin_id
38-
* The pluginId for the plugin instance.
39-
* @param mixed $plugin_definition
40-
* The plugin implementation definition.
41-
* @param \Drupal\commerce_price\RounderInterface $rounder
42-
* The rounder.
43-
* @param \Drupal\commerce_order\PriceSplitterInterface $splitter
44-
* The splitter.
45-
*/
46-
public function __construct(array $configuration, $plugin_id, $plugin_definition, RounderInterface $rounder, PriceSplitterInterface $splitter) {
47-
parent::__construct($configuration, $plugin_id, $plugin_definition, $rounder);
48-
49-
$this->splitter = $splitter;
50-
}
51-
52-
/**
53-
* {@inheritdoc}
54-
*/
55-
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
56-
return new static(
57-
$configuration,
58-
$plugin_id,
59-
$plugin_definition,
60-
$container->get('commerce_price.rounder'),
61-
$container->get('commerce_order.price_splitter')
62-
);
63-
}
22+
use FixedAmountOffTrait;
6423

6524
/**
6625
* {@inheritdoc}

modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderItemFixedAmountOff.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
* entity_type = "commerce_order_item",
1616
* )
1717
*/
18-
class OrderItemFixedAmountOff extends FixedAmountOffBase {
18+
class OrderItemFixedAmountOff extends OrderItemPromotionOfferBase {
19+
20+
use FixedAmountOffTrait;
1921

2022
/**
2123
* {@inheritdoc}
@@ -25,11 +27,11 @@ public function apply(EntityInterface $entity, PromotionInterface $promotion) {
2527
/** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
2628
$order_item = $entity;
2729
$total_price = $order_item->getTotalPrice();
28-
$adjustment_amount = $this->getAmount();
29-
if ($total_price->getCurrencyCode() != $adjustment_amount->getCurrencyCode()) {
30+
$amount = $this->getAmount();
31+
if ($total_price->getCurrencyCode() != $amount->getCurrencyCode()) {
3032
return;
3133
}
32-
$adjustment_amount = $adjustment_amount->multiply($order_item->getQuantity());
34+
$adjustment_amount = $amount->multiply($order_item->getQuantity());
3335
$adjustment_amount = $this->rounder->round($adjustment_amount);
3436
// Don't reduce the order item total price past zero.
3537
if ($adjustment_amount->greaterThan($total_price)) {

modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderItemPercentageOff.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
* entity_type = "commerce_order_item",
1616
* )
1717
*/
18-
class OrderItemPercentageOff extends PercentageOffBase {
18+
class OrderItemPercentageOff extends OrderItemPromotionOfferBase {
19+
20+
use PercentageOffTrait;
1921

2022
/**
2123
* {@inheritdoc}
@@ -24,15 +26,16 @@ public function apply(EntityInterface $entity, PromotionInterface $promotion) {
2426
$this->assertEntity($entity);
2527
/** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
2628
$order_item = $entity;
27-
$adjustment_amount = $order_item->getTotalPrice()->multiply($this->getPercentage());
29+
$percentage = $this->getPercentage();
30+
$adjustment_amount = $order_item->getTotalPrice()->multiply($percentage);
2831
$adjustment_amount = $this->rounder->round($adjustment_amount);
2932

3033
$order_item->addAdjustment(new Adjustment([
3134
'type' => 'promotion',
3235
// @todo Change to label from UI when added in #2770731.
3336
'label' => t('Discount'),
3437
'amount' => $adjustment_amount->multiply('-1'),
35-
'percentage' => $this->getPercentage(),
38+
'percentage' => $percentage,
3639
'source_id' => $promotion->id(),
3740
]));
3841
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer;
4+
5+
/**
6+
* Provides the base class for order item offers.
7+
*/
8+
abstract class OrderItemPromotionOfferBase extends PromotionOfferBase implements OrderItemPromotionOfferInterface {
9+
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer;
4+
5+
/**
6+
* Defines the interface for order item offers.
7+
*/
8+
interface OrderItemPromotionOfferInterface extends PromotionOfferInterface {
9+
10+
}

modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderPercentageOff.php

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
namespace Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer;
44

55
use Drupal\commerce_order\Adjustment;
6-
use Drupal\commerce_order\PriceSplitterInterface;
7-
use Drupal\commerce_price\RounderInterface;
86
use Drupal\commerce_promotion\Entity\PromotionInterface;
97
use Drupal\Core\Entity\EntityInterface;
10-
use Symfony\Component\DependencyInjection\ContainerInterface;
118

129
/**
1310
* Provides the percentage off offer for orders.
@@ -20,47 +17,9 @@
2017
* entity_type = "commerce_order",
2118
* )
2219
*/
23-
class OrderPercentageOff extends PercentageOffBase {
20+
class OrderPercentageOff extends OrderPromotionOfferBase {
2421

25-
/**
26-
* The price splitter.
27-
*
28-
* @var \Drupal\commerce_order\PriceSplitterInterface
29-
*/
30-
protected $splitter;
31-
32-
/**
33-
* Constructs a new OrderPercentageOff object.
34-
*
35-
* @param array $configuration
36-
* A configuration array containing information about the plugin instance.
37-
* @param string $plugin_id
38-
* The pluginId for the plugin instance.
39-
* @param mixed $plugin_definition
40-
* The plugin implementation definition.
41-
* @param \Drupal\commerce_price\RounderInterface $rounder
42-
* The rounder.
43-
* @param \Drupal\commerce_order\PriceSplitterInterface $splitter
44-
* The splitter.
45-
*/
46-
public function __construct(array $configuration, $plugin_id, $plugin_definition, RounderInterface $rounder, PriceSplitterInterface $splitter) {
47-
parent::__construct($configuration, $plugin_id, $plugin_definition, $rounder);
48-
49-
$this->splitter = $splitter;
50-
}
51-
52-
/**
53-
* {@inheritdoc}
54-
*/
55-
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
56-
return new static(
57-
$configuration,
58-
$plugin_id,
59-
$plugin_definition,
60-
$container->get('commerce_price.rounder'),
61-
$container->get('commerce_order.price_splitter')
62-
);
63-
}
22+
use PercentageOffTrait;
6423

6524
/**
6625
* {@inheritdoc}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer;
4+
5+
use Drupal\commerce_order\PriceSplitterInterface;
6+
use Drupal\commerce_price\RounderInterface;
7+
use Symfony\Component\DependencyInjection\ContainerInterface;
8+
9+
/**
10+
* Provides the base class for order offers.
11+
*/
12+
abstract class OrderPromotionOfferBase extends PromotionOfferBase implements OrderPromotionOfferInterface {
13+
14+
/**
15+
* The price splitter.
16+
*
17+
* @var \Drupal\commerce_order\PriceSplitterInterface
18+
*/
19+
protected $splitter;
20+
21+
/**
22+
* Constructs a new OrderPromotionOfferBase object.
23+
*
24+
* @param array $configuration
25+
* A configuration array containing information about the plugin instance.
26+
* @param string $plugin_id
27+
* The pluginId for the plugin instance.
28+
* @param mixed $plugin_definition
29+
* The plugin implementation definition.
30+
* @param \Drupal\commerce_price\RounderInterface $rounder
31+
* The rounder.
32+
* @param \Drupal\commerce_order\PriceSplitterInterface $splitter
33+
* The splitter.
34+
*/
35+
public function __construct(array $configuration, $plugin_id, $plugin_definition, RounderInterface $rounder, PriceSplitterInterface $splitter) {
36+
parent::__construct($configuration, $plugin_id, $plugin_definition, $rounder);
37+
38+
$this->splitter = $splitter;
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
45+
return new static(
46+
$configuration,
47+
$plugin_id,
48+
$plugin_definition,
49+
$container->get('commerce_price.rounder'),
50+
$container->get('commerce_order.price_splitter')
51+
);
52+
}
53+
54+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer;
4+
5+
/**
6+
* Defines the interface for order offers.
7+
*/
8+
interface OrderPromotionOfferInterface extends PromotionOfferInterface {}

modules/promotion/src/Plugin/Commerce/PromotionOffer/PercentageOffBase.php renamed to modules/promotion/src/Plugin/Commerce/PromotionOffer/PercentageOffTrait.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
use Drupal\Core\Form\FormStateInterface;
77

88
/**
9-
* Provides the base class for percentage off offers.
9+
* Provides common configuration for percentage off offers.
1010
*/
11-
abstract class PercentageOffBase extends PromotionOfferBase {
11+
trait PercentageOffTrait {
1212

1313
/**
1414
* {@inheritdoc}
@@ -32,16 +32,6 @@ public function setConfiguration(array $configuration) {
3232
}
3333
}
3434

35-
/**
36-
* Gets the percentage.
37-
*
38-
* @return string
39-
* The percentage.
40-
*/
41-
public function getPercentage() {
42-
return (string) $this->configuration['percentage'];
43-
}
44-
4535
/**
4636
* {@inheritdoc}
4737
*/
@@ -79,8 +69,20 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form
7969
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
8070
parent::submitConfigurationForm($form, $form_state);
8171

82-
$values = $form_state->getValue($form['#parents']);
83-
$this->configuration['percentage'] = (string) ($values['percentage'] / 100);
72+
if (!$form_state->getErrors()) {
73+
$values = $form_state->getValue($form['#parents']);
74+
$this->configuration['percentage'] = (string) ($values['percentage'] / 100);
75+
}
76+
}
77+
78+
/**
79+
* Gets the percentage.
80+
*
81+
* @return string
82+
* The percentage.
83+
*/
84+
protected function getPercentage() {
85+
return (string) $this->configuration['percentage'];
8486
}
8587

8688
}

0 commit comments

Comments
 (0)