Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
TransferSourceValidator.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
12 use Magento\Framework\Validation\ValidationResultFactory;
15 
21 {
25  private $validationResultFactory;
26 
30  private $sourceRepository;
31 
37  public function __construct(
38  ValidationResultFactory $validationResultFactory,
39  SourceRepositoryInterface $sourceRepository
40  ) {
41  $this->validationResultFactory = $validationResultFactory;
42  $this->sourceRepository = $sourceRepository;
43  }
44 
48  public function validate(array $skus, string $originSource, string $destinationSource): ValidationResult
49  {
50  $errors = [];
51 
52  try {
53  $this->sourceRepository->get($originSource);
54  } catch (NoSuchEntityException $e) {
55  $errors[] = __('Origin source %sourceCode does not exist', ['sourceCode' => $originSource]);
56  }
57 
58  try {
59  $this->sourceRepository->get($destinationSource);
60  } catch (NoSuchEntityException $e) {
61  $errors[] = __('Destination source %sourceCode does not exist', ['sourceCode' => $destinationSource]);
62  }
63 
64  if ($originSource === $destinationSource) {
65  $errors[] = __('Cannot transfer a source on itself');
66  }
67 
68  return $this->validationResultFactory->create(['errors' => $errors]);
69  }
70 }
__()
Definition: __.php:13
foreach($websiteCodes as $websiteCode) $skus
__construct(ValidationResultFactory $validationResultFactory, SourceRepositoryInterface $sourceRepository)
validate(array $skus, string $originSource, string $destinationSource)
$sourceRepository
Definition: source.php:20
$errors
Definition: overview.phtml:9