Skip to content

Commit

Permalink
MOL-1285: use stock manager config
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalij Mik committed Dec 18, 2023
1 parent bb26750 commit 087a3e0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Resources/config/services/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@

<service id="Kiener\MolliePayments\Service\Stock\StockManager">
<argument type="service" id="Doctrine\DBAL\Connection"/>
<argument type="service" id="service_container"/>
</service>


Expand Down
40 changes: 38 additions & 2 deletions src/Service/Stock/StockManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,44 @@
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\RetryableQuery;
use Shopware\Core\Framework\Feature;
use Shopware\Core\Framework\Uuid\Uuid;
use Symfony\Component\DependencyInjection\ContainerInterface;

class StockManager implements StockManagerInterface
{
private const STOCK_MANAGER_PARAMETER_NAME = 'shopware.stock.enable_stock_management';
/**
* @var Connection
*/
private $connection;

/**
* @var bool
*/
private $enableStockManagement;


/**
* @param Connection $connection
* @param ContainerInterface $container
*/
public function __construct(Connection $connection)
public function __construct(Connection $connection, ContainerInterface $container)
{
$this->connection = $connection;

/**
* Enable stock management per default and disable it by config
*/
$this->enableStockManagement = true;

/**
* We have to use here the container because the parameter does not exists in earlier shopware versions and we get an exceptions
* when activating the plugin
*/
if ($container->hasParameter(self::STOCK_MANAGER_PARAMETER_NAME)) {
$this->enableStockManagement = (bool)$container->getParameter(self::STOCK_MANAGER_PARAMETER_NAME);
}
}

/**
Expand All @@ -35,12 +57,16 @@ public function __construct(Connection $connection)
*/
public function increaseStock(OrderLineItemEntity $lineItem, int $quantity, string $mollieRefundID): void
{
if ($this->isEnabled() === false) {
return;
}

if ($lineItem->getPayload() === null) {
return;
}

# check if we have a product
if (!isset($lineItem->getPayload()['productNumber'])) {
if (! isset($lineItem->getPayload()['productNumber'])) {
return;
}

Expand All @@ -59,4 +85,14 @@ public function increaseStock(OrderLineItemEntity $lineItem, int $quantity, stri
]
);
}

/**
* We do not listen to Feature::isActive('STOCK_HANDLING') this feature is disabled by default and enabled in later versions
* if we listen to this feature, we will introduce breaking changes and this feature has to be enabled explicit so the refund manager will increase the stock
* @return bool
*/
private function isEnabled(): bool
{
return $this->enableStockManagement;
}
}

0 comments on commit 087a3e0

Please sign in to comment.