Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DeleteById.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 use Magento\InventoryApi\Api\Data\StockInterfaceFactory;
15 use Psr\Log\LoggerInterface;
16 
21 {
25  private $stockResource;
26 
30  private $stockFactory;
31 
35  private $logger;
36 
42  public function __construct(
43  StockResourceModel $stockResource,
44  StockInterfaceFactory $stockFactory,
45  LoggerInterface $logger
46  ) {
47  $this->stockResource = $stockResource;
48  $this->stockFactory = $stockFactory;
49  $this->logger = $logger;
50  }
51 
55  public function execute(int $stockId)
56  {
58  $stock = $this->stockFactory->create();
59  $this->stockResource->load($stock, $stockId, StockInterface::STOCK_ID);
60 
61  if (null === $stock->getStockId()) {
62  throw new NoSuchEntityException(
63  __(
64  'There is no stock with "%fieldValue" for "%fieldName". Verify and try again.',
65  [
66  'fieldName' => StockInterface::STOCK_ID,
67  'fieldValue' => $stockId
68  ]
69  )
70  );
71  }
72 
73  try {
74  $this->stockResource->delete($stock);
75  } catch (\Exception $e) {
76  $this->logger->error($e->getMessage());
77  throw new CouldNotDeleteException(__('Could not delete Stock'), $e);
78  }
79  }
80 }
__construct(StockResourceModel $stockResource, StockInterfaceFactory $stockFactory, LoggerInterface $logger)
Definition: DeleteById.php:42
__()
Definition: __.php:13
$logger
$stockFactory
Definition: stock.php:15
$stock