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 240d585
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 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
33 changes: 30 additions & 3 deletions src/Service/Stock/StockManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,61 @@
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;
$this->enableStockManagement = false;

/**
* 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);
}
}

/**
* @param OrderLineItemEntity $lineItem
* @param int $quantity
* @param string $mollieRefundID
* @throws \Doctrine\DBAL\Exception
* @return void
* @throws \Doctrine\DBAL\Exception
*/
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 +81,9 @@ public function increaseStock(OrderLineItemEntity $lineItem, int $quantity, stri
]
);
}

private function isEnabled(): bool
{
return $this->enableStockManagement && Feature::isActive('STOCK_HANDLING');
}
}

0 comments on commit 240d585

Please sign in to comment.