Skip to content

Commit ffaba3a

Browse files
committed
PAC-354: refactoring
1 parent fc6c84f commit ffaba3a

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

src/Callbacks/StoreWebsiteValidatorCallback.php

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
*/
3030
class StoreWebsiteValidatorCallback extends ArrayValidatorCallback
3131
{
32-
3332
/**
3433
* The flag to query whether or not the value can be empty.
3534
*
@@ -61,11 +60,11 @@ class StoreWebsiteValidatorCallback extends ArrayValidatorCallback
6160
/**
6261
* Initializes the callback with the loader instance.
6362
*
64-
* @param \TechDivision\Import\Loaders\LoaderInterface $storeLoader The loader instance to load the validations with
65-
* @param ImportProcessorInterface $importProcessor The loader instance to load the validations with
66-
* @param boolean $nullable The flag to decide whether or not the value can be empty
67-
* @param boolean $mainRowOnly The flag to decide whether or not the value has to be validated on the main row only
68-
* @param boolean $ignoreStrictMode The flag to query whether or not the value has to be ignored global strict mode configuration.
63+
* @param LoaderInterface $storeLoader The loader instance to load the validations with
64+
* @param ImportProcessorInterface $importProcessor The import processor instance
65+
* @param boolean $nullable The flag to decide whether or not the value can be empty
66+
* @param boolean $mainRowOnly The flag to decide whether or not the value has to be validated on the main row only
67+
* @param boolean $ignoreStrictMode The flag to query whether or not the value has to be ignored global strict mode configuration.
6968
*/
7069
public function __construct(LoaderInterface $storeLoader, ImportProcessorInterface $importProcessor, $nullable = false, $mainRowOnly = false, $ignoreStrictMode = true)
7170
{
@@ -78,53 +77,44 @@ public function __construct(LoaderInterface $storeLoader, ImportProcessorInterfa
7877
$this->mainRowOnly = $mainRowOnly;
7978
$this->ignoreStrictMode = $ignoreStrictMode;
8079

81-
// load the store websites
82-
$storeWebsites = $importProcessor->getStoreWebsites();
83-
8480
// initialize the array with the store websites
85-
foreach ($storeWebsites as $storeWebsite) {
81+
foreach ($importProcessor->getStoreWebsites() as $storeWebsite) {
8682
$this->storeWebsites[$storeWebsite[MemberNames::CODE]] = $storeWebsite[MemberNames::WEBSITE_ID];
8783
}
8884
}
8985

9086
/**
9187
* Will be invoked by a observer it has been registered for.
9288
*
93-
* @param string|null $attributeCode The code of the attribute that has to be validated
89+
* @param string|null $attributeCode The code of the attribute that has to be validated
9490
* @param string|null $attributeValue The attribute value to be validated
9591
*
9692
* @return mixed The modified value
9793
*/
9894
public function handle($attributeCode = null, $attributeValue = null)
9995
{
100-
101-
// the validations for the attribute with the given code
102-
$validations = $this->getValidations($attributeCode);
103-
10496
// query whether or not the passed value IS empty and empty
10597
// values are allowed
10698
if ($this->isNullable($attributeValue)) {
10799
return;
108100
}
109101

102+
// the validations for the attribute with the given code
103+
$validations = $this->getValidations($attributeCode);
104+
110105
$website = $this->load();
111106
$productWebsite = $this->getSubject()->getValue('product_websites');
112-
$message = sprintf(
113-
'The store "%s" does not belong to the website "%s" . Please check your data.',
114-
$attributeValue,
115-
$productWebsite
116-
);
117107

118-
if ($validations[$attributeValue][MemberNames::WEBSITE_ID] === $website[$productWebsite]) {
119-
return;
120-
} else {
121-
if ($this->hasHandleStrictMode($attributeCode, $message)) {
122-
return;
108+
if ($validations[$attributeValue][MemberNames::WEBSITE_ID] !== $website[$productWebsite]) {
109+
$message = sprintf(
110+
'The store "%s" does not belong to the website "%s" . Please check your data.',
111+
$attributeValue,
112+
$productWebsite
113+
);
114+
if (!$this->hasHandleStrictMode($attributeCode, $message)) {
115+
throw new \InvalidArgumentException($message);
123116
}
124117
}
125-
126-
// throw an exception if the store not in Website
127-
throw new \InvalidArgumentException($message);
128118
}
129119

130120
/**

0 commit comments

Comments
 (0)