6 declare(strict_types=1);
11 use Magento\Framework\Validation\ValidationResultFactory;
22 private $validationResultFactory;
27 private $defaultSourceProvider;
32 private $defaultStockProvider;
40 ValidationResultFactory $validationResultFactory,
44 $this->validationResultFactory = $validationResultFactory;
46 $this->defaultStockProvider = $defaultStockProvider;
54 $initialAssignment = $this->isInitialAssignment(
$link);
55 $linkContainDefaultSourceOrStock = $this->isLinkContainDefaultSourceOrStock(
$link);
57 if (!$initialAssignment && $linkContainDefaultSourceOrStock) {
58 $errors[] =
__(
'Can not save link related to Default Source or Default Stock');
61 return $this->validationResultFactory->create([
'errors' =>
$errors]);
72 $defaultStockId = $this->defaultStockProvider->getId();
73 $defaultSourceCode = $this->defaultSourceProvider->getCode();
74 $linkStockId =
$link->getStockId();
75 $linkSourceCode =
$link->getSourceCode();
76 $initialAssignment =
false;
77 if ($defaultStockId === $linkStockId && $defaultSourceCode === $linkSourceCode) {
78 $initialAssignment =
true;
80 return $initialAssignment;
91 private function isLinkContainDefaultSourceOrStock(StockSourceLinkInterface
$link)
93 $defaultStockId = $this->defaultStockProvider->getId();
94 $defaultSourceCode = $this->defaultSourceProvider->getCode();
95 $linkStockId =
$link->getStockId();
96 $linkSourceCode =
$link->getSourceCode();
97 $linkContainDefaultSourceOrStock =
false;
98 if ($linkStockId === $defaultStockId || $linkSourceCode === $defaultSourceCode) {
99 $linkContainDefaultSourceOrStock =
true;
101 return $linkContainDefaultSourceOrStock;
__construct(ValidationResultFactory $validationResultFactory, DefaultSourceProviderInterface $defaultSourceProvider, DefaultStockProviderInterface $defaultStockProvider)
validate(StockSourceLinkInterface $link)