Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SalesChannelsValidator.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
12 use Magento\Framework\Validation\ValidationResultFactory;
17 
22 {
26  private $validationResultFactory;
27 
31  private $websiteRepository;
32 
37  public function __construct(
38  ValidationResultFactory $validationResultFactory,
39  WebsiteRepositoryInterface $websiteRepository
40  ) {
41  $this->validationResultFactory = $validationResultFactory;
42  $this->websiteRepository = $websiteRepository;
43  }
44 
49  {
50  $extensionAttributes = $stock->getExtensionAttributes();
51  $salesChannels = $extensionAttributes->getSalesChannels();
52 
53  $errors = [];
54  if (is_array($salesChannels)) {
55  foreach ($salesChannels as $salesChannel) {
56  $type = (string)$salesChannel->getType();
57  if ('' === trim($type)) {
58  $errors[] = __('"%field" can not be empty.', ['field' => SalesChannelInterface::TYPE]);
59  }
60 
61  $code = (string)$salesChannel->getCode();
62  if ('' === trim($code)) {
63  $errors[] = __('"%field" can not be empty.', ['field' => SalesChannelInterface::CODE]);
64  }
65 
67  try {
68  $this->websiteRepository->get($code);
69  } catch (NoSuchEntityException $e) {
70  $errors[] = __('The website with code "%code" does not exist.', ['code' => $code]);
71  }
72  }
73  }
74  }
75 
76  return $this->validationResultFactory->create(['errors' => $errors]);
77  }
78 }
__construct(ValidationResultFactory $validationResultFactory, WebsiteRepositoryInterface $websiteRepository)
__()
Definition: __.php:13
$type
Definition: item.phtml:13
$extensionAttributes
Definition: payment.php:22
$stock
$errors
Definition: overview.phtml:9
$code
Definition: info.phtml:12