Skip to content

Commit

Permalink
extensibility enhancements (factories and final classes) (#3724)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitek-rostislav authored Jan 23, 2025
2 parents 734211a + c8baaf5 commit 4544fd2
Show file tree
Hide file tree
Showing 63 changed files with 536 additions and 443 deletions.
6 changes: 3 additions & 3 deletions src/Controller/Admin/ProductPickerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@ public function basicProductPriceAction(Request $request): Response
$productId = (int)$request->get('productId');
$domainId = (int)$request->get('domainId');

$basicPrice = $this->productFacade->getProductSellingPriceForDefaultPricingGroup(
$basicPrice = $this->productFacade->getProductPriceForDefaultPricingGroup(
$this->productFacade->getById($productId),
$domainId,
);

$basicPriceAmount = $this->pricingSetting->getInputPriceType() === PricingSetting::INPUT_PRICE_TYPE_WITH_VAT
? $basicPrice->getPriceWithVat()->getAmount()
: $basicPrice->getPriceWithoutVat()->getAmount();
? $basicPrice->getPrice()->getPriceWithVat()->getAmount()
: $basicPrice->getPrice()->getPriceWithoutVat()->getAmount();

return new JsonResponse([
'basicPrice' => $basicPriceAmount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

class PricesWithCalculatedSellingPricesType extends AbstractType
class PricesByPricingGroupsType extends AbstractType
{
/**
* @param \Shopsys\FrameworkBundle\Model\Pricing\Group\PricingGroupFacade $pricingGroupFacade
Expand All @@ -29,12 +29,13 @@ public function __construct(
#[Override]
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$sellingPrices = $options['selling_prices'];
/** @var \Shopsys\FrameworkBundle\Model\Product\Pricing\ProductPrice[] $productPrices */
$productPrices = $options['product_prices'];

foreach ($this->pricingGroupFacade->getByDomainId($options['domain_id']) as $pricingGroup) {
$builder->add((string)$pricingGroup->getId(), MoneyWithCalculatedPriceType::class, [
'selling_price' => $sellingPrices !== null ? $sellingPrices[$pricingGroup->getId()]?->getSellingPrice() : null,
'block_prefix' => 'prices_with_calculated_selling_prices_input',
$builder->add((string)$pricingGroup->getId(), PricingGroupPriceType::class, [
'product_price' => $productPrices !== null ? $productPrices[$pricingGroup->getId()] : null,
'block_prefix' => 'pricing_group_price_input',
'scale' => 6,
'required' => false,
'invalid_message' => 'Please enter price in correct format (positive number with decimal separator)',
Expand All @@ -54,11 +55,11 @@ public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefaults([
'selling_prices' => null,
'product_prices' => null,
])
->setRequired(['domain_id'])
->setAllowedTypes('domain_id', 'int')
->setAllowedTypes('selling_prices', ['array', 'null']);
->setAllowedTypes('product_prices', ['array', 'null']);
}

/**
Expand All @@ -68,6 +69,6 @@ public function configureOptions(OptionsResolver $resolver): void
public function buildView(FormView $view, FormInterface $form, array $options): void
{
$view->vars['domain_id'] = $options['domain_id'];
$view->vars['selling_prices'] = $options['selling_prices'];
$view->vars['product_prices'] = $options['product_prices'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
namespace Shopsys\FrameworkBundle\Form\Admin\Product\Price;

use Override;
use Shopsys\FrameworkBundle\Model\Pricing\Price;
use Shopsys\FrameworkBundle\Model\Product\Pricing\ProductPriceInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

class MoneyWithCalculatedPriceType extends AbstractType
class PricingGroupPriceType extends AbstractType
{
/**
* {@inheritdoc}
Expand All @@ -21,9 +21,9 @@ class MoneyWithCalculatedPriceType extends AbstractType
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'selling_price' => null,
'product_price' => null,
]);
$resolver->setAllowedTypes('selling_price', [Price::class, 'null']);
$resolver->setAllowedTypes('product_price', [ProductPriceInterface::class, 'null']);
}

/**
Expand All @@ -32,7 +32,7 @@ public function configureOptions(OptionsResolver $resolver): void
#[Override]
public function buildView(FormView $view, FormInterface $form, array $options): void
{
$view->vars['selling_price'] = $options['selling_price'];
$view->vars['product_price'] = $options['product_price'];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
])
->add(
'manualInputPricesByPricingGroupId',
PricesWithCalculatedSellingPricesType::class,
PricesByPricingGroupsType::class,
[
'label' => false,
'required' => false,
'domain_id' => $options['domain_id'],
'selling_prices' => $options['selling_prices'],
'product_prices' => $options['product_prices'],
],
);
}
Expand All @@ -66,12 +66,12 @@ public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefaults([
'selling_prices' => null,
'product_prices' => null,
'data_class' => ProductInputPriceData::class,
])
->setRequired(['domain_id'])
->setAllowedTypes('domain_id', 'int')
->setAllowedTypes('selling_prices', ['array', 'null']);
->setAllowedTypes('product_prices', ['array', 'null']);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Form/Admin/Product/ProductFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -578,18 +578,18 @@ private function createPricesGroup(FormBuilderInterface $builder, ?Product $prod
return $builderPricesGroup;
}

$productSellingPricesIndexedByDomainId = null;
$productPricesIndexedByDomainId = null;

if ($product !== null) {
$productSellingPricesIndexedByDomainId = $this->productFacade->getAllProductSellingPricesIndexedByDomainId($product);
$productPricesIndexedByDomainId = $this->productFacade->getAllProductPricesIndexedByDomainId($product);
}

$optionsByDomainId = [];

foreach ($this->domain->getAllIds() as $domainId) {
$optionsByDomainId[$domainId] = [
'domain_id' => $domainId,
'selling_prices' => $productSellingPricesIndexedByDomainId[$domainId] ?? null,
'product_prices' => $productPricesIndexedByDomainId[$domainId] ?? null,
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Shopsys\FrameworkBundle\Model\Product\Parameter\ParameterValueDataFactory;
use Shopsys\FrameworkBundle\Model\Product\Parameter\ProductParameterValueDataFactory;
use Shopsys\FrameworkBundle\Model\Product\Parameter\ProductParameterValuesLocalizedData;
use Shopsys\FrameworkBundle\Model\Product\Parameter\ProductParameterValuesLocalizedDataFactory;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;

Expand All @@ -15,10 +15,12 @@ class ProductParameterValueToProductParameterValuesLocalizedTransformer implemen
/**
* @param \Shopsys\FrameworkBundle\Model\Product\Parameter\ProductParameterValueDataFactory $productParameterValueDataFactory
* @param \Shopsys\FrameworkBundle\Model\Product\Parameter\ParameterValueDataFactory $parameterValueDataFactory
* @param \Shopsys\FrameworkBundle\Model\Product\Parameter\ProductParameterValuesLocalizedDataFactory $productParameterValuesLocalizedDataFactory
*/
public function __construct(
protected readonly ProductParameterValueDataFactory $productParameterValueDataFactory,
protected readonly ParameterValueDataFactory $parameterValueDataFactory,
protected readonly ProductParameterValuesLocalizedDataFactory $productParameterValuesLocalizedDataFactory,
) {
}

Expand All @@ -44,7 +46,7 @@ public function transform($value): ?array
$locale = $productParameterValueData->parameterValueData->locale;

if (!array_key_exists($parameterId, $normData)) {
$normData[$parameterId] = new ProductParameterValuesLocalizedData();
$normData[$parameterId] = $this->productParameterValuesLocalizedDataFactory->create();
$normData[$parameterId]->parameter = $productParameterValueData->parameter;
$normData[$parameterId]->valueTextsByLocale = [];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Cart/CartFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function addProductToExistingCart(

$productPrice = $this->productPriceCalculation->calculatePriceForCurrentUser($product);
$notOnStockQuantity = $this->productAvailabilityFacade->getNotOnStockQuantity($product, $this->domain->getId(), $quantity) ?? 0;
$newCartItem = $this->cartItemFactory->create($cart, $product, $quantity, $productPrice->getPriceWithVat());
$newCartItem = $this->cartItemFactory->create($cart, $product, $quantity, $productPrice->getPrice()->getPriceWithVat());
$cart->addItem($newCartItem);
$cart->setModifiedNow();

Expand Down
10 changes: 5 additions & 5 deletions src/Model/Cart/Watcher/CartWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public function getModifiedPriceItemsAndUpdatePrices(Cart $cart)
$modifiedItems = [];

foreach ($cart->getItems() as $cartItem) {
$productPrice = $this->productPriceCalculationForCustomerUser->calculatePriceForCurrentUser(
$price = $this->productPriceCalculationForCustomerUser->calculatePriceForCurrentUser(
$cartItem->getProduct(),
);
)->getPrice();

if (!$productPrice->getPriceWithVat()->equals($cartItem->getWatchedPrice())) {
if (!$price->getPriceWithVat()->equals($cartItem->getWatchedPrice())) {
$modifiedItems[] = $cartItem;
}
$cartItem->setWatchedPrice($productPrice->getPriceWithVat());
$cartItem->setWatchedPrice($price->getPriceWithVat());
}

return $modifiedItems;
Expand Down Expand Up @@ -85,7 +85,7 @@ public function getNotListableItems(Cart $cart, CurrentCustomerUser $currentCust
$product,
);

if ($productPrice->getPriceWithVat()->equals(Money::zero())) {
if ($productPrice->getPrice()->getPriceWithVat()->equals(Money::zero())) {
$notListableItems[] = $item;
}
} catch (ProductNotFoundException $e) {
Expand Down
13 changes: 7 additions & 6 deletions src/Model/Order/Item/OrderItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Shopsys\FrameworkBundle\Model\Order\Item\Exception\WrongItemTypeException;
use Shopsys\FrameworkBundle\Model\Order\Order;
use Shopsys\FrameworkBundle\Model\Pricing\Price;
use Shopsys\FrameworkBundle\Model\Pricing\PriceInterface;

/**
* @ORM\Table(name="order_items")
Expand Down Expand Up @@ -147,7 +148,7 @@ class OrderItem
/**
* @param \Shopsys\FrameworkBundle\Model\Order\Order $order
* @param string $name
* @param \Shopsys\FrameworkBundle\Model\Pricing\Price $price
* @param \Shopsys\FrameworkBundle\Model\Pricing\PriceInterface $price
* @param string $vatPercent
* @param int $quantity
* @param string $type
Expand All @@ -157,7 +158,7 @@ class OrderItem
public function __construct(
Order $order,
string $name,
Price $price,
PriceInterface $price,
string $vatPercent,
int $quantity,
string $type,
Expand Down Expand Up @@ -228,9 +229,9 @@ public function getUnitPriceWithVat()
}

/**
* @return \Shopsys\FrameworkBundle\Model\Pricing\Price
* @return \Shopsys\FrameworkBundle\Model\Pricing\PriceInterface
*/
public function getPrice(): Price
public function getPrice(): PriceInterface
{
return new Price($this->unitPriceWithoutVat, $this->unitPriceWithVat);
}
Expand All @@ -257,9 +258,9 @@ public function getTotalPriceWithVat()
* The total price property can be used when order item has prices that differ from current price calculation implementation.
* Otherwise, it should be set to NULL (which means it will be calculated automatically).
*
* @param \Shopsys\FrameworkBundle\Model\Pricing\Price|null $totalPrice
* @param \Shopsys\FrameworkBundle\Model\Pricing\PriceInterface|null $totalPrice
*/
public function setTotalPrice(?Price $totalPrice): void
public function setTotalPrice(?PriceInterface $totalPrice): void
{
$this->totalPriceWithVat = $totalPrice?->getPriceWithVat();
$this->totalPriceWithoutVat = $totalPrice?->getPriceWithoutVat();
Expand Down
17 changes: 9 additions & 8 deletions src/Model/Order/Item/OrderItemData.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Shopsys\FrameworkBundle\Model\Order\Item;

use Shopsys\FrameworkBundle\Model\Pricing\Price;
use Shopsys\FrameworkBundle\Model\Pricing\PriceInterface;

class OrderItemData
{
Expand Down Expand Up @@ -93,35 +94,35 @@ class OrderItemData
public $relatedOrderItemsData = [];

/**
* @param \Shopsys\FrameworkBundle\Model\Pricing\Price $unitPrice
* @param \Shopsys\FrameworkBundle\Model\Pricing\PriceInterface $unitPrice
*/
public function setUnitPrice(Price $unitPrice): void
public function setUnitPrice(PriceInterface $unitPrice): void
{
$this->unitPriceWithVat = $unitPrice->getPriceWithVat();
$this->unitPriceWithoutVat = $unitPrice->getPriceWithoutVat();
}

/**
* @param \Shopsys\FrameworkBundle\Model\Pricing\Price $totalPrice
* @param \Shopsys\FrameworkBundle\Model\Pricing\PriceInterface $totalPrice
*/
public function setTotalPrice(Price $totalPrice): void
public function setTotalPrice(PriceInterface $totalPrice): void
{
$this->totalPriceWithVat = $totalPrice->getPriceWithVat();
$this->totalPriceWithoutVat = $totalPrice->getPriceWithoutVat();
}

/**
* @return \Shopsys\FrameworkBundle\Model\Pricing\Price
* @return \Shopsys\FrameworkBundle\Model\Pricing\PriceInterface
*/
public function getTotalPrice(): Price
public function getTotalPrice(): PriceInterface
{
return new Price($this->totalPriceWithoutVat, $this->totalPriceWithVat);
}

/**
* @return \Shopsys\FrameworkBundle\Model\Pricing\Price
* @return \Shopsys\FrameworkBundle\Model\Pricing\PriceInterface
*/
public function getUnitPrice(): Price
public function getUnitPrice(): PriceInterface
{
return new Price($this->unitPriceWithoutVat, $this->unitPriceWithVat);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Order/Item/OrderItemFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function addProductToOrder(int $orderId, int $productId): OrderItem
$product,
$order->getDomainId(),
$order->getCustomerUser(),
);
)->getPrice();

$orderItemData = $this->orderItemDataFactory->create(OrderItemTypeEnum::TYPE_PRODUCT);
$orderItemData->name = $product->getName($orderDomainConfig->getLocale());
Expand Down
7 changes: 4 additions & 3 deletions src/Model/Order/Item/OrderItemPriceCalculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Shopsys\FrameworkBundle\Model\Order\Item\Exception\OrderItemHasNoIdException;
use Shopsys\FrameworkBundle\Model\Pricing\Price;
use Shopsys\FrameworkBundle\Model\Pricing\PriceCalculation;
use Shopsys\FrameworkBundle\Model\Pricing\PriceInterface;
use Shopsys\FrameworkBundle\Model\Pricing\Vat\VatDataFactory;
use Shopsys\FrameworkBundle\Model\Pricing\Vat\VatFactory;

Expand Down Expand Up @@ -43,9 +44,9 @@ public function calculatePriceWithoutVat(OrderItemData $orderItemData, int $doma

/**
* @param \Shopsys\FrameworkBundle\Model\Order\Item\OrderItem $orderItem
* @return \Shopsys\FrameworkBundle\Model\Pricing\Price
* @return \Shopsys\FrameworkBundle\Model\Pricing\PriceInterface
*/
public function calculateTotalPrice(OrderItem $orderItem): Price
public function calculateTotalPrice(OrderItem $orderItem): PriceInterface
{
if ($orderItem->hasForcedTotalPrice()) {
return new Price($orderItem->getTotalPriceWithoutVat(), $orderItem->getTotalPriceWithVat());
Expand All @@ -65,7 +66,7 @@ public function calculateTotalPrice(OrderItem $orderItem): Price

/**
* @param \Shopsys\FrameworkBundle\Model\Order\Item\OrderItem[] $orderItems
* @return \Shopsys\FrameworkBundle\Model\Pricing\Price[]
* @return \Shopsys\FrameworkBundle\Model\Pricing\PriceInterface[]
*/
public function calculateTotalPricesIndexedById($orderItems): array
{
Expand Down
Loading

0 comments on commit 4544fd2

Please sign in to comment.