|
3 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
| 6 | + |
6 | 7 | namespace Magento\Inventory\Model\Source\Validator;
|
7 | 8 |
|
8 | 9 | use Magento\Framework\Validation\ValidationResult;
|
@@ -42,29 +43,47 @@ public function __construct(ValidationResultFactory $validationResultFactory, Co
|
42 | 43 | public function validate(SourceInterface $source): ValidationResult
|
43 | 44 | {
|
44 | 45 | $carrierLinks = $source->getCarrierLinks();
|
45 |
| - |
46 | 46 | $errors = [];
|
47 |
| - if (null !== $carrierLinks) { |
48 |
| - if (!is_array($carrierLinks)) { |
49 |
| - $errors[] = __('"%field" must be list of SourceCarrierLinkInterface.', [ |
50 |
| - 'field' => SourceInterface::CARRIER_LINKS |
51 |
| - ]); |
52 |
| - } elseif (count($carrierLinks) && $source->isUseDefaultCarrierConfig()) { |
53 |
| - $errors[] = __('You can\'t configure "%field" because you have chosen Global Shipping configuration.', [ |
54 |
| - 'field' => SourceInterface::CARRIER_LINKS |
55 |
| - ]); |
56 |
| - } |
57 | 47 |
|
58 |
| - $availableCarriers = $this->shippingConfig->getAllCarriers(); |
59 |
| - foreach ($carrierLinks as $carrierLink) { |
60 |
| - $carrierCode = $carrierLink->getCarrierCode(); |
61 |
| - if (array_key_exists($carrierCode, $availableCarriers) === false) { |
62 |
| - $errors[] = __('You can\'t configure because carrier with code: "%carrier" don\'t exists.', [ |
63 |
| - 'carrier' => $carrierCode |
64 |
| - ]); |
65 |
| - } |
| 48 | + if (null === $carrierLinks) { |
| 49 | + return $this->buildValidationResult($errors); |
| 50 | + } |
| 51 | + |
| 52 | + if (!is_array($carrierLinks)) { |
| 53 | + $errors[] = __('"%field" must be list of SourceCarrierLinkInterface.', [ |
| 54 | + 'field' => SourceInterface::CARRIER_LINKS |
| 55 | + ]); |
| 56 | + return $this->buildValidationResult($errors); |
| 57 | + } |
| 58 | + |
| 59 | + if (count($carrierLinks) && $source->isUseDefaultCarrierConfig()) { |
| 60 | + $errors[] = __('You can\'t configure "%field" because you have chosen Global Shipping configuration.', [ |
| 61 | + 'field' => SourceInterface::CARRIER_LINKS |
| 62 | + ]); |
| 63 | + return $this->buildValidationResult($errors); |
| 64 | + } |
| 65 | + |
| 66 | + $availableCarriers = $this->shippingConfig->getAllCarriers(); |
| 67 | + foreach ($carrierLinks as $carrierLink) { |
| 68 | + $carrierCode = $carrierLink->getCarrierCode(); |
| 69 | + if (array_key_exists($carrierCode, $availableCarriers) === false) { |
| 70 | + $errors[] = __('You can\'t configure because carrier with code: "%carrier" don\'t exists.', [ |
| 71 | + 'carrier' => $carrierCode |
| 72 | + ]); |
66 | 73 | }
|
67 | 74 | }
|
| 75 | + |
| 76 | + return $this->buildValidationResult($errors); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Build the ValidationResult by given errors. |
| 81 | + * |
| 82 | + * @param array $errors |
| 83 | + * @return ValidationResult |
| 84 | + */ |
| 85 | + private function buildValidationResult(array $errors): ValidationResult |
| 86 | + { |
68 | 87 | return $this->validationResultFactory->create(['errors' => $errors]);
|
69 | 88 | }
|
70 | 89 | }
|
0 commit comments