Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Save.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
15 use Psr\Log\LoggerInterface;
16 
20 class Save implements SaveInterface
21 {
25  private $stockValidator;
26 
30  private $stockResource;
31 
35  private $logger;
36 
42  public function __construct(
43  StockValidatorInterface $stockValidator,
44  StockResourceModel $stockResource,
45  LoggerInterface $logger
46  ) {
47  $this->stockValidator = $stockValidator;
48  $this->stockResource = $stockResource;
49  $this->logger = $logger;
50  }
51 
55  public function execute(StockInterface $stock): int
56  {
57  $validationResult = $this->stockValidator->validate($stock);
58  if (!$validationResult->isValid()) {
59  throw new ValidationException(__('Validation Failed'), null, 0, $validationResult);
60  }
61 
62  try {
63  $this->stockResource->save($stock);
64  return (int)$stock->getStockId();
65  } catch (\Exception $e) {
66  $this->logger->error($e->getMessage());
67  throw new CouldNotSaveException(__('Could not save Stock'), $e);
68  }
69  }
70 }
__construct(StockValidatorInterface $stockValidator, StockResourceModel $stockResource, LoggerInterface $logger)
Definition: Save.php:42
__()
Definition: __.php:13
$logger
execute(StockInterface $stock)
Definition: Save.php:55
$stock